3

I want to dispatch a custom event from the Country() sto the MenuButton();

CountryEvent

package  {
import flash.events.Event;

public class CountryEvent extends Event {

    public static const COUNTRY_HOVERED:String = "onCountryOver";

    private var _countryName:String = "";

    public function CountryEvent(type:String, countryName:String, bubbles:Boolean=true, cancelable:Boolean=false) {
        super(type, bubbles, cancelable);
        _countryName = countryName;
    }

    public function get countryName():String {
        return _countryName;
    }

    public override function clone():Event
    {
        return new CountryEvent(type,countryName,bubbles,cancelable);
    }
}

} Country Class

package 
{

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

    public class Country extends MovieClip
    {
        private var countryEvent:CountryEvent;


        public function Country()
        {
            this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
        }

        private function onMouseOver(e:MouseEvent):void
        {

                countryEvent = new CountryEvent("onCountryOver",this.name);

                dispatchEvent(countryEvent);

            }
        }

        private function onMouseOut(e:MouseEvent):void
        {

        }
    }

}

MenuButton Class

package  {

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


    public class MenuButton extends MovieClip {

        public var countryName:String = "";

        public function MenuButton() {

            this.buttonMode = true;
            this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
            this.addEventListener(CountryEvent.COUNTRY_HOVERED,onCountryOver);
        }

        private function onCountryOver(e:CountryEvent):void {
            if(e.countryName == countryName) {
                this.gotoAndPlay(2);
            }
        }

        private function onMouseOver(e:MouseEvent):void {
            this.gotoAndPlay(2);

        }

        private function onMouseOut(e:MouseEvent):void {
            this.gotoAndPlay(11);
        }
    }

}

When a country is hovered a custom event is dispatched which I want the MenuButton to listen and if the parameter passed is the same as its name to get highlighted. The Country Class is the Base Class for my countries movieclips I have on stage and the MenuButton the Base Class for the menu button

It seems that the event never gets through

Thanks in advance

chchrist
  • 18,854
  • 11
  • 48
  • 82
  • Hi, how are your different elements (country, menubutton) added to stage ? Do they have the same parent? You may also want to remove the tweens from your example as they are independent from the problem you have and reduce the readability of your question. – Kodiak Apr 20 '11 at 09:59
  • They are not added by code but they are part of the world movieclip. world.Germany , world.Spain etc. I'll clear the code from the irrelevant stuff. – chchrist Apr 20 '11 at 10:04

2 Answers2

5

You have to make two modifications:

First, set your event bubbles property to true, so when a Country clip dispatches an event it will go up to the top level.

Then your MenuButtons should listen to stage, not to themselves. So when a Country dispatches an event it goes up to stage and can be caught by the buttons. If you want to listen to the stage you have to do a slight change in your code:

public function MenuButton() {

    this.buttonMode = true;
    this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
    this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(e:Event):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    stage.addEventListener(CountryEvent.COUNTRY_HOVERED,onCountryOver); 
}
Kodiak
  • 5,978
  • 17
  • 35
  • Thnx man you rock! One question why do I have to wait for the added_to_stage event? – chchrist Apr 20 '11 at 10:26
  • Because if you don't, `stage` doesn't exist yet. – Kodiak Apr 20 '11 at 10:27
  • I see... so I want for stage to be initialized. Thanx that helped clear some things in my mind – chchrist Apr 20 '11 at 10:36
  • 1
    Just to make a small correction. Its not stage initialized. Its when menuButton gets added to stage. Stage should be initialized long before anything is added to its displaylist. By waiting for ADDED_TO_STAGE to trigger we in effect are waiting for many processes to finish. ADDED_TO_STAGE just makes sure stage is accessible. – The_asMan Apr 20 '11 at 18:37
0

the best and easy way for solving this issue is by simply pass the stage reference as class level parameter and add event to the stage reference and dispatch event to the stage reference.

Country Class

package{

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

public class Country extends MovieClip
{
    private var countryEvent:CountryEvent;
    private var _stageRef:Stage;


    public function Country(pStageRef:Stage)
    {
        _stageRef = pStageRef;

        this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
        this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
    }

    private function onMouseOver(e:MouseEvent):void
    {

            countryEvent = new CountryEvent("onCountryOver",this.name);

            _stageRef.dispatchEvent(countryEvent);

        }
    }

    private function onMouseOut(e:MouseEvent):void
    {

    }
}

MenuButton Class

package  {

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


public class MenuButton extends MovieClip {

    public var countryName:String = "";
    private var _stageRef:Stage;

    public function MenuButton(pStageRef:Stage) {
        _stageRef = pStageRef;
        this.buttonMode = true;
        this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
        this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
        _stageRef.addEventListener(CountryEvent.COUNTRY_HOVERED,onCountryOver);
    }

    private function onCountryOver(e:CountryEvent):void {
        if(e.countryName == countryName) {
            this.gotoAndPlay(2);
        }
    }

    private function onMouseOver(e:MouseEvent):void {
        this.gotoAndPlay(2);

    }

    private function onMouseOut(e:MouseEvent):void {
        this.gotoAndPlay(11);
    }
}
Jose G Varanam
  • 767
  • 8
  • 19