Skip to main content
Hano188
Participant
February 1, 2021
Answered

Need a bit of help with buttons and using functions that i already created in the past

  • February 1, 2021
  • 1 reply
  • 389 views

Alright, i'll try to keep this brief. I'm working on a little project wheree it switches between a lot of scenes via buttons and on many of them only partial changes are applied, so my question is this: What command/commands could i use to recall/reuse a function originally created. As an example, here is one of the functions for the main screen. 

 

BigPurpleButton1.addEventListener(MouseEvent.CLICK, Database);
function Database(evt:MouseEvent):void {
gotoAndStop(1, "Database");
}

 

There you go, i'd rather not make 231564 variations of the database function if there's a way to reuse it.

But so far i've made 3, i don't really want to complicate the code so for now i'll take a break until this can be figured out.

 

Any help would be greatly appreciated, thank you.

This topic has been closed for replies.
Correct answer mark@headTrix

You can easily use functions. That's the basis of programming. "Write once, run anywhere"

 

I don't think it makes too much sense with an event listener though. The user should have to click a specific button for that, and you can always just place the button in other places.

 

But you can reuse a function like this

 

function myTestFunction() {
     alert("I am testing my function!");
}

and to call it:

myTestFunction();

 

that's it. Hope that helps!

 

cheers,

mark

1 reply

mark@headTrixCommunity ExpertCorrect answer
Community Expert
February 2, 2021

You can easily use functions. That's the basis of programming. "Write once, run anywhere"

 

I don't think it makes too much sense with an event listener though. The user should have to click a specific button for that, and you can always just place the button in other places.

 

But you can reuse a function like this

 

function myTestFunction() {
     alert("I am testing my function!");
}

and to call it:

myTestFunction();

 

that's it. Hope that helps!

 

cheers,

mark

headTrix, Inc. | Adobe Certified Training & Consulting
Hano188
Hano188Author
Participant
February 2, 2021

Ah, i see, i'm just starting out in actionscript so i just found the base code for a button online. Not sure what else to use but in any case, thanks a lot for the answer!

Community Expert
February 2, 2021

You are welcome. I hope it will help you.

 

With a button, you place the code once, and the button has an instance name... so wherever that button is - if the user clicks... it will trigger the action/function. So you would not really reuse the code.

 

A function is a list of instructions, so if you had a long list of things the function was doing - then it would make sense to trigger that list by calling the function instead of rewriting the list of code. It's also good for updating. So if you want to add something to that action you can.


An example of this would be a game. When playing the game you would trigger correct and incorrect responses.

When correct you could trigger a movie clip animation that makes some lights blink, and you can trigger another movie clip that roates a siren light, and you can trigger an audio snippet. All of that could be in a function called "Winner();" for example. You could also create a function for an incorrect answer, and load a red x, and a different audio file.

 

Then when playing the game there would probably be an array or conditional statement.
In laymans terms - if movie clip A and movieClip B collide - run the Winner function, and you might also have if movie clip B and movie clip C collide run the Winner function. So this function might be in a bunch of places, and re-writing it over ane over would be a pain the manage. So if you ever wanted to update the Winner function by adding +10 to the current score... you could do it easily and the winner function would easily be updated, and nothing else would have to be changed.

 

Hope that helps.
have fun programming.

cheers,

mark

headTrix, Inc. | Adobe Certified Training & Consulting