How to load array of object into extjs gridpanel - arrays

I am new to ExtJS. In examples, I do not find any one similar to my situation: I have an Array of Object like bellow:
arr = new Array();
obj = new Object();
obj.StringField = "Field1";
obj.IntField = 1;
arr.push(obj);
obj = new Object();
obj.StringField = "Field2";
obj.IntField = 3;
arr.push(obj);
obj = new Object();
obj.StringField = "Field3";
obj.IntField = 5;
arr.push(obj);
How to put this in to GridPanel?

You can create a grid panel with an ArrayReader : http://www.sencha.com/learn/Manual:Data:Readers:ArrayReader
There's also an example: http://dev.sencha.com/deploy/ext/examples/grid/array-grid.html

Related

Action Script 3 URLLoader in for loop

I have 7 Arrays to begin with:
private var listArray:Array = new Array();
private var oneArray:Array = new Array();
private var twoArray:Array = new Array();
private var threeArray:Array = new Array();
private var fourArray:Array = new Array();
private var fiveArray:Array = new Array();
private var sixArray:Array = new Array();
listArray contain 6 string element of text file name.
something like:
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
All other array is empty at the moment.
I have wrote a for loop like this:
for (var i:int = 0; i < listArray.length; i++)
{
var urlRequest:URLRequest = new URLRequest(File.documentsDirectory.resolvePath(listArray[i]).url);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try
{
urlLoader.load(urlRequest);
}catch (error:Error){
trace("Cannot load : " + error.message);
}
}
if without for loop I know I can do this for only one array of data:
private function completeHandler(e:Event):void
{
oneArray = e.target.data.split(/\r\n/);
}
Here I am trying to get something to work like:
oneArray contain the data from 1.txt
twoArray contain the data from 2.txt
so on...
sixArray contain the data from 6.txt
problem:
I known the completeHandler function only execute after for loop looped six times.
is there anyway I could get the correct data to the correct array.
Thanks
Since you are using AIR to load data from the file-system, you don't have to do it asynchronously. You can load it synchronously like this:
function readTxtList(url:String):Array {
var file:File = File.documentsDirectory.resolvePath(url);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var text:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
return text.split("\r\n");
}
Now you can just assign each value directly:
var oneArray:Array = readTxtList("1.txt");
var twoArray:Array = readTxtList("2.txt");
// etc
I recommend you to use Dictionary.
Create new Dictionary:
var dict:Dictionary = new Dictionary();
Bind an instance of the URLLoader class to the file name:
var urlRequest:URLRequest = ...
var urlLoader:URLLoader = new URLLoader();
dict[urlLoader] = listArray[i];
In the completeHandler you can get the file name:
trace(dict[e.currentTarget]);
Add if statements to reach your goal.
if (dict[e.currentTarget] == "1.txt")
oneArray = e.target.data.split(/\r\n/);
else if (dict[e.currentTarget] == "2.txt")
twoArray = e.target.data.split(/\r\n/);
...

Can i display multiple copies of a movieclip inside a array at once

Is there a way to make the code below work properly? When I use this code it only shows one movieclip:
var tempHead:head001 = new head001();
var mcArr:Array = new Array( tempHead );
var firstHead:MovieClip = mcArr[0];
firstHead.y = 30;
addChild(firstHead);
var secondHead:MovieClip = mcArr[0];
secondHead.y = 180;
addChild(secondHead);
`
You were just assigning a reference to the MovieClip. That'y, its not working.
First take instance of head001 class using new operator as much you want and store it to an array, then you can access very easily.
var tempHead: head001;
var mcArr: Array = new Array();
for (var i: uint = 0; i < 2; i++) {
tempHead = new head001();
addChild(tempHead);
mcArr.push(tempHead);
mcArr[i].y = mcArr[i].height * i;
}

AS3 Add array to another array

Example of the my problem.
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
// and the out come I want it to be is this
trace(array_1[0]) // 2,4,6,8,10,12,14,16,20,22,24
// I did try array_1[0] += array_2[0] but it didn't work currently
Any suggestion would be great.
This will perform what you are looking for and also allows you to add multiple rows of data to array_1 or array_2
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:Array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
var combinedArray:Array = new Array();
for( var i:int = 0; i < array_1.length; i++ ) {
combinedArray = combinedArray.concat(array_1[i]);
}
for( i = 0; i < array_2.length; i++ ) {
combinedArray = combinedArray.concat(array_2[i]);
}
trace(combinedArray);
As stated in the comments, you can use the concat method:
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
array_1[0] = array_1[0].concat(array_2[0]).concat(array_2[1]);
This, of course, is very messy looking. I am wondering why you are storing arrays inside of other arrays for no discernible reason.

Actionscript 3, Flash CC: Placing Objects In An Array From The Library Onto The Stage

Hello programming gurus of stackoverflow, I am hoping that at least one of you will be able to help me with my coding problem. This is the first time I'm posting on this site, so if I miss something with the structure of my post, or anything please let me know (preferably not in a condescending matter) and I will gladly change it.
I actually had a different problem I was going to ask about, but I recently realized that some objects from my library weren't showing up on my stage. Hopefully, if this gets solved I won't have my other problem.
I am creating a learning module app using Flash CC and Actionscript 3, I like to think I am fairly proficient with Flash, but right now all my code is on the timeline because when I started I wasn't aware of the package setup. When I finish with the learning module I'll try and move everything to an AS package, so please bear with me.
This current frame of the module is a drag and drop game where the user drags the correct food, for the animal they chose in the previous frame, to the animal in the middle. The animal is dynamically placed on the stage, as well as an array of six possible food choices, all MovieClips pulled from the library. The array of food elements is actually not what I'm having problem with, they appear on my stage with no problems at all. The problem I'm having is when the user drags the correct food onto the animal, and the win condition is met, the array of balloon elements does not show up on the stage. I find it weird because I'm using near identical code for both the food and balloon array.
Here is my full code:
import flash.display.MovieClip;
import flash.events.MouseEvent;
foodPet();
function foodPet():void {
//all of my pet, food, and balloon library objects have been exported for AS
var theBird:pet_bird = new pet_bird;
var theCat:pet_cat = new pet_cat;
var theChicken:pet_chicken = new pet_chicken;
var theDog:pet_dog = new pet_dog;
var theDuck:pet_duck = new pet_duck;
var theGuinea:pet_guinea = new pet_guinea;
var theHamster:pet_hamster = new pet_hamster;
var birdSeed:food_bird_seed = new food_bird_seed;
var catFood:food_cat_food = new food_cat_food;
var chickenFeed:food_chicken_feed = new food_chicken_feed;
var chocolate:food_chocolate = new food_chocolate;
var dogFood:food_dog_food = new food_dog_food;
var duckFood:food_duck_food = new food_duck_food;
var animalList:Array = [theBird, theCat, theChicken, theDog,
theDuck, theGuinea, theHamster];
var food1Array:Array = [birdSeed, catFood, chickenFeed,
chocolate, dogFood, duckFood, 4];
var xPosFood:Array = new Array();
var yPosFood:Array = new Array();
xPosFood = [32, 71, 146, 363, 431, 512];
yPosFood = [304, 222, 123, 123, 222, 304];
var animalClip:MovieClip;
animalClip = animalList[chosenAnimal];
addChild(animalClip);
animalClip.x = 256;
animalClip.y = 287;
animalClip.name = "selectedAnimal";
for (var i:uint = 0; i < food1Array.length - 1; i++){ //Where the food gets added
var isItRight:Boolean = false;
var foodName:String = ("food" + i);
var foodClip:MovieClip;
foodClip = food1Array[i];
foodClip.x = xPosFood[i];
foodClip.y = yPosFood[i];
foodClip.name = foodName;
addChild(foodClip);
trace(foodClip.parent);
foodDragSetup(foodClip, animalClip, food1Array[food1Array.length - 1], isItRight);
}
}
function foodDragSetup(clip:MovieClip, targ:MovieClip, correctNum:uint, isItRight:Boolean) {
var beingDragged:Boolean = false;
var xPos:Number = clip.x;
var yPos:Number = clip.y;
clip.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(event:MouseEvent):void
{
clip.startDrag();
if (int(clip.name.substr(4)) == correctNum){
isItRight = true;
}
this.beingDragged = true;
setChildIndex(clip, numChildren - 1);
clip.addEventListener(MouseEvent.MOUSE_UP, endDrag);
}
function endDrag(event:MouseEvent):void
{
if (this.beingDragged) {
this.beingDragged = false;
clip.stopDrag();
if ((isItRight) && (clip.hitTestPoint(targ.x, targ.y, true))){
trace(targ.name + " has been hit.");
clip.x = targ.x;
clip.y = targ.y;
win_animal_food();
} else {
isItRight = false;
clip.x = xPos;
clip.y = yPos;
}
}
}
}
function win_animal_food():void {
const BALLOON_ROW:int = 4;
var count:uint = 0;
var altX:uint = 0;
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
var balloonY:Array = [144, -205, -265, -325];
var balloonX:Array = [0, 140, 284, 428, 68, 212, 356, 500];
for (var ballY:uint = 0; ballY < balloonY.length; ballY++){ //Where balloons
for (var ballX:uint = altX; ballX < altX + BALLOON_ROW; ballX++){ //get added
var balloonName:String = ("balloon" + count);
var balloonClip:MovieClip;
balloonClip = balloonList[count];
balloonClip.x = balloonX[ballX];
balloonClip.y = balloonY[ballY];
balloonClip.name = balloonName;
addChild(balloonClip);
trace(balloonClip.parent);
trace(balloonClip + " has been added!");
balloonClip.addEventListener(MouseEvent.CLICK, balloonPop);
count++;
}
if (altX == 0) {
altX = BALLOON_ROW;
} else {
altX = 0;
}
}
function balloonPop(event:MouseEvent):void {
event.target.play();
event.target.removeEventListener(MouseEvent.CLICK, balloonPop);
}
}
I thought there might have been a problem with my balloon MovieClips, so I subbed them in the food array:
var birdSeed:blue_balloon = new blue_balloon;
var catFood:green_balloon = new green_balloon;
var chickenFeed:orange_balloon = new orange_balloon;
var chocolate:purple_balloon = new purple_balloon;
var dogFood:red_balloon = new red_balloon;
var duckFood:yellow_balloon = new yellow_balloon;
They all showed up on the stage, so there's nothing wrong with the MovieClips.
Added: The first values of balloonXArray and balloonYArray were originally -4 and -145 respectively, but when I started having problems I wanted to make sure the balloons were showing up so I set the first values to 0 and 144 the balloon height and width are both 144 and their cross (not sure on it's name) is in the top left corner.
Added: The reason why there are multiple instances of the same balloon in the balloonList is because I need four rows of four balloons, but only have six different balloons.
I know the balloons are on the stage because the debug display shows their x and y values on the viewable stage. Using trace(foodClip.parent) and trace(balloonClip.parent) shows that the balloons and food all have the same parent, MainTimeline, so I know the balloons aren't getting added to some different space.
I have searched online, but have not come across anyone with a similar problem. Thus, I am asking on this forum if anyone can tell me why my balloons will not show up on the stage.
Please and thank you.
One thing I see straight off in the baloonList is that you have the same object instances listed multiple times. Each instance can only exist on stage exactly once. If you addChild() an instance that is already on stage, the instance is first removed, then re-added at the top of the display list.
You should change:
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
to:
var balloonList:Array = [
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon
];

How to get size of bitmapdata converted into bytearray

I have bitmapdata object converted to bytearray:
var bytes:ByteArray = bitmapDataA.getPixels(bitmapDataA.rect);
and I save it as .jpg file for later:
writeStream.writeBytes(bytes, 0, bytes.bytesAvailable);
Now, when I want to retrieve the data:
var file:File = File.documentsDirectory.resolvePath("data/"+photoNameVec[loadCounter]);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var fileContent:ByteArray = new ByteArray;
fileStream.readBytes(fileContent);
fileStream.close();
var bmpData:BitmapData = new BitmapData(XXX, YYY);
bmpData.setPixels(bmpData.rect, fileContent);
addChild(new Bitmap(bmpData));
But I need to get exact XXX and YYY to show the graphics correctly. How can I do this?
EDIT:
As there was no way to determine bitmapdata width and height based on bytearray, I decided to transform all pictures I take to 1024px x 768px:
var bitmapDataA:BitmapData = new BitmapData(mpLoaderInfo.width, mpLoaderInfo.height);
var matrix:Matrix = new Matrix();
matrix.scale( 1024 / mpLoaderInfo.width, 768 / mpLoaderInfo.height);
var bitmapDataB:BitmapData = new BitmapData(1024,768);
bitmapDataB.draw(mpLoaderInfo.content, matrix, null, null, new Rectangle(0,0,1024,768), true);
Still waiting for tips on getting Width x Height from bytearray created by getPixels method of bitmapdata.
You can use a Loader class to load the image then you can access it's width/height and BitmapData
var file:File = File.documentsDirectory.resolvePath("data/"+photoNameVec[loadCounter]);
var myImage:BitmapData;
var loader:Loader = new Loader();
var urlReq:URLRequest = new URLRequest(file.url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
loader.load(urlReq);
function loaded(e:Event):void
{
var bmp:Bitmap = e.target.content as Bitmap;
myImage = bmp.bitmapData;
trace(myImage.width, myImage.height);
}
And here is with loadBytes
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, loaded);
loader.loadBytes(yourByteArray);
function loaded(e:Event):void
{
var bmp:Bitmap = e.target.content as Bitmap;
myImage = bmp.bitmapData;
trace(myImage.width, myImage.height);
}
I guess that's the most suiting solution :)
Simply expand the the bytearray with extra values from which I shall gather width and height.
http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/
Ps.: Creating and reading uncompressed files is way faster than making png/jpg
The byte array created by getPixels does not contain any information on the height or width of the bitmap, it's simply an "unsigned integer (a 32-bit unmultiplied pixel value) for each pixel".
So you'll need to either write it as an image format (PNG/JPG) which will require encoding (hence time), or simply add the width and height to your byte array before writing to file.
Eg in your stream writer:
writeStream.writeInt( bitmapDataA.width );
writeStream.writeInt( bitmapDataA.height );
writeStream.writeBytes(bytes, 0, bytes.bytesAvailable);
Then when you are loading:
var file:File = File.documentsDirectory.resolvePath("data/"+photoNameVec[loadCounter]);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var w:int = fileStream.readInt();
var h:int = fileStream.readInt();
var fileContent:ByteArray = new ByteArray;
fileStream.readBytes(fileContent);
fileStream.close();
var bmpData:BitmapData = new BitmapData( w , h);
bmpData.setPixels(bmpData.rect, fileContent);
addChild(new Bitmap(bmpData));

Resources