Robdogga55
Robdogga55

Reputation: 309

AS3 Accessing a movieclip that is inside of another movie clip and a child

I hope the title isn't too confusing. I know you are able to access a movieclip that is inside another movieclip, like I had done;

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

but I have now changed it so that inside of the movieclip caveman there is another movieclip cavemanmenu that is AS Linkage btncavemanMenu and inside of cavemanmenu is btn_CollectResources2 so I tried the following;

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

and I get this error;

TypeError: Error #1009: Cannot access a property or method of a null object reference. at stoneApp_new_fla::MainTimeline/allowResourceCollection()[stoneApp_new_fla.MainTimeline::frame1:104]

I'm so confused at what is going on. All I am trying to do is by clicking on caveman, cavemanmenu pops up and I am able to tap on btn_CollectResources2 which is nested inside of cavemanmenu. Here is all the code in the section so you understand;

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")
    }
}

As you can see, I used to have it so it would just change the frame in the movieclip caveman but because I need to create multiple caveman then I need to be able to have the menu pop up below them wherever they are and creating a child and bringing it in like that was the only way I could find. Thanks.

Upvotes: 0

Views: 171

Answers (1)

Florian Salihovic
Florian Salihovic

Reputation: 3951

The event bubbles so the depth of the dispatching MovieClip does not matter. In fact, it is easier to let them be handled by caveman.

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

I highly recommend following naming conventions ...

Upvotes: 1

Related Questions