PDA

View Full Version : communicating from a nested clip to another nested clip



SchmackLab
December 9th, 2008, 04:08 PM
I keep getting "TypeError: Error #1009: Cannot access a property or method of a null object reference." and I don't understand why.

What I am trying to do is to communicate from inside a movieclip nested in the main timeline to another movieclip nested on the main timeline. And I assume that this is why I am getting the error although I do not understand why or how to fix it.

more details and my code:

I have a menu nested in a movieclip on the main timeline that when activated creates a fog over the images in the background. A series of buttons appear that when clicked removed the fog and play individual movieclips nested in the main timeline.

I am trying to create a rollover that will preview the movieclips before you click on them and confirm your choice.


cutlery_btn.addEventListener(MouseEvent.MOUSE_OVER , onRollOverCutlery);

cutlery_btn.addEventListener(MouseEvent.CLICK, onClickCutlery);

function onRollOverCutlery(event:MouseEvent):void
{
MovieClip(root).cutlery_mc.gotoAndStop(10);
}


function onClickCutlery(event:MouseEvent):void
{
MovieClip(root).gotoAndPlay("cutlery");
MovieClip(root).menuTab_mc.visible = false;
MovieClip(root).fog_mc.play();
}



Thanks for your help!

enriquepr
December 9th, 2008, 04:57 PM
You are getting that because of the way AS3 is structured. Since root is being called, it'll just allow you to call methods or properties of it but when it comes to something sitting "on" the root, it won't know what you are referring to. This really helps alot when building a project simply because you shouldn't try to access things that way (in good practice speaking).

However, if you did want to access it anyway you would do the following:

root["movieClipName"].property = value;

Now, you can also apply the same thing for accessing a method or function call from a parent or root by doing the following:

parent["functionName"](); or root["functionName"]();

Once again, I don't recommend this because it is very static and doesn't follow general good practice. What I would do in your case is set an EventListener on the stage that listens to the MovieClip (which you should technically be assigning the class for it in the Library via linkage in a perfect world). Inside the MovieClip code you would have the onRollOverCutlery and onClickCutlery dispatch events that are then reacted to by the stage. This way you can avoid altogether attempting access of "undefined" or "null" properties/methods. It would look something like so (not the entire thing):

function onClickCutlery(event:MouseEvent):void
{
dispatchEvent(new Event(Event.ACTIVATE));
}

This would just dispatch the Event.ACTIVATE event to the stage. You obviously do not have to use this exact one and can even define your own if you want to write it but for sake of simplicity I'll leave it at that. On the stage you could have an EventListener like the following:

movieClipYouAreDispatchingEventFrom.addEventListen er(Event.ACTIVATE, eventHandlerFunction);

Inside the eventHandlerFunction you could then provide all the logic you have inside the MovieClip like so:

function eventHandlerFunction(event:Event):void {

gotoAndPlay("cutlery");
menuTab_mc.visible = false;
fog_mc.play();

}

Hopefully that helps a little.

SchmackLab
December 9th, 2008, 05:05 PM
Thank you so much for the very detailed explanation. This makes alot more sense now!

I appreciate your time!