AS3访问另一个影片剪辑和子项内的动画片段

时间:2014-07-04 16:24:37

标签: actionscript-3 flash

我希望这个标题不会让人感到困惑。 我知道你能够像我一样访问另一个动画片段中的动画片段;

function allowResourceCollection():void {
    caveman.btn_CollectResources2.addEventListener(TouchEvent.TOUCH_TAP, checkResourceCollection);
}

但我现在更改了它,以便在movieclip caveman 内部有另一个动画片段 cavemanmenu ,即AS Linkage btncavemanMenu cavemanmenu btn_CollectResources2 所以我尝试了以下内容;

function allowResourceCollection():void {
    caveman.cavemanmenu.btn_CollectResources2.addEventListener(TouchEvent.TOUCH_TAP, checkResourceCollection);
}

我收到此错误;

  

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在   stoneApp_new_fla :: MainTimeline / allowResourceCollection()[stoneApp_new_fla.MainTimeline ::帧1:104]

我对发生的事情感到很困惑。我所要做的就是点击穴居人 cavemanmenu 弹出,我可以点击 btn_CollectResources2 ,它嵌套在 cavemanmenu 即可。以下是本节中的所有代码,以便您理解;

var myMovieClip:MovieClip = new btncavemanMenu();
//Caveman Menu
function allowTapCaveman():void {
    caveman.addEventListener(TouchEvent.TOUCH_TAP, cavemanMenu);
}
function cancelTapCaveman():void {
    caveman.removeEventListener(TouchEvent.TOUCH_TAP, cavemanMenu);
}
function cavemanMenu(event:TouchEvent):void {
    addChild(myMovieClip);
    myMovieClip.x = caveman.x;
    myMovieClip.y = caveman.y;
    //myMovieClip.addEventListener(TouchEvent.TOUCH_TAP, cavemanMenu);
    //caveman.gotoAndStop(2);
    //trace('2');
    if (caveman.currentFrame == 2){
        cancelTapCaveman();
        allowTapCavemanClose();
    }
}
function allowTapCavemanClose():void {
    caveman.addEventListener(TouchEvent.TOUCH_TAP, cavemanMenuClose);
}
function cancelTapCavemanClose():void {
    caveman.removeEventListener(TouchEvent.TOUCH_TAP, cavemanMenuClose);
}
function cavemanMenuClose(event:TouchEvent):void {
    cancelTapCavemanClose();
    removeChild(myMovieClip);
    //caveman.gotoAndStop(1);
    allowTapCaveman();
    //trace('1');
}
function allowResourceCollection():void {
    caveman.btncavemanMenu.btn_CollectResources2.addEventListener(TouchEvent.TOUCH_TAP, checkResourceCollection);
}
function cancelResourceCollection():void {
    caveman.btn_CollectResources2.removeEventListener(TouchEvent.TOUCH_TAP, checkResourceCollection);
}
function checkResourceCollection(event:TouchEvent):void {
    if(remaningActions >= 1){
        spawnWood();
        spawnFood();
        spawnStone();
        remaningActions -= 1;
        updateTextBox();
    }
    if(remaningActions <= 0){
        trace("not enough actions")
    }
}

正如你所看到的,我曾经拥有它,所以它只会改变动画片段穴居人中的帧,但因为我需要创建多个穴居人然后我需要能够让菜单弹出它们下面的任何地方,并创建一个孩子并将其带入,就像我能找到的唯一方法一样。感谢。

1 个答案:

答案 0 :(得分:1)

事件起泡,因此调度MovieClip的深度无关紧要。事实上,让caveman处理它们更容易。

function allowResourceCollection():void {
  caveman.addEventListener(TouchEvent.TOUCH_TAP, checkResourceCollection);
}

我强烈推荐遵循命名约定......