Klye
Klye

Reputation: 104

AS3: Two actions on one Click event listener

So what i am trying to do is get a menu to pop up on click. Now I only want to use one. So on the first click i would need it to move to x 130. Then on the next click move to x -77. How would I do this? I've tried an if statement but that didn't work out to well for me.

function clickMove (e:MouseEvent):void{
    smenu_mc.x = 130;
    if(smenu_mc.x == 130){
        smenu_mc.x = -77;
    }
}

Upvotes: 0

Views: 89

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

What you are currently doing is always setting it to -77. Because your if statement will always be true: (see comments next to you code)

function clickMove (e:MouseEvent):void{
    smenu_mc.x = 130; //you're setting it to 130
    if(smenu_mc.x == 130){ //this will be true, because of the line above...
        smenu_mc.x = -77;
    }
}

What you need to do is toggle the value:

function clickMove (e:MouseEvent):void{
    if(smenu_mc.x < 130){ //if it's less than 130, then set it to 130, otherwise set it to -77.
        smenu_mc.x = 130;
    }else{
        smenu_mc.x = -77;
    }
}

Upvotes: 2

Related Questions