PDA

View Full Version : Tween a nested Movieclip = error


pollockd
07-02-2009, 02:22 AM
Hi, I am relatively new to actionscript 3.0. I am trying to tween a Movieclip which is a menu that has 3 simple buttons in it. Each simple button has a movieclip on the three states (so i can apply filters to them).

The project is a simple paper scissors rock game and the menu has the paper scissors rock buttons for the user.

When I have the tween code and Movieclip in the first frame of the movie they tween perfectly so I know the code is correct there.
BUT, When I move all that to frame 2 so that I can have an intro screen to the game and a button which does a simple gotoAndStop(2) to the game screen I get a "cannot access a property or method of a null object reference"

If I remove the button symbols from the Menu movieclip it will tween (even on frame 2) but add them in and it fails. Do I have to initialise the buttons somewhere????

Can anyone tell me what I'm doing wrong? I have been at this for hours.

Is it something to do with the nested MC or have I not initialised something?
Here is the code from the dummy test app I've been trying to work.

frame 1:

import fl.transitions.Tween;
import fl.transitions.easing.None;

stop();
thing.addEventListener(MouseEvent.CLICK,stuff);

function stuff(event:MouseEvent):void
{
gotoAndStop(15);

}

Frame 2


var boxTween:Tween = new Tween(box,"x",None.easeNone,400,50,1,true);
boxTween.start();

Mazoonist
07-02-2009, 02:50 AM
I don't see how you can expect anybody to help you without having a file to test. Can you upload your fla file?

pollockd
07-02-2009, 04:10 AM
Sorry, I was in a rush and forgot. The file is now posted in a zip in original post. It is only a dummy test file, but it has the menu MovieClip with nested buttons in it.

Any help would be appreciated

Mazoonist
07-02-2009, 05:57 AM
The problem isn't being caused by whatever is nested inside your clip.

The problem is that when you say:
gotoAndStop(15);
The playhead moves to frame 15, and the code there executes before the frame is rendered. Since the code refers to an object (box) that isn't rendered yet, you get a null object reference error.

I solved it by moving the keyframe containing the box object to the the previous frame, putting a stop() command on frame 15, and then changing the command on frame 1 to this:
gotoAndPlay(14);
The reason is that if you still say gotoAndStop(15), the frames in between get skipped. By telling it to gotoAndPlay(14), and then putting a stop() command on frame 15, you make sure that the playhead has played through frame 14 and the timeline is "aware" of the existance of the box object by the time any code can refer to it. I've attached the working file.

To avoid these kinds of errors, though, it's better to control visibility by using an object's visible property. In this scenario, instead of having the object present on some frames and not others, the object is present on all frames and is hidden on the frames of your choice by actions you put there (in their own keyframes, of course) like:
box.visible = false;

That way, the object is considered "there" in a coding sense, but not "there" as far as the user is concerned, because it's invisible, and you will never get null object reference errors and you will save yourself some headaches from playing games with getting the playhead to recognize objects before the code refers to them.

Here's the revised file, I uploaded it to my site for you: Untitled-3.fla (http://www.theflashconnection.com/sites/default/files/flash/helpful_uploads/Untitled-3.fla)

Mazoonist
07-02-2009, 06:25 AM
Here's the file again, reworked in the way I described in my previous post. I called it "new_approach.fla" because in this new approach, all objects are present on all frames, so no code can cause null object errors. Instead, each object's visible property is manipulated. in this scenario, now it IS possible to say gotoAndStop(15). Also, your actions should always be on their own layer, just for the sake of staying organized.

new_approach.fla (http://www.theflashconnection.com/sites/default/files/flash/helpful_uploads/new_approach.fla)

pollockd
07-02-2009, 11:26 AM
Thanks heaps for your time, understanding the workings of Flash (as far as rendering etc) is something I'm going to look into more.
Thanks again