[AS3]Assign multiple functions to a single button

5 posts

Flag Post

So, I’m trying to find an efficient method of assigning multiple functions to a single button that are used based on whatever the button is labeled as. I’ve looked for several ways but several solutions are way out of my scope of understanding and the tutorials and information a bit hard to follow.

For simplicity, I was thinking of a function attached to the button that was, basically like this:

[ buttonFunction("Button Label", targetFunction, targetFunction Argument()); ]

However, I can’t even begin to figure out how to makes something like that work. Currently, my coding to achieve this is freakishly messy. Because my movieclips don’t exists on the stage and are loaded through another class when called, the references to the buttons are all dot notations for this reason.

[ private function btnEvent1(e:MouseEvent):void {
			if (nextBtn) nextBtn = false; //Flag for an over-ride function
			if (btn1Choice == 0) Core.screens.game.btn1.visible = false;
			if (Core.screens.game.btn1.btnText.text == "Male") {
				Player.isMale();
			}
			Core.events.currEvent = btn1Choice;//Button assignment handled through a numeric system
			if (Core.story_Active) { //Flags for different segments of events, so the game properly calls the right one
				MainStory.mainStory(Core.events.currEvent);
			}
			if (Core.tyrVillage_Active) {
				Tyr.village(Core.events.currEvent);
			}
			if (Core.tyrRuins_Active) {
				Tyr.ruins(Core.events.currEvent);
			}
		} ]

If anyone could point me to some tutorials that 1)Answer my need and 2)Are fairly simple to understand, that would be great or even advice on alternative approaches or whatever.

I thought using arrays might help but I’m still not completely comfortable working with them so it’s been kinda hit or miss utilizing those.

 
Flag Post
Originally posted by T3HFish:

So, I’m trying to find an efficient method of assigning multiple functions to a single button that are used based on whatever the button is labeled as.

Why do you need multiple functions per button? Why not just have multiple buttons?

I’m not 100% clear on what you’re trying to do, but my best guess is that you want a context-dependent button. That is, a button whose text changes based on the state of the game, and whose effects change along with the text.

Assuming that’s it, what I’d suggest is placing multiple buttons on top of one another. Each button should have only one label, and it should always perform the same function. The key is that you set button.visible = false; for all but one of the buttons. That way, only the currently-active button can be seen or clicked.

 
Flag Post

I should probably clarify that I’m making a text adventure game.

Like I showed in my example, Button 1 already has, at least, 2 functions attached to it in a weird way. The nextBtn controls a doNext function that over-rides the button coding and programs Button 1 so it just moves to the next event when clicked, but only when the nextBtn flag is tripped. Then, if the button is labeled ‘Male’, Button 1 is assigned an entirely different function and then as the game goes, it’s name changes, as will the other 15 buttons I’ve got currently that fall under this problem.

To move forward in the linked up events, it then checks for a whichever flag is active, then attaches the numeric assignment (currEvent) to the button’s number assignment (btn1Choice)… which is very annoying because it’s causing issues across the board when it comes to navigating through the story right now… not to mention having to manually label each button in dot notation is a pain.

 
Flag Post
//pseudocode
var gamestate = "gamestate1";
var events = {
gamestate1:{
label1:function() {
trace("You clicked the button with the label \"label1\" while being in gamestate1");
}
label2:function() {
trace("You clicked the button with the label \"label2\" while being in gamestate1");
}
}
}
function onBtnClick(mouseEvent) {
events[gamestate][mouseEvent.target.label]();
}
for (button in everybutton) {
button.addEventListener("click", onBtnClick);
}

Not a very neat way to do it, but this allows you to call different functions by the label of the button being clicked with only one MouseEvent handler, and also being able to switch between different gamestates.
By following a similar structure, you can add more data so you don’t have to hardcode every function. For example, if you are making a text adventure you could use numerical keys for different events and, instead of using a function to determine the next action, making a number property for the next event key.

 
Flag Post

A state machine is what you’re looking for.