transition + button help needed

i am new to corona sdk and i have been experimenting with the software for a while now, but i am building an app at the same time

I was trying to get a image to move from one position to another when i click a button; this worked howwever i noticed that if i keep clicking the button the image would keep moving left.

is there a way that i can get the image to reset itself and start the transition from start again when the button is pressed again?

or/and is there a function i could use so that once the image has moved after the first button press, it then moves back to its origin on the second button press?

here is my code below:_________________________________


local background = display.setDefault(“background”, 255, 255, 255 )

widget = require(“widget”)

local rightEyeCover = display.newImage( “eyecoverright.png”, left, top, isFullResolution )
rightEyeCover:translate( 620, -30)

local function event1(event)
transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad })
–transition.to(rightEyeCover, {time = 2000, x = 0, delta=true, transition=easing.outQuad })
end

local button6 = widget.newButton{
    label = “move shape”,
    fontSize = 30,
    width = 640,
    height = 90,
    left = 0,
    top = 355,
    onPress=event1
}


please answer asap

thanks :slight_smile:

A method you can use to accomplish this is to implement an onComplete variable to your transition. 

http://docs.coronalabs.com/api/library/transition/to.html#params.oncomplete-optional

If you used the onComplete option, you could do something like this:

local function event1(event)

        local function finishMove()

            rightEyeCover.x = 620

        end

    transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove })

end

That should get you started on the road to a viable solution for your app.

Thanks alot for the help

Ive just encountered another problem…

if i repeatedly click on the button before the animation is finish then the image (rightEyeCover) keeps moving foward as well

is there a way that i can restrict the distance the image can travel

and can i set it so it restarts the transition when the button is pressed again (i know i asked this before but im still struggling)

In essence i want the whole ‘image moving animation/transition’ thing to restart after i click the button again

thanks again

So you don’t want it to revert to the original location? If that is the case, try this out:

local rightEyeCover = display.newImage( “eyecoverright.png”, left, top, isFullResolution )

rightEyeCover:translate( 620, -30)

rightEyeCoverMovement = 1

local function event1(event)

    if rightEyeCoverMovement == 1 then

        local function finishMove1()

            rightEyeCoverMovement = 2

        end

    rightEyeCoverMovement = 3

    transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove1 })

    end

    if rightEyeCoverMovement == 2 then

        local function finishMove2()

            rightEyeCoverMovement = 1

        end

    rightEyeCoverMovement = 3

    transition.to(rightEyeCover, {time = 2000, x = 620, delta=true, transition=easing.outQuad, onComplete=finishMove2 })

    end

end

This is the most super-simple of examples and I haven’t tested it out yet, but it should do the following:

a. Move the object to the a location

b. Not let the object be altered during the transition is taking place

c. Allow the object to move again after the transition has completed

Anything more than that, and you might need to sign up for that Corona 101 course!

That did the job thanks

A method you can use to accomplish this is to implement an onComplete variable to your transition. 

http://docs.coronalabs.com/api/library/transition/to.html#params.oncomplete-optional

If you used the onComplete option, you could do something like this:

local function event1(event)

        local function finishMove()

            rightEyeCover.x = 620

        end

    transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove })

end

That should get you started on the road to a viable solution for your app.

Thanks alot for the help

Ive just encountered another problem…

if i repeatedly click on the button before the animation is finish then the image (rightEyeCover) keeps moving foward as well

is there a way that i can restrict the distance the image can travel

and can i set it so it restarts the transition when the button is pressed again (i know i asked this before but im still struggling)

In essence i want the whole ‘image moving animation/transition’ thing to restart after i click the button again

thanks again

So you don’t want it to revert to the original location? If that is the case, try this out:

local rightEyeCover = display.newImage( “eyecoverright.png”, left, top, isFullResolution )

rightEyeCover:translate( 620, -30)

rightEyeCoverMovement = 1

local function event1(event)

    if rightEyeCoverMovement == 1 then

        local function finishMove1()

            rightEyeCoverMovement = 2

        end

    rightEyeCoverMovement = 3

    transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove1 })

    end

    if rightEyeCoverMovement == 2 then

        local function finishMove2()

            rightEyeCoverMovement = 1

        end

    rightEyeCoverMovement = 3

    transition.to(rightEyeCover, {time = 2000, x = 620, delta=true, transition=easing.outQuad, onComplete=finishMove2 })

    end

end

This is the most super-simple of examples and I haven’t tested it out yet, but it should do the following:

a. Move the object to the a location

b. Not let the object be altered during the transition is taking place

c. Allow the object to move again after the transition has completed

Anything more than that, and you might need to sign up for that Corona 101 course!

That did the job thanks