|
-
Script kiddie
[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?
-
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
}
}
-
Script kiddie
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.
-
Trace works in flashdevelop. I use it all the time. What version do you have?
-
Script kiddie
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|