-1

See pics attached: I have a button(expnd_btn) inside a movieClip(bannerContent), and trying to add a listener to it so it can call code on the main timeline frame2. frame1 has no clips.

bannerContent.expnd_btn.addEventListener(MouseEvent.CLICK,ShowPanel);

It doesn't work inside the MC... but code works if I remove the button and place it on the main stage and ref like below.

expnd_btn.addEventListener(MouseEvent.CLICK,ShowPanel);

Code entirely(main stage):

bannerContent.expnd_btn.addEventListener(MouseEvent.CLICK,ShowPanel);
function ShowPanel(event:MouseEvent){
//do something
}

I dont get an error indicating the listener cant attach to the btn inside the MC... however the btn isnt finding the function outside.

So question: 1. How do you write this differently so it is somewhat FORCED to work. I tried below but it gives errors

function get Expndbtn():MovieClip{return this.getChildByName("expnd_btn") as MovieClip;}
bannerContent.Expndbtn.addEventListener(MouseEvent.CLICK,ClosePanel);

3. Also how do you write _root.callFuction() or say _global.callFunction() in as3.

code in banner keyframing structure

fhonics
  • 113
  • 9

1 Answers1

1

First, I will advise you to read a bit more about Action Script and how it works.

On each frame, you attach code specific to the particular frame and maybe the frames after that. When talking about nested MovieClips - they have a tree structure. So if you write code in some frame, your code works in that particular MovieClip. this is the current clip. So this.parent is the parent, and this.expnd_btn will get expnd_btn from the current clip..

My point is - write the code where you want, but keep in mind the so called scope - where are you are the moment. If you write code in root - insert the whole path to the button: bannerContent.expnd_btn.addEventListener..; If you write code inside the banner, then you only need expnd_btn.add.., but the function that is called on click is NOT here, so you need to write the path to it: expnd_btn.addEventListener(MouseEvent.CLICK, this.parent.ShowPanel);

Note: this is not needed, but is sometimes helpful to understand what's going on. Simply btn means this.btn. Good luck!

Oh, one more thing - when you want to call a function which is on the next frame, think of this real world example: You live in the present (current frame). You know how to bike and can do it (call a method). Tomorrow (next frame) you will learn how to swim (method is defined on second frame, but you are still at frame 1). So you want to swim today? :)

There is a reason why it's called timeline - it's because it happens with time. If you want to call something - you can call methods only from the past (previous frames). Either define everything on first frame, or learn how to manage those needs (or best - start using classes).

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58