Adding Controls Dynamically In ActionScriptand Access The Correct Instance - actionscript-3

My question here is, I'm trying to understand exactly how this piece of code I wrote works. This does what I need, but not sure exactly what is going on behind the scenes. Can any ActionScript Pro's out there help??
So, what I'm doing is adding buttons and datagrids dynamically while iterating through a for loop. The button controls the visibility of the canvas containing a datagrid by setting the visible property to false and removing the canvas from the layout.
If I create a function that adds a button and an event listener that sets the visible properties, the button is associated with the correct canvas. If I do not add the button and the event listener in function, the all the buttons control the last canvas added in the for loop.
Does this have something to do with closure? See code below.
This Works:
for(var j:int = 0; j < resultsArray.length; j++)
{
var dataGridCanvas:Canvas = new Canvas();
dataGridCanvas.top = 5;
var vboxDatagrid:VBox = new VBox();
var vboxButton:VBox = new VBox();
var resultsAttributeDataGrid:DataGrid = new DataGrid();
vboxButton.addChild(addButtonToCanvas(identifyResults[0], dataGridCanvas));
sidePanel.idResults.vbxIdToolIResults.addChild(vboxButton);
var vboxDataGridChild:VBox = new VBox();
vboxDataGridChild.addChild(resultsAttributeDataGrid);
vboxDatagrid.addChild(vboxDataGridChild);
dataGridCanvas.addChild(vboxDatagrid);
idResults.vbxIdToolIResults.addChild(dataGridCanvas);
}
private function addButtonToCanvas(layerName:String, theCanvas:Canvas):VBox
{
var vbox:VBox = new VBox();
var layerButton:spark.components.Button = new spark.components.Button();
layerButton.label = layerName as String;
layerButton.addEventListener(MouseEvent.CLICK,
function onClick():void{
theCanvas.visible == true ? theCanvas.visible = false : theCanvas.visible = true;
theCanvas.visible == false ? theCanvas.includeInLayout = false : theCanvas.includeInLayout = true
});
vbox.addChild(layerButton);
return vbox;
}
This does not work, but is doing the same thing - adding a button and it's event listener to a vbox:
for(var j:int = 0; j < resultsArray.length; j++)
{
var dataGridCanvas:Canvas = new Canvas();
dataGridCanvas.top = 5;
var vboxDatagrid:VBox = new VBox();
var vboxButton:VBox = new VBox();
var resultsAttributeDataGrid:DataGrid = new DataGrid();
var vbox:VBox = new VBox();
var layerButton:spark.components.Button = new spark.components.Button();
layerButton.label = layerName as String;
layerButton.addEventListener(MouseEvent.CLICK,
function onClick():void{
dataGridCanvas.visible == true ? dataGridCanvas.visible = false : dataGridCanvas.visible = true;
dataGridCanvas.visible == false ? dataGridCanvas.includeInLayout = false : dataGridCanvas.includeInLayout = true
});
vbox.addChild(layerButton);
sidePanel.idResults.vbxIdToolIResults.addChild(vbox);
var vboxDataGridChild:VBox = new VBox();
vboxDataGridChild.addChild(resultsAttributeDataGrid);
vboxDatagrid.addChild(vboxDataGridChild);
dataGridCanvas.addChild(vboxDatagrid);
idResults.vbxIdToolIResults.addChild(dataGridCanvas);
}

It has everything to do with closures.
When you pass the Canvas to addButtonToCanvas in the first example, the closure keeps it associated with the onClick handler.

Related

Multiple sprites added in loop, but only last sprite clickable

I have multiple bitmap images added into sprites(each image added into 1 sprite) in a loop, then all the sprites added to 1 _contentHolder(Sprite) then that is added to a viewport.
What the problem is, the multiple sprites that are added inside the loop, everything displays with no problem but only the last sprite added is clickable. None of the sprite added before it is clickable. Wondering what the problem is, they are not overlapping and when i hover the mouse over the top of all the sprites, it turns into the mouse clicker but it just won't click.
Thanks for your time!
My code:
function onImageLoaded(e:Event):void {
loadedArray.push(e.target.content as Bitmap);
for(var i:int = 0; i < loadedArray.length; i++){
var currentY1:int = 200;
var image: Sprite= new Sprite;
e.currentTarget.loader.content.height =200;
e.currentTarget.loader.content.y += currentY1;
image.mouseChildren = true; // ignore children mouseEvents
image.mouseEnabled = true; // enable mouse on the object - normally set to true by default
image.useHandCursor = true; // add hand cursor on mouse over
image.buttonMode = true;
image.addChild(loadedArray[i]);
_contentHolder.addChild(image);
}
newArray.push(image);
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
image.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
Edit:
function gotoscene(e: MouseEvent):void{
var index:Number;
index = newArray.indexOf(e.target);
trace(index);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect( -1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
var originalBitmap : BitmapData = loadedArray[index].bitmapData;
var duplicate:Bitmap = new Bitmap(originalBitmap);
duplicate.width = stage.width;
_contentHolder1.addChild(duplicate);
// Use counter here to only add _contentHolder1 once
//Assuming that `samedata` is a class member (I can't see the rest of your code)
addChild(_contentHolder1);
}
Edit2:
private var image:Array = new Array;
//In the For loop
image[i] = new Sprite();
image[i].addChild(loadedArray[i]);
image[i].addEventListener(MouseEvent.CLICK, gotoscene);
function gotoscene(e:MouseEvent):void{
index = image.indexOf(e.target);
trace(index);
}
You should move image.addEventListener(MouseEvent.CLICK, gotoscene); statement into the loop where you add child sprites. Once you do, the listener will be added to all of the sprites, not just the last one that's currently stored in image variable, and is the only one that responds to your clicks.
for(var i:int = 0; i < loadedArray.length; i++){
var currentY1:int = 200;
var image: Sprite= new Sprite;
e.currentTarget.loader.content.height =200;
e.currentTarget.loader.content.y += currentY1;
image.mouseChildren = true; // ignore children mouseEvents
image.mouseEnabled = true; // enable mouse on the object - normally set to true by default
image.useHandCursor = true; // add hand cursor on mouse over
image.buttonMode = true;
image.addEventListener(MouseEvent.CLICK, gotoscene); // <-- THIS
image.addChild(loadedArray[i]);
_contentHolder.addChild(image);
}
And for all that is holy, learn to indent your code, so that you will be able to visually find the start and end of your loops and see if a certain statement is within the loop or not.
I did work for a few years in AS3 and this was a weird an usual problem. I used to solve it with a function that adds the event to each clip:
function someFunction():void {
for (...) {
var image:Sprite = new Sprite();
addSceneListener(image);
}
}
function addSceneListener(mc:Sprite):void {
mc.addEventListener(MouseEvent.CLICK, gogoscene);
}

not currently clicked movieclip comes up as true

private var previousClick = null;
public static var floor1:Array = new Array();
floor1[0] = [2,1,1,1,1,1,2];
floor1[1] = [1,1,1,1,1,1,1];
floor1[2] = [1,1,1,2,1,1,1];
floor1[3] = [1,1,1,1,1,1,1];
floor1[4] = [1,1,1,2,1,1,1];
floor1[5] = [1,1,1,1,1,1,1];
floor1[6] = [2,1,1,1,1,1,2];
public function Main()
{
var tilew:int = 60;
var tileh:int = 60;
for (var i:int=0; i<floor1.length; i++)
{
for (var u:int=0; u<floor1[i].length; u++)
{
var cell:MovieClip = new Tile();
cell.gotoAndStop(floor1[i][u]);
cell.x = ((u-i)*tileh)+365;
cell.y = ((u+i)*tileh/2)+70;
addChild(cell);
cell.addEventListener(MouseEvent.CLICK, mouseclick);
}
}
function mouseclick(event:MouseEvent)
{
(event.currentTarget as Tile).outline.gotoAndStop("active");
event.currentTarget.cellSelected = true;
event.currentTarget.removeEventListener(MouseEvent.CLICK,mouseclick);
if (previousClick !== null){
(previousClick as Tile).outline.gotoAndStop(1);
event.currentTarget.cellSelected = false;
previousClick.addEventListener(MouseEvent.CLICK,mouseclick);
trace(previousClick);
}
previousClick = event.currentTarget;
previousClick.cellSelected = true;
}
So I have tiles called cells lined up to each other to make a floor. When you click a cell, the outline of that tile glows, indicating that it is currently selected. I have also created a boolean that checks if the current tile is selected. The problem is, the boolean is returning true to a tile that I clicked that isn't the currently selected one, especially the first tile I click when the game starts.
So my question is, how can I get a tile that I clicked that isnt currently selected to not equal true?
I think it caused by overlap.
you may try cell.y = ((u+i)*tileh)+70;

event listener won't work second time around AS3

I'm stuck! If I need to go into more detail please just leave a comment and I will elaborate.
I've tried to create some code that dynamically adds linked objects to the stage and then removes then when dropped in the correct area, which in turn creates the next and so on. The information is in an array and it cycles through until the game is complete and the targetScore is met. If a timer runs out the games stops, and the win() or lose() functions are called and a retry button is displayed. This works fine until the game has stopped and I try to restart the program using the retry() function. The retry() function attempts to reset everything as it was when the program started, by creating the baseball object again and then letting the releaseToDrop() repeat everything as it did the first time. Depending on where I have stopped, when I get around to the same place a second time, the clicktoDrag1() function fails to pick up the current object. It could be on any 1 of the 8 objects that are created dynamically from the library. When the stage hears the listener on *mouse_up* I can click and drag the object but then it kind of falls apart as it goes slightly out of synch with the arrays (which were reset in the retry() function), the target which it's dropped on doesn't recognise it even though all the trace statement read as they should. I know this is a lot to look though, and I'm not sure if this can be solved via the forum, any general tips would be appreciated though.
I normally keep the code simple, but I want to advance and make the code write itself, which seems to work until things get too complicated for me.
I've never posted for help before, but I've given this everything, if it can't be fixed I'll have to start again and simplify.
Thanks in advance if anyone takes the time to look at this, I would be humbled - and would love to use this forum more to become a better coder.
It's all on the timeline, here's the code.
import flash.display.MovieClip;
import flash.ui.Mouse;
var startPosX = 450;
var startPosY = 400;
//setup first clip
var baseball:MovieClip = new Baseball();
baseball.name = "baseball";
addChild(baseball);
baseball.buttonMode = true;
baseball.x = startPosX;
baseball.y = startPosY;
activity_txt.text = "Swinging a baseball bat";
//setup small clips
baseballSmall.visible = false;
golfSmall.visible = false;
swimSmall.visible = false;
boxingSmall.visible = false;
tennisSmall.visible = false;
dartsSmall.visible = false;
powerSmall.visible = false;
marathonSmall.visible = false;
theEnd.visible = false;
retry_btn.visible = false;
fast1.visible = false;
fast2.visible = false;
fast3.visible = false;
fast4.visible = false;
slow1.visible = false;
slow2.visible = false;
slow3.visible = false;
slow4.visible = false;
//setup vars
var counter:int = 0;
var sportCounter:int = 0;
var startingLife:int = 15;
var playerLife = startingLife;
var lifeBoost:int = 3;
var targetScore:int = 8;
var countdownTimer:Timer = new Timer(500,0);
var questionTimer:Timer = new Timer(250,2);
var score:int = 0;
var smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");
var arraySmall:Array = smallArray;
var arrayLet:Array = letArray;
var arrayActivity:Array = activityTXTArray;
//var draggable = getChildByName(sportArray[0]);
//setup bonus bar
bonusBar.gotoAndStop(2);
bonusBar.x = timeBar.x;
bonusBar.y = timeBar.y - timeBar.height;
bonusBar.height = timeBar.height/startingLife * lifeBoost;
playerLife = startingLife;
timeBar.height = playerLife * (300/startingLife);
/* listeners */
countdownTimer.addEventListener(TimerEvent.TIMER, timerTick);
countdownTimer.start();
retry_btn.addEventListener(MouseEvent.CLICK, retry);
baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function clickToDrag1(event:MouseEvent):void
{
var draggable = getChildByName(sportArray[sportCounter]);
trace("the counter name is "+ sportArray[sportCounter]);
trace("the baseball name is "+ baseball.name);
trace("the draggable name is "+ draggable.name);
trace("the answer array is "+ answersArray[counter]);
trace("the sport array is "+ sportArray[sportCounter]);
this.setChildIndex(draggable,this.numChildren-1);
draggable.startDrag();
}
function releaseToDrop(event:MouseEvent):void
{
//get current obj name from sportArray
var draggable = getChildByName(sportArray[sportCounter]);
//check if this obj is dropped on the Fast or Slow MovieClip
if(draggable.hitTestObject(getChildByName(answersArray[counter])))
{
//move on to the next F/S answer
counter++;
score++;
var tick = new Tick();
addChild(tick);
tick.x = 370;
tick.y = 200;
activity_txt.text = activityTXTArray.shift();
playerLife += lifeBoost;
timeBar.height = playerLife * (300/startingLife);
bonusBar.gotoAndPlay(2);
var smallName = getChildByName(smallArray.shift());
smallName.visible = true;
var letters = getChildByName(letArray.shift());
letters.visible = true;
//remove the drag listenter on the current object (name assigned via sportArray)
draggable.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
//remove the current object
removeChild(getChildByName(sportArray[sportCounter]));
//delete ref
var deleteRef = getChildByName(sportArray[sportCounter]);
deleteRef = null;
//move on to the next one
sportCounter++;
//add a new object
var obj:Class = getDefinitionByName(sportArray[sportCounter]) as Class;
var myMclip = new obj();
//name it
myMclip.name = sportArray[sportCounter];
//var clipName = getChildByName(sportArray[0]);
myMclip.x = myMclip.y = 400;
myMclip.buttonMode = true;
trace("myClip name "+myMclip.name);
addChild(myMclip);
//add listener to new obj (is this removed via draggable?)
myMclip.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
else{
draggable.x = startPosX;
draggable.y = startPosY;
draggable.stopDrag();
var cross = new Cross();
addChild(cross);
cross.x = 370;
cross.y = 200
}
}
function timerTick(e:TimerEvent):void {
//removes from 40(life) every half a second
playerLife -= 1;
//bar height = % of whats left of life
timeBar.height = playerLife * (300/startingLife);
bonusBar.y = timeBar.y - timeBar.height;
if(playerLife == 0) {
loseGame();
} else if(playerLife>0 && score > targetScore-1) {
winGame();
}
}
function loseGame():void
{
var removeCurrent = getChildByName(sportArray[sportCounter]);
removeCurrent.visible = false;
hideStuff();
theEnd.visible = true;
theEnd.end_txt.text = "sorry you lost"
retry_btn.visible = true;
//baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
//baseball = null;
trace(baseball);
}
function winGame():void
{
var removeCurrent = getChildByName(sportArray[sportCounter]);
removeCurrent.visible = false;
hideStuff();
theEnd.visible = true;
theEnd.end_txt.text = "You've won!"
retry_btn.visible = true;
baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
function retry(e:MouseEvent):void
{
playerLife = startingLife;
timeBar.height = playerLife * (300/startingLife);
score = 0;
counter = 0;
sportCounter = 0;
countdownTimer.reset();
countdownTimer.start();
var baseball:MovieClip = new Baseball();
baseball.name = "baseball";
trace("the type is "+baseball);
trace("the name is " + baseball.name);
addChild(baseball);
baseball.buttonMode = true;
baseball.x = startPosX;
baseball.y = startPosY;
baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
activity_txt.text = "Swinging a baseball bat";
smallArray = arraySmall;
letArray = arrayLet;
activityTXTArray = arrayActivity;
retry_btn.visible = false;
theEnd.visible = false;
showStuff();
smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");
}
function showStuff():void
{
activity_txt.visible = true;
fast.visible = true;
slow.visible = true;
timeBar.visible = true;
bonusBar.visible = true;
}
function hideStuff():void
{
fast1.visible = false;
fast2.visible = false;
fast3.visible = false;
fast4.visible = false;
slow1.visible = false;
slow2.visible = false;
baseballSmall.visible = false;
golfSmall.visible = false;
swimSmall.visible = false;
boxingSmall.visible = false;
tennisSmall.visible = false;
dartsSmall.visible = false;
activity_txt.visible = false;
fast.visible = false;
slow.visible = false;
timeBar.visible = false;
bonusBar.visible = false;
}
The OP has solved the problem, as is indicated in this comment:
I hadn't removed the object on game end, I had hidden it so when it came around again a duplicate was created. I've removed it instead of hiding and it works fine. Having two clips with on the stage created some strange drag and drop behavior which sort of gave it away. "This is not 'my' answer. MAngoPop answered this in the comments but forgot to add it as an answer."

AS3 removing dynamically created child movieclips

I'm fairly new to AS3. Anyways, I'm try to remove a dynamically created child movieclip when clicked on. When a dirt block is clicked on, which is a child movieclip of 'world' I want to remove it.
I've tried various ways of removing it using removeChild. I've also tried moving the function inside/outside of the for loop that creates the movieclips.
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
function removeBlock(event:Event){
world.removeChild(getChildByName(event.target.name));
}
}
Thanks for the help.
Try this
function removeBlock(event:Event){
world.removeChild(event.currentTarget as DisplayObject);
}
No function definition should be inside a for. I changed that in your code and rewrited a little below:
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
}
function removeBlock(event:MouseEvent){
trace("Is click really working? This target name is " + event.currentTarget.name);
world.removeChild(event.currentTarget));
}

Closure problem? - passing current value of a variable

I'm trying to pass the current value of a variable when an a dynamically generated navigation 'node' is clicked. This needs to just be an integer, but it always results in the last node's value.. have tried some different methods to pass the value, a custom event listener, a setter, but I suspect it's a closure problem.. help would be appreciated ;-)
function callGrid():void {
for (var i:Number = 0; i < my_total; i++) {
var gridnode_url = my_grid[i].#gridnode;
var news_category= my_grid[i].#category;
var newstitle = my_grid[i].#newstitle;
var news_content = my_grid[i]..news_content;
var news_image = my_grid[i]..news_image;
var gridnode_loader = new Loader();
container_mc.addChild(gridnode_loader);
container_mc.mouseChildren = false;
gridnode_loader.load(new URLRequest(gridnode_url));
gridnode_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gridLoaded);
gridnode_loader.name = i;
text_container_mc = new MovieClip();
text_container_mc.x = 0;
text_container_mc.mouseEnabled = false;
var textY = text_container_mc.y = (my_gridnode_height+18)*y_counter;
addChild(text_container_mc);
var tf:TextSplash=new TextSplash(newstitle,10,0,4 );
container_mc.addChild(tf);
tf.mouseEnabled = false;
tf.height = my_gridnode_height;
text_container_mc.addChild(tf);
var text_container_mc_tween = new Tween(text_container_mc, "alpha", Strong.easeIn, 0,1,0.1, true);
gridnode_loader.x = (my_gridnode_width+5) * x_counter;
gridnode_loader.y = (my_gridnode_height+15) * y_counter;
if (x_counter+1 < columns) {
x_counter++;
} else {
x_counter = 0;
y_counter++;
}
}
}
function gridLoaded(e:Event):void {
var i:uint;
var my_gridnode:Loader = Loader(e.target.loader);
container_mc.addChild(my_gridnode);
_xmlnewstarget = my_gridnode.name;
//||||||||||||||||||||||||||||||||||||||||
//when a particular grid node is clicked I need to send the current _xmlnewstarget value to the LoadNewsContent function...
//||||||||||||| ||||||||||||||||||||||||
my_tweens[Number(my_gridnode.name)]=new Tween(my_gridnode, "alpha", Strong.easeIn, 0,1,0.1, true);
my_gridnode.contentLoaderInfo.removeEventListener(Event.COMPLETE, gridLoaded);
my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
}
function loadNewsContent(e:MouseEvent):void {
createNewsContainer();
getXMLNewsTarget();
news_category = my_grid[_xmlnewstarget].#category;
var tfnews_category:TextSplash=new TextSplash(news_category,20,16,32,false,false,0xffffff );
tfnews_category.mouseEnabled = false;
newstitle = my_grid[_xmlnewstarget].#newstitle;
var tftitle:TextSplash=new TextSplash(newstitle,20,70,24,false,false,0x333333 );
news_container_mc.addChild(tftitle);
tftitle.mouseEnabled = false;
news_content = my_grid[_xmlnewstarget]..news_content;
var tfnews_content:TextSplash=new TextSplash(news_content,20,110,20,true,true,0x333333,330);
news_container_mc.addChild(tfnews_content);
tfnews_content.mouseEnabled = false;
news_image = my_grid[_xmlnewstarget].#news_image;
loadNewsImage();
addChild(tfnews_category);
addChild(tftitle);
addChild(tfnews_content);
var news_container_mc_tween = new Tween(news_container_mc, "alpha", Strong.easeIn, 0,1,0.3, true);
news_container_mc_tween.addEventListener(Event.INIT, newsContentLoaded);
}
I'm not going to try to read your code (try to work on your formatting, even if it's just indenting), but I'll provide a simplified example:
for (var i = 0; i < my_total; i++) {
var closure = function() {
// use i here
}
}
As you say, when closure is called it will contain the last value of i (which in this case would be my_total). Do this instead:
for (var i = 0; i < my_total; i++) {
(function(i) {
var closure = function() {
// use i here
}
})(i);
}
This creates another function inside the loop which "captures" the current value of i so that your closure can refer to that value.
See also How does the (function() {})() construct work and why do people use it? for further similar examples.
Umm, as mentioned above, the code is a bit dense, but I think you might have a bit of type conversion problem between string and integers, is the "last value" always 0? try making these changes and let me know how you get on.
// replace this gridnode_loader.name = i;
gridnode_loader.name = i.toString();
// explictly type this as an int
_xmlnewstarget = parseInt(my_gridnode.name);
// replace this: my_tweens[Number(my_gridnode.name)] = new Tween(......
my_tweens[parseInt(my_gridnode.name)] = new Tween();
Oh and I think it goes without saying that you should massively refactor this code block once you've got it working.
Edit: after further study I think you need this
//replace this: my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
var anonHandler:Function = function(e:MouseEvent):void
{
loadNewsContent(_xmlnewstarget);
};
my_gridnode.addEventListener(MouseEvent.CLICK, anonHandler);
Where your loadNewsContent has changed arguements from (e:MouseEvent) to (id:String)
Firstly, you do not need to call addChild for the same loader twice (once in callGrid) and then in (gridLoaded). Then you can try putting inside loadNewsContent: news_category = my_grid[int(e.target.name)].#category;instead of news_category = my_grid[_xmlnewstarget].#category; As _xmlnewstarget seems to be bigger scope, which is why it is getting updated every time a load operation completes.