0

I want to call the function "clica" more than once but changing the number of the frame inside gotoAndStop ("gotoAndStop(x)"). How can I pass a value x to the function clica? Thanks

 stop();

jogar.addEventListener(MouseEvent.CLICK, clica);

function clica(e:MouseEvent):void{
    gotoAndStop(2);
}
  • Can you be more specific in what way you want to "change the number of the frame"? Do you want it to increase with each click? Do you have different buttons that each go to a different frame? – Aaron Beall Apr 16 '15 at 15:51
  • check out that answer, the question it's about the same http://stackoverflow.com/questions/14028444/can-dispatchevent-carry-arguments-in-as3/14028926#14028926, you can't use first approach with native events, but second one will work – fsbmain Apr 16 '15 at 15:57
  • I don't want to wrote two functions like this... I want only one function jogar.addEventListener(MouseEvent.CLICK, clica); jogar2.addEventListener(MouseEvent.CLICK,clica2); function clica(e:MouseEvent):void{ gotoAndStop(2); } function clica2(e:MouseEvent):void{ gotoAndStop(3); } – Inês Rodrigues Apr 16 '15 at 15:58

2 Answers2

2

Ok, your goal is to go to a certain frame, depending on what caused the MouseEvent.

One way of doing this is to save what object corresponds to what frame. Think of this like a telephone book. It associates a person with a number to be called.

To create such a "telephone book" in actionscript, you use a dictionary. First add all associations to it like so:

var object2frame:Dictionary = new Dictionary();
object2frame[play] = 2; // associate play with the number 2
object2frame[play2] = 3;

DISCLAIMER: I replaced your non English identifiers with English ones. You should always write your code in English. This makes it easier for people to read the code who do not know your language. Get used to it.

the second part of the solution is currentTarget, a property of the Event object that references the object that caused the Event. I hope you can see how this comes together now:

  1. If the function is called, first find out who caused it, that is, what object was clicked.
  2. Knowing the source of the Event, use that information to look up the frame that you should go to in the dictionary.
  3. Go to that frame.

Here's how the code could look like:

var object2frame:Dictionary = new Dictionary();
object2frame[play] = 2; // associate play with the number 2
object2frame[play2] = 3;

play.addEventListener(MouseEvent.CLICK, click);
play2.addEventListener(MouseEvent.CLICK, click);

function click(e:MouseEvent):void
{
    gotoAndStop(object2frame[e.currentTarget]);
}

There are other solutions to this problem.

null
  • 5,207
  • 1
  • 19
  • 35
  • 1
    custom events is the cleanest solution but many people disregard it for lack of understanding or simple laziness. – BotMaster Apr 16 '15 at 16:55
  • @BotMaster I totally agree. Do you think I should expand the "there are other solutions" part at the end to explicitly include this or is your comment enough of a hint for the interested reader? – null Apr 16 '15 at 16:59
  • @BotMaster - can you show an example of a custom Mouse Click event. – BadFeelingAboutThis Apr 16 '15 at 17:57
1

Assuming jogar is a MovieClip (or any dynamic class instance) you can assign properties to it, such as what frame it should navigate to when clicked, then use event.currentTarget to retrieve its properties in the event handler. For example:

jogar1.targetFrame = 1;
jogar2.targetFrame = 2;
jogar3.targetFrame = 3;

jogar1.addEventListener(MouseEvent.CLICK, clica);
jogar2.addEventListener(MouseEvent.CLICK, clica);
jogar3.addEventListener(MouseEvent.CLICK, clica);

function clica(e:MouseEvent):void {
    var targetFrame:int = e.currentTarget.targetFrame;
    gotoAndPlay(targetFrame);
}

You can reduce this even further by wrapping the setup in a function:

function setupJogar(jogar:MovieClip, targetFrame:int):void {
    jogar.targetFrame = targetFrame;
    jogar.addEventListener(MouseEvent.CLICK, jogarClick);
}

function jogarClick(e:MouseEvent):void {
    gotoAndStop(e.currentTarget.targetFrame);
}

setupJogar(jogar1, 1);
setupJogar(jogar2, 2);
setupJogar(jogar3, 3);
// ...etc
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
  • Dictionary properties are dynamic properties, too. ;) The only real difference here is the property is associated with the target instead of a separate property holder. If you don't want to use dynamic properties, declare a class that has a property `targetFrame` and link it to the `jogar` objects. – Aaron Beall Apr 16 '15 at 18:24
  • Yes, that would be a much better way. Should have clarified that for me it's more of a compile time checking thing with dynamic objects. If you misspell the defined dictionary object, you'll get a compiler error. If you misspell "targetFrame" you'll get a runtime error. Certainly not that big of difference in the grand scheme of things. – BadFeelingAboutThis Apr 16 '15 at 18:47
  • @Aaron I totally agree, both solutions rely on dynamic properties. I posted the dictionary solution because it works with ´MouseEvent´ dispatchers that are not dynamic. Other than that, they are pretty much the same. – null Apr 16 '15 at 18:47