开发者

In actionscript 3, how do i address objects that aren't on frame 1?

I have an object (button) on frame 2 of my timeline, with the name btnMenu. When clicked, i want to return to frame 1 in the timeline. Actionscript 3 does not allow me to bind the eventlistener to the button from my code on frame 1.

Layout:

layer 1: Actions, only on frame 1 layer 2: btnMenu, only on frame 2 (with an empty frame in front)

Code:

stop(); // don't automatically go to frame 2

btnMenu.addEventListener(MouseEvent.CLICK, function() { gotoAndStop(1); }

(and other code to go to frame 2 obviously)

The error i get 开发者_如何学JAVAis "can't find method/property of object that is null" (roughly translated).

Please help?


You can only access objects that are on the same frame where your code lives. You have several ways to get around this. Depends how you want to structure you application.

  1. You can move all your buttons to the frame where you have the code, toggle their visibility to false and once you reach the frame where the button should be visible just set it to true.

  2. Other way is to move the addEventListener code to the same frame of your button. You can still access your code in the first frame, and call function if you need so.

Consider the following:

// code in first frame
stop();

function goto(evt:MouseEvent):void {
    gotoAndStop(1);
}

// code in button frame
stop();
btnMenu.addEventListener(MouseEvent.CLICK, goto);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜