user2521634
user2521634

Reputation: 61

Clicking objects to move it over and over

What i am trying to do here is to make a button move when i click it, and then when i click the button again it moves to the original position. However when i run this on flash the button moves only once when i first click it, but then when i click it again it doesnt move back to its position. Any help would be great thanks.

stop();

import flash.events.MouseEvent;

var step1click;
var step2click;

button1.addEventListener(MouseEvent.CLICK, positionswitch);
function positionswitch(event:MouseEvent):void
{
    button1.x = 426;
    button1.x = 266;
    step1click = 1
}

if(step1click == 1) {

    button1.addEventListener(MouseEvent.CLICK, positionswitch2);
    function positionswitch2(event:MouseEvent):void 
    {  
        button1.x = 156;
        button1.y = 253;
        step2click = 1;
    }
}

if(step2click == 1){

    button1.addEventListener(MouseEvent.CLICK, positionswitch3);
    function positionswitch3(event:MouseEvent):void 
    {
        button1.x = 426;
        button1.y = 266;
    }
}

Upvotes: 0

Views: 898

Answers (2)

moosefetcher
moosefetcher

Reputation: 1901

You could try this:

stop();
import flash.events.MouseEvent;
var _clickToggle:int = 0;
var _xPos:Array = new Array(156, 426);
var _yPos:Array = new Array(253, 266);

button1.addEventListener(MouseEvent.CLICK, positionswitch);

function positionSwitch(e:MouseEvent):void {
_clickToggle = (_clickToggle + 1) % 2; // the % sign is modulo; will toggle the value between 0 and 1
button1.x = _xPos[_clickToggle];
button1.y = _yPos[_clickToggle];
}

Upvotes: 0

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

Try something like this:

import flash.events.MouseEvent;

stop();

var dir:Boolean=false;

button1.addEventListener(MouseEvent.CLICK, positionSwitch);

function positionSwitch(event:MouseEvent):void
{
  dir =!dir;
  button1.x = (dir) ? 426 : 156;
  button1.y = (dir) ? 266 : 253;
}

Upvotes: 2

Related Questions