Create a Crate. Moves right to left repeatedly. Press button. Crate goes up 100 px on Y axis. And starts over again.

Create a Crate.
Moves crate from right to left repeatedly until a button is pressed.
Move Crate up 100 px on Y axis.
And crate starts repeating the side to side transition again.
Repeat until Y axis value is 0.

For some reason I cant get the logic to work.

How do I keep creating a NEW crate every few pixels on a NEW Y Axis ?

I know I got to use a transition.to but I cant seem to get it to work.

seems that every time i modify the crate.y value it RESETS to original starting Y value.
Any ideas ?

Here is the code

local physics = require( “physics” )
physics.start()
local disp_width = display.contentWidth --is the width in screen coordinates.

local sky = display.newImage( “bkg_clouds.png” )
sky.x = 160; sky.y = 195

local ground = display.newImage( “ground.png” )
ground.x = 160; ground.y = 445
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )
local crate = display.newImage( “crate.png” )
crate.x =lvl_x;crate.y = 380;crate.rotation = 0
physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )

function transition_to_left()
transition.to(crate, {time=crate_speed, x = 0, y = crate.y, onComplete = transition_to_right})
end

function transition_to_right()
transition.to(crate, {time=crate_speed, x = 300, y = crate.y, onComplete = transition_to_left})
end

transition_to_right()
–lt blue
local button2 = display.newRoundedRect( 220, 425, 50, 40, 8 )
button2:setFillColor( 0, 170, 170, 170 )

local side_right = display.newImage(“side_right.png”,-95,0)
physics.addBody(side_right,“static”, {density=1,friction=1,bounce=1})

local side_left = display.newImage(“side_left.png”,350,0)
physics.addBody(side_left,“static”, {density=1,friction=1,bounce=1})

function button2:touch(event)

if event.phase == “began” then
crate.y = crate.y - 50
transition_to_right()
end
if event.phase == “ended” then
local crate = display.newImage( “crate.png” )
crate.x =lvl_x;crate.y = 380;crate.rotation = 0
crate.alpha=1
end

return true
end

button2:addEventListener(“touch”,button2)
----------------------------------------------------------------- [import]uid: 11094 topic_id: 16774 reply_id: 316774[/import]

if you want to animate multiple images you may need to create separate handles for each image. you can store the image name in an array to do this.
I dint get what your need actually is. If you can describe it more clearly I will help you. :slight_smile:
I tried running your code. it creates a crate which moves continuously from left to right and back. when i click the button the first crate moves up and another one appears ? do you want that one also to animate the same way ?
[import]uid: 71210 topic_id: 16774 reply_id: 62845[/import]

my goal is to have a crate move right to left.
button is pressed the crate “stops” AND a new crate is created but 60 pixels higher on
the Y Axis.

when the button is pressed again the crate stops. The crate drops. AND another new crate
is created but yet another 60 pixels higher on the Y Axis.

Basically I am trying to “stack” the crates on one another at the press of a button
but if the crate is not exactly “above” the previous “below” crate, it drops and breaks.

I just trying to get the tranistions to move back and forth on each new Y Axis
after the button is pressed each time.

[import]uid: 11094 topic_id: 16774 reply_id: 62869[/import]

you may use tables for this
the below code may help you.
[lua]local physics = require( “physics” )
physics.start()
local disp_width = display.contentWidth
local crate ={}

crate[1] = display.newImage( “crate.png” )
crate[1].x =lvl_x;crate[1].y = 380;crate[1].rotation = 0
physics.addBody( crate[1], { density=3.0, friction=0.5, bounce=0.3 } )

function transition_to_left()
transition.to(crate[#crate], {time=crate_speed, x = 0, y = crate[#crate].y, onComplete = transition_to_right})
end

function transition_to_right()
transition.to(crate[#crate], {time=crate_speed, x = 300, y = crate[#crate].y, onComplete = transition_to_left})

end

transition_to_right()

–lt blue
local button2 = display.newRoundedRect( 220, 425, 50, 40, 8 )
button2:setFillColor( 0, 170, 170, 170 )
function button2:touch(event)
if event.phase == “ended” then
crate[#crate + 1] = display.newImage( “crate.png” )
crate[#crate].x =lvl_x
crate[#crate].y = crate[#crate-1].y - 50
crate[#crate].rotation = 0
crate[#crate].alpha=1
end

return true
end

button2:addEventListener(“touch”,button2)[/lua] [import]uid: 71210 topic_id: 16774 reply_id: 62935[/import]

just a quick thought…
can you try removing the local keyword from front of crate declared inside the touch function. otherwise it will create a local version of the crate and the crate.y used inside transition will still be referring to the one outside the function. [import]uid: 71210 topic_id: 16774 reply_id: 62936[/import]

does your book include techniques/examples like this ?
[import]uid: 11094 topic_id: 16774 reply_id: 63244[/import]

@troynall the book available at technowand.com is a study guide to learn the basics. It won’t be having specific examples like this. But our new code snippet library (which is to be released soon) will have code samples for many common tasks. keep an eye for the release… :slight_smile:
[import]uid: 71210 topic_id: 16774 reply_id: 63259[/import]