Results 1 to 2 of 2

Thread: OOP Custom Event not working

  1. #1
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default OOP Custom Event not working

    ok I have 4 classes i have my main class, one for buttons, one for a complete menu, and i have my custom event.

    This is the set up. I have a button in the flash library that i'm giving a linkage of my Button class. the Menu class then creates the buttons and gives them all of there properties like text, width, etc. and adds them onto the menu. The Main is just creating a menu from my menu class and adding it to the stage.

    now for the real problem..

    in my buttons class i have an event listener listening for the button to be clicked. when it does it dispatches my custom event.

    that function looks like this:

    Code:
    public function onClick(e:MouseEvent):void
    		{
    			trace(e.currentTarget.id + " was clicked");
    			TweenLite.to(this.buttonMask, .3, {width:this.buttonBG.width});
    			
    			removeListeners();
    			dispatchEvent(new ButtonClickedEvent(ButtonClickedEvent.CHANGE_LISTENERS, e.currentTarget.id));
    		}
    everything works fine as far as I know in this function it traces out, runs the tween, and removes the event Listeners all fine.

    in my menu class i'm listening for that event so that i can take the previos button that was clicked and re add the event listeners that i removed from from it. the function i'm telling to be fired though when the event is fired isn't working

    i have this event Listener in my menu class

    Code:
    this.addEventListener(ButtonClickedEvent.CHANGE_LISTENERS, changeListeners);
    and then here's my current function that isnt working

    Code:
    private function changeListeners(e:ButtonClickedEvent):void
    		{
    			trace("current button "+ buttonList[4]);
    			buttonList[4].addListeners();
    			buttonList[4] = e.buttonClicked;
    			trace("new button "+ buttonList[4]);
    			
    		}
    I know this function isn't being fired because nothing is being traced out when i click on a button.

    here are my three classes in full for a complete reference and not just the trouble spots.

    The Button Class:

    Code:
    package AS
    {
    	import flash.display.MovieClip;
    	import flash.events.*;
    	
    	import gs.TweenLite;
    
    	public class Button extends MovieClip
    	{
    		public var id:String;
    				
    		public function Button(txt:String, id:String)
    		{
    			this.buttonMode = true;
    			this.button_text.mouseEnabled = false;
    			this.button_text2.mouseEnabled = false;
    			
    			this.button_text.text = txt;
    			this.button_text2.text = txt;
    			
    			this.id = id;
    			
    			addListeners();	
    		}
    		public function addListeners():void
    		{
    			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
    			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
    			this.addEventListener(MouseEvent.CLICK, onClick);
    		}
    		
    		public function removeListeners():void
    		{
    			this.removeEventListener(MouseEvent.MOUSE_OVER, mouseOver);
    			this.removeEventListener(MouseEvent.MOUSE_OUT, mouseOut);
    			this.removeEventListener(MouseEvent.CLICK, onClick);
    		}
    		
    		private function mouseOver(e:MouseEvent):void
    		{
    			TweenLite.to(this.buttonMask, .3, {width:this.buttonBG.width});
    		}
    		
    		private function mouseOut(e:MouseEvent):void
    		{
    			TweenLite.to(this.buttonMask, .1, {width:1});
    		}
    		
    		public function onClick(e:MouseEvent):void
    		{
    			trace(e.currentTarget.id + " was clicked");
    			TweenLite.to(this.buttonMask, .3, {width:this.buttonBG.width});
    			
    			removeListeners();
    			dispatchEvent(new ButtonClickedEvent(ButtonClickedEvent.CHANGE_LISTENERS, e.currentTarget.id));
    		}	
    		
    	}
    }
    The Menu Class:

    Code:
    package AS
    {
    	import flash.display.MovieClip;
    
    	public class Menu extends MovieClip
    	{
    		public var who_btn:Button;
    		public var work_btn:Button;
    		public var play_btn:Button;
    		public var contact_btn:Button;
    		private var buttonList:Array
    		
    		public function Menu()
    		{
    			initButtons();
    			this.addEventListener(ButtonClickedEvent.CHANGE_LISTENERS, changeListeners);
    		}
    		
    		private function initButtons():void
    		{
    			who_btn = new Button("WHO", "who_btn");
    			work_btn = new Button("WORK","work_btn");
    			play_btn = new Button("PLAY", "play_btn");
    			contact_btn = new Button("CONTACT", "contact_btn");
    			
    			buttonList = [who_btn, work_btn, play_btn, contact_btn, who_btn];
    			
    			work_btn.y = who_btn.height;
    			play_btn.y = work_btn.y + work_btn.height;
    			contact_btn.y = play_btn.y + play_btn.height;
    			
    			who_btn.button_text.width = 67;
    			who_btn.button_text2.width = 67;
    			work_btn.button_text.width = 80;
    			work_btn.button_text2.width = 80;
    			play_btn.button_text.width = 63;
    			play_btn.button_text2.width = 63;
    			contact_btn.button_text.width = 115
    			contact_btn.button_text2.width = 115;
    			
    			who_btn.buttonBG.width = who_btn.button_text.width + 5;
    			work_btn.buttonBG.width = work_btn.button_text.width + 5;
    			play_btn.buttonBG.width = play_btn.button_text.width + 5;
    			contact_btn.buttonBG.width = contact_btn.button_text.width + 5;
    						
    			this.addChild(who_btn);
    			this.addChild(work_btn);
    			this.addChild(play_btn);
    			this.addChild(contact_btn);
    		}
    		
    		private function changeListeners(e:ButtonClickedEvent):void
    		{
    			trace("current button "+ buttonList[4]);
    			buttonList[4].addListeners();
    			buttonList[4] = e.buttonClicked;
    			trace("new button "+ buttonList[4]);
    			
    		}
    		
    		
    		
    	}
    }
    and my ButtonClickedEvent

    Code:
    package AS
    {
    	import flash.events.*;
    	
    	public class ButtonClickedEvent extends Event
    	{
    		public static const LOAD_MOVIECLIP:String = "Load Movie Clip";
    		public static const CHANGE_LISTENERS:String = "Add and Remove Button MouseEvents";
    		public var buttonClicked:*;
    		
    		public function ButtonClickedEvent(type:String, button:* )
    		{
    			super(type);
    			buttonClicked = button;
    		}
    		
    		override public function clone():Event
            {
                return new ButtonClickedEvent(type, buttonClicked);
            }
     
            override public function toString():String
            {
                return formatToString("ButtonClickedEvent", "type", "buttonClicked");
            }
    
    	}
    }
    I'm really new to OOP and trying to teach myself so any help would be appreciated

    thanks a lot.

  2. #2
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    I figured it out.

    since i wasn't mimicking the event class exactly the event wasn't bubbling up to the next document.

    so heres how I fixed it.

    Code:
    public function ButtonClickedEvent(type:String, button:String, bubbles:Boolean=true, cancelable:Boolean=false)
    Heres the whole event class.

    Code:
    package AS
    {
    	import flash.events.*;
    	
    	public class ButtonClickedEvent extends Event
    	{
    		public static const LOAD_MOVIECLIP:String = "Load Movie Clip";
    		public static const CHANGE_LISTENERS:String = "Add and Remove Button MouseEvents";
    		public var buttonClicked:String;
    		
    		public function ButtonClickedEvent(type:String, button:String, bubbles:Boolean=true, cancelable:Boolean=false)
    		{
    			super(type, bubbles, cancelable);
    			buttonClicked = button;
    		}
    		
    		override public function clone():Event
            {
                return new ButtonClickedEvent(type, buttonClicked);
            }
     
            override public function toString():String
            {
                return formatToString("ButtonClickedEvent", "type", "buttonClicked");
            }
    
    	}
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •