mrGott
mrGott

Reputation: 1076

Action Script and MovieClip - how to unite several MovieClips?

I just started to read the book Learning ActionScript 3.0. So in the 3rd chapter the author covers EventLinsteners.

There's a sub-heading Event Propagation. From the beginning author creates to Folders on the Stage, with instance names Folder0 and Folder1 and explains Mouse_Over event. Than it says if you have 100 of such folders on your stage you'll have to write EventListener for every Folder instance. So they introduce some "strange for me" type of instance with name folder_group, and it's like 1 MovieClip but with several MovieClips inside (I guess MovieClip is the correct reference).

So, shortly, in the next part of this chapter there's Frame Events sub-heading where Author has Unicycle. That unicycle is a single MovieClip too. It has to instances in it, when you double click it. The body instance and wheel instance.

So, how the hell should I do that. How to create such movieclip that has several different parts in it. When you double click it, you move from Scene1 -> "Some Name".

Please someone, explain how to do that... I tried to google it but to clue yet.

and one more thing. What's the difference between the separate ActionScript file and FLA-s ActionScript mode (I mean F9)

Thanks in advance

Upvotes: 0

Views: 289

Answers (1)

Daniel
Daniel

Reputation: 35724

regarding event propagation you can look at this link, the example swf shows bubbling/propagation and stoping propagation

by default, the click event will propagate, so as long as you attach the event to the top-level movieClip, you can set the action there.

you can try this simple test. Create a MovieClip with a name "mc" and and put some other movie clips in there, using this code, any time you click on anything that is within that mc, an event will fire. if you name these movieClips, you'll see the names pop up, otherwise you should see names that are something like this "instance#"

mc.addEventListener(MouseEvent.CLICK,onHit);

function onHit(e:MouseEvent):void{
    trace(e.target.name);
}

having the code in an .as file makes it a lot easier to code. For example I do 99.7% of the coding in an external editor, and only use flash to create my graphic resources. You can use what's called a documentClass, which extends the default MovieClip that acts as the stage, most examples in adobe help are using these. For quick tests I just use the code panel(F9). The big difference really is that coding this way (using .as files) is a lot easier and faster, especially with an IDE like FlashDevelop, FlashBuilder, FDT etc.


for the fla you provided, you could use this for the doc class

package {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class FrameEvents extends MovieClip {

        public function FrameEvents() {
            cycle.addEventListener(MouseEvent.CLICK,onHit);
        }

        public function onHit(e:MouseEvent):void{
            trace(e.target.name);
        }
    }
}

you should see body traced when you click on the body, and wheel when you click on wheel

Upvotes: 1

Related Questions