引用存储在数组中的精灵/对象



通过push()将对象添加到数组后,如何引用它的xy值?例如:

var Test:Sprite = new Sprite();         
Test.graphics.beginFill(0x000000);
Test.graphics.drawRect(0,0,10,10);
testHolder.addChildAt(genericBlock,0);

可以通过以下方式访问存储在数组中的任何对象的x值和y值:

var myArray = new Array();
myArray.push(testSprite1); // First object 'pushed' into array at position 0
myArray.push(testSprite2); // Second object 'pushed' into array at position 1
myArray[0].x = 50; // testSprite1.x = 50
myArray[1].x = 100; // testSprite2.x = 100;
// To referece..
trace(myArray[0].x, myArray[1].x); // Outputs: 50 100

这是一个很好的教程,用于学习如何实现数组并操作其中保存的所有/单个元素:

http://www.republicofcode.com/tutorials/flash/as3arrays/

最新更新