A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [HELP] Class hell, how do I make my button call a method?

  1. #1
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590

    [HELP] Class hell, how do I make my button call a method?

    Just popped out of dormancy to ask this:

    In my root class's constructor function, I make an instance of a button. How do I make it so that the button, when clicked, calls one of the root class's methods?

    looks kinda like this (preçised it to just the relevant parts)... class structure is set up such that we spend all of our time in the constructor function of Invasion_5; even the main game loop is an enterFrame function that's called from within the constructor function (I suppose that's weird, but for the life of me I couldn't find a better method).

    basically we call begin("menu") to bring up the menu, and what I need is for the button to call begin("game").

    Code:
    package {
    	// import stuff
    	public class Invasion_5 extends Sprite {
    		// declare loads of stuff and import sprites here
    		public function Invasion_5() {
    			function begin(mode:String) { // begin() takes you to a different screen (like frames)
    				if (mode == "menu") {
    					var myButton:menuButton = new menuButton(/*some parameters*/);
    					addChild(myButton);
    					// this is the button I want to use.
    					// when clicked, it will need to call begin("game");
    				} else if (mode == "game") {
    					// starts game
    				}
    			}
    			begin("menu"); // go to menu screen
    		}
    	}
    }
    so there's the main class, and here's how my button's set up:

    Code:
    package {
    	// import stuff
    	public class menuButton extends MovieClip {
    		// declare loadsa stuff
    		public function menuButton(/*some parameters*/):void {
    			// initiate stuff, setup eventListeners
    		}
    		// a few functions for handling mosueOver, mouseOut, etc
    		public function mouseUp(event:Event):void {
    			// button has been released
    			// at this point we want it to tell the parent, Invasion_5, to call begin("game");
    			// but... _how_?
    		}
    	}
    }
    I think that explains the nature of the problem. Anyone have any thoughts? Ideally something which can slip into my current, weird class structure; I know this whole begin() system of switching between states is really non-standard. But it has the advantage that the whole game stays inside that same public function, Invasion_5, rather than moving into different functions; it seems to be the only way I can carry my variables and declarations across into the next 'frame' (or some similar reason; I never really understood it myself).

    could it be that events could be a solution to this? for example, dispatching an "I have been clicked" event when the button is clicked, and having my root class listen out for the event, then call begin("game"); when that happens? I played around with this and came to the conclusion that I would only be able to call one of those public function with an event listener, as opposed to begin(), which is a function declared inside a public function. so that's probably not an option.

    but yeah, really having trouble with this. any ideas?
    http://www.birchlabs.co.uk/
    You know you want to.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Hrmm...your function is inside the constructor but I guess so is the button so the scopes will hold eachother in memory...a little unorthodox but I guess it probably works.

    Try this:
    PHP Code:
            public function mouseUp(event:Event):void {
                
    dispatchEvent(new Event('menuButtonClicked'));
            } 
    PHP Code:
                function begin(mode:String) { // begin() takes you to a different screen (like frames)
                    
    if (mode == "menu") {
                        var 
    myButton:menuButton = new menuButton(/*some parameters*/);
                        
    addChild(myButton);
                        
    myButton.addEventListener('menuButtonClicked', function(e:Event):void{
                            
    begin('game');
                        })
                    } else if (
    mode == "game") {
                        
    // starts game
                    
    }
                } 

  3. #3
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    thanks man, you cracked it!

    I was sitting there for about half an hour wondering why no matter what I typed, nothing changed (it's damn hard to debug an app without a trace() function; what's wrong with FlashDevelop?!), then it turned out that all the code I'd been typing ALL DAY was in the wrong section of the if(){ structure; in other words, I'd been assuming this was all under (mode == "test"), where I had developed the button class, but it was actually in (mode == "menu"), like I wrote in that pseudo-code. :P

    I wonder now, how many of the things I tried would've worked if I'd typed them in the right place... although in retrospect I don't think any of my eventListener attempts would've worked inside the constructor function, so no big loss.
    http://www.birchlabs.co.uk/
    You know you want to.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Trace works in flashdevelop. I use it all the time. What version do you have?

  5. #5
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Got 3.0.0 Beta7... I'll see if an update fixes it.

    [EDIT] Yup, worked.
    Last edited by VENGEANCE MX; 06-19-2009 at 07:20 PM.
    http://www.birchlabs.co.uk/
    You know you want to.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center