Dart
June 11, 2008, 7:34pm
1
Hi,
I have a little question, How can i access childs of objects in AS 3.0.
MovieClip _shape has 4 childs block1 … block4
I used to be using this in AS 2.0, but it dosent work in AS 3.0 anymore, it says that it is undefined.
for(var i = 1; i < 5; i++){
doSomething(_shape["block"+i]);
}
Please, help me :]
thanks.
Fidodo
June 11, 2008, 7:39pm
2
put each movie in an array and access it that way instead.
that syntax is still correct as i just used this:
mapMc[‘c’ + i]
on an AS3 project yesterday.
check to see if it’s even getting to _shape
*edit: added c0, c1, c2, etc…at author time. i think there is a getChildByName() if above doesn’t work.
Dart
June 12, 2008, 3:27pm
4
Thank you for your replies, there is whole class:
package {
import flash.display.MovieClip;
import flash.utils.getDefinitionByName;
public class Tetris_Shape extends MovieClip {
private var _shape:MovieClip = new MovieClip();
private var _xdist:int;
private var _ydist:int;
private var _shapeNum:uint;
private var _posArray:Array = new Array(
[[_block_size, 3*_block_size],
[_block_size,_block_size],
[_block_size, -_block_size],
[_block_size, 3*-_block_size]],
[[-_block_size, _block_size],
[_block_size, _block_size],
[_block_size, -_block_size],
[_block_size, 3*-_block_size]]
);
private var _rotation:uint = 0;
private var _block_size = Block.SIZE/2;
public function Tetris_Shape (type:String, xdist:int, ydist:int):void {
addChild(_shape);
_xdist = xdist;
_ydist = ydist;
switch(type){
case "I": doShape(1); break;
case "J": doShape(2); break;
case "L": doShape(3); break;
case "O": doShape(4); break;
case "S": doShape(5); break;
case "T": doShape(6); break;
case "Z": doShape(7); break;
}
}
public function rotate ():void {
for(var i = 1; i < 5; i++){
trace(_shape["block"+i]); //undefined
trace(_shape.block1); //undefined
trace(_shape.getChildAt(i-1)); //[object Block]
trace(_shape.getChildByName("block"+i)); //null
}
}
private function doShape (shapeNum:uint):void {
_shapeNum = shapeNum;
for(var i = 1; i < 5; i++){
var block = "block"+i;
block = new Block (shapeNum,_posArray[shapeNum-1][i-1][0],_posArray[shapeNum-1][i-1][1]);
recalculatePositions(block, Math.atan2(_posArray[shapeNum-1][i-1][1], _posArray[shapeNum-1][i-1][0]), shapeNum, i);
_shape.addChild(block);
trace(block); //[object Block]
trace(_shape.getChildByName(block)); //null
}
}
private function recalculatePositions (block:MovieClip, newAngle:Number, shape:uint, blocknum:uint):void {
var pyth:Number = pythagor_ab(_posArray[shape-1][blocknum-1][0], _posArray[shape-1][blocknum-1][1])
block.x = Math.cos(block._angle + newAngle) * pyth+_xdist;
block.y = Math.sin(block._angle + newAngle) * pyth+_ydist;
block._angle = newAngle;
}
public static function getSymbolClone(symbolName:String, i:Number):*
{
return duplicate(symbolName, i);
function duplicate (className:String, i:Number):*
{
var currentClass:Class = getDefinitionByName(className+i) as Class;
return new currentClass();
}
}
private function pythagor_ab(a:Number, b:Number):Number {
return Math.sqrt(a*a+b*b);
}
}
}
and thi is in fla. file
var _currentBlock:Tetris_Shape = new Tetris_Shape("I",0,0);
addChild(_currentBlock);
_currentBlock.rotate();
I really dont know what is wrong I:-)
Fidodo
June 12, 2008, 4:16pm
5
[quote=SmoothDime;2341152]that syntax is still correct as i just used this:
mapMc[‘c’ + i]
on an AS3 project yesterday.
check to see if it’s even getting to _shape
*edit: added c0, c1, c2, etc…at author time. i think there is a getChildByName() if above doesn’t work.[/quote]
The syntax is correct, but avoid doing this because when you reference a variable by using a string like this the compiler can’t check it and you’re setting yourself up for an obscure run time error.
Dart
June 13, 2008, 9:56am
6
Thank you guys very much.