user2421975
user2421975

Reputation: 197

Simple click event

I don't know why I can't figure out this problem that is really basics !! (sometimes the brain is tired I guess).

I've got a movieclip with a guitar string. I want the string to move everytime I click on it.

I've created a movie clip with 2 lables. the first = non movement, the second = movement. I've placed in the second one a stop(); action. (in order to stop the loop)

I've put this code :

stringOne.addEventListener(MouseEvent.CLICK, accord1, false, 0, true);

public function accord1(e:MouseEvent):void{
            var stringOne;
            trace("DING");
            stringOne.gotoAndStop("first");
        }

It works but, of course, it only play the string movement at the first click.

Do you know how I could play the string movement EVERYTIME that I click on the string ?

Thank you very much and sorry for this easy question (little ashamed)..,

EDIT

Ah ! It seems to work with goToAndPlay !

if (stringOne.currentLabel=="premier") {
     stringOne.gotoAndStop("default");
            } else {
     stringOne.gotoAndStop("first");
}

Just a thing, I have to click twice..

(one click = the string vibrate (label2)) 
(one click again = the string does nothing (going to label 1)) 
(one click again = the string vibrate (label 2)) 

Is there anyway to automatically skip the 2nd click (the one that tells the string to go back at label 1), and let do the code like : - When animation of label 2 is finished, automatically go back to label 1 ? –

Upvotes: 0

Views: 117

Answers (3)

dhc
dhc

Reputation: 657

Presumably, the "movement" label of the string is some sort of animation?

It seems to me what you want in your "guitar string" movieClip is, on the last frame of the timeline animation for "movement", a script that says gotoAndStop('non movement'). The click handler should gotoAndPlay('movement').

Then, when the string 'movement' animation is finished, it will reset itself, so that the next time you click it will play again.

So, your original code is fine (before the edit; but remove the "var stringOne;" since that will break the reference to stringOne). The only thing you need to add is a script in the timeline on the last frame of the movement animation ("first" label?) that says gotoAndPlay("default") (assuming default is the 'non movement' label). You may need a stop() in the timeline frame for "default".

Upvotes: 1

user2421975
user2421975

Reputation: 197

Thank you for the answer. I've found an other way, instead of mouse click I've used mouse down for going to frame 2 and mouse up for going to frame 1

Upvotes: 0

Simon Eyraud
Simon Eyraud

Reputation: 2435

Something like that :

stage.addEventListener( MouseEvent.CLICK, onStageClick);

protected function onStageClick(event:MouseEvent):void
{
    switch( event.target )
    {
        case stringOne:
            trace("String one stuff");
            break;
        case stringTwo:
            trace("String 2 stuff");
            break;
    }
}

Or add your movieClip into a Sprite and listen click event on the Sprite and not on the MovieClips.

Upvotes: 0

Related Questions