Adding addEventListener too a Child object
Hey guys,
I am programming a “build a tower” game.
After removing the event listener from the first movieclip, flash won’t add the event listener to the by as3 to the stage attached new movieclip.
Error MSG:
TypeError: Error #1010: A term is undefined and has no properties.
at stackers02_fla::MainTimeline/keyDownHandler()
//Vars
var $newObjectName:String;
var $direction:String;
var $level:int;
var $currObjPosX:int;
var $currObjPosY:int;
$newObjectName = "new OBJ";
$direction="goRight";
$level=1;
//Event Listeners
this["mcBlockContainer"+$level].addEventListener(Event.ENTER_FRAME, mcBlockContainerMoveF);
trace(this["mcBlockContainer"+$level]);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
//Functions
function keyDownHandler(event : KeyboardEvent):void {
if (event.keyCode==Keyboard.SPACE) {
$currObjPosX = this["mcBlockContainer"+$level].x;
$currObjPosY = this["mcBlockContainer"+$level].y;
this["mcBlockContainer"+$level].removeEventListener(Event.ENTER_FRAME, mcBlockContainerMoveF);
$level++;
$newObject = this["mcBlockContainer"+$level];
$newObjectName = "mcBlockContainer"+$level;
var $newObject:asBlockContainer = new asBlockContainer;//
addChild($newObject);
$newObject.y = $currObjPosY - ($newObject.height + 2);
$newObject.x = $currObjPosX;
trace($newObjectName);
trace($newObject.name);
$newObject.name = $newObjectName;
trace($newObject.name);
//PROBLEM!!! //PROBLEM!!! //PROBLEM!!! //PROBLEM!!! //PROBLEM!!! //PROBLEM!!!
this["mcBlockContainer"+$level].addEventListener(Event.ENTER_FRAME, mcBlockContainerMoveF);
trace("Space pressed");
}
}
//Enterframe Script
function mcBlockContainerMoveF(e:Event) {
if (this["mcBlockContainer"+$level].x>=550) {
$direction="goLeft";
} else if (this["mcBlockContainer"+$level].x <= 0) {
$direction="goRight";
}
if ($direction=="goRight") {
this["mcBlockContainer"+$level].x=this["mcBlockContainer"+$level].x+78;
} else if ($direction == "goLeft") {
this["mcBlockContainer"+$level].x=this["mcBlockContainer"+$level].x-78;
}
}//END: mcBlockContainerMoveF
any ideas?
Kind Regards
Marcel