Moving object on top of another

Hmm.  Apparently “tap” doesn’t send the object along with it.  I would recommend changing to a touch handler.

Rob

So Ive kind of figured out what I’m doing and kind of not lol.  What I have for code right now is a for loop putting random “lily” images on the screen in rows/columns and a jump event to move the frog to the images.  the issue is that it only “works” on the first image but won’t work on the rest, but it doesn’t “center” the frog on top of the lily either.  the code for that is this:

[lua] local rowCount = 4
local columnCount = 4
local imgSize = 160
for row = 0, rowCount do – Loop through the rows
local col = math.random(columnCount) – Choose a column randomly
local x = (col * imgSize) - imgSize – subtracting imgSize to start from 0
local y = row * imgSize
lily = display.newImageRect(“lily.png”, imgSize, imgSize)
lily.x = x
lily.y = y
lily.anchorX = 0
lily.anchorY = 0
sceneGroup:insert(lily)
end

local frog = display.newImageRect(“frog.png”,160,160)
frog.anchorX = .5
frog.anchorY = .5
frog.x = display.contentCenterX
frog.y = display.contentCenterY + 400
sceneGroup:insert(frog)

local function jump(event)
if event.phase == “began” then
frog.x = lily.x
frog.y = lily.y
end
return true
end

lily:addEventListener(“touch”,jump)

end

scene:addEventListener( “createScene”, scene )

return scene[/lua]

Now to test some theories I had I created a “new” game and made a jump event for each lily on the screen and it works when i click on either of them. The issue with this is that I want to make it so when a jump is made to the lily the images all move down to the previous image and a new one is made on the top of the screen in a random location. Im assuming this has to be done with a loop right? Anywhere is is the code that “works” in the  “new” game:

[lua]display.setStatusBar(display.HiddenStatusBar)

water = display.newImageRect(“water.jpg”, 1698, 1131)
water.x = display.contentWidth / 2
water.y = display.contentHeight / 2

lily1 = display.newImageRect(“lily.png”, 160, 160)
lily1.x = display.contentWidth / 2
lily1.y = display.contentHeight / 2
lily2 = display.newImageRect(“lily.png”, 160, 160)
lily2.x = display.contentWidth / 3
lily2.y = display.contentHeight / 3

frog = display.newImageRect(“frog.jpg”, 160, 160)
frog.x = display.contentWidth / 2
frog.y = display.contentHeight

local function jump1(event)
if event.phase == “began” then
frog.x = lily1.x
frog.y = lily1.y
end
return true
end

local function jump2(event)
if event.phase == “began” then
frog.x = lily2.x
frog.y = lily2.y
end
return true
end

local function endGame(event)
if event.phase == “began” then
local text = display.newText(“YOU FELL IN THE WATER!!!”, 315,300,native.systemFont,48)
text:setFillColor(0,0,0)
frog.alpha = 0
end
return true
end[/lua]

Hi @ssccard,

you may want to put your lily in a table before your code gets longer from adding too many lilies. :smiley:

This way you need just one jump function.

Here is one way you can do.

 local frog local water = display.newImageRect("water.png", 1698, 1131) water.x = display.contentWidth / 2 water.y = display.contentHeight / 2 local function jump(event) if event.phase == "began" then frog.x = event.target.x frog.y = event.target.y end return true end local numberOfLily = 5 -- number of lilies you want local lilyLocations = { {402, 400}, {202, 200}, {302, 500}, {102, 350}, {202, 700} } -- put the locations depend on number of lilies local lily = {} -- your table for i= 1, numberOfLily do lily[i] = display.newImageRect("lily.png", 160, 160) lily[i].x = lilyLocations[i][1] lily[i].y = lilyLocations[i][2] --lily[i].id = i lily[i]:addEventListener("touch",jump) --- you may add to display group here if you want to move lilies together end frog = display.newImageRect("frog.png", 160, 160) frog.x = display.contentWidth / 2 frog.y = display.contentHeight local function endGame(event) if event.phase == "began" then local text = display.newText("YOU FELL IN THE WATER!!!", 315,300,native.systemFont,48) text:setFillColor(0,0,0) frog.alpha = 0 end return true end water:addEventListener("touch",endGame)

Happy Coding!

burhan

burhan j,

when i use that code i get this error:[lua]Runtime error

assertion failed!
stack traceback:
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’
game.lua:51: in function game.lua:4
?: in function ‘dispatchEvent’
?: in function ‘gotoScene’
instructions.lua:6: in function instructions.lua:4
?: in function [/lua]

line 51 is :        lily[i]:addEventListener(“touch”,jump)

the error in the instructions line 6 and 4 has been corrected already.

Possibly could not find the jump function.

The jump function should be before calling the addEventlistener as in my example.

Which line 6 and 4 have errors and what errors?

burhan

that fixed it, but now when i click on the lily the frog is gone. i assumed that it was just “behind” the lily so i added frog:toFront() in the jump function but that didn’t do anything.

nevermind i fixed it. i had:

sceneGroup:insert(lily)

and i needed:

sceneGroup:insert(lily[i])

Now I’m stuck again lol.  Im trying to put them randomly on the screen by saving the x and y to the table to display but I seem to be missing something…

local numberOfLily = 5 – number of lilies you want
local x = 0
local y = 0

local lilyLocations = {{x, y}}

for i =1, numberOfLily do
local columnCount = 5
local col = math.random(columnCount) – Choose a column randomly
x.lilyLocations[i] = (col * 160) - 160 – subtracting imgSize to start from 0
y.lilyLocations[i] = i * 160
end

Try this.

local numberOfLily = 5 -- number of lilies you want local lilyLocations = {} local columnCount = 5 for i =1, numberOfLily do local col = math.random(columnCount) -- Choose a column randomly lilyLocations[i]= {} lilyLocations[i].x = (col \* 160) - 160 -- subtracting imgSize to start from 0 lilyLocations[i].y = i \* 160 end

burhan

ok well that runs without sending back any errors. but when i run it the lily is at the top left of the screen. i tried to set it with an anchor point but it still appears in the top left still

My latest code is based on your code and i edited to correct the syntax .

To put back on earlier code you have to change a bit since you use .x and .y instead of [1], [2].

So when creating lilies,

 for i= 1, numberOfLily do lily[i] = display.newImageRect("lily.png", 160, 160) lily[i].x = lilyLocations[i].x --- lilyLocations[i][1] -- changes lily[i].y = lilyLocations[i].y --- lilyLocations[i][2] -- changes --lily[i].id = i lily[i]:addEventListener("touch",jump) --- you may add to display group here if you want to move lilies together end 

burhan

that still leaves it in the top right corner when i run it…

Can you do a print statement?

for i =1, numberOfLily do local col = math.random(columnCount) -- Choose a column randomly lilyLocations[i]= {} lilyLocations[i].x = (col \* 160) - 160 -- subtracting imgSize to start from 0 lilyLocations[i].y = i \* 160 print("lilyLocations", i, lilyLocations[i].x, lilyLocations[i].y) end

 for i= 1, numberOfLily do lily[i] = display.newImageRect("lily.png", 160, 160) lily[i].x = lilyLocations[i].x --- lilyLocations[i][1] -- changes lily[i].y = lilyLocations[i].y --- lilyLocations[i][2] -- changes --lily[i].id = i lily[i]:addEventListener("touch",jump) print("lily ", i, lily[i].x, lily[i].y) end

Both location should be the same.

burhan

it has them all going to 0,0

2014-04-18 23:34:22.908 Corona Simulator[12140:507] lily     1    0    0

2014-04-18 23:34:22.908 Corona Simulator[12140:507] lily     2    0    0

2014-04-18 23:34:22.908 Corona Simulator[12140:507] lily     3    0    0

2014-04-18 23:34:22.909 Corona Simulator[12140:507] lily     4    0    0

2014-04-18 23:34:22.909 Corona Simulator[12140:507] lily     5    0    0

I may have to see more of your code.

You have to check why lilyLocations is 0.

Try putting some print statement along your code and work from there.

I run these code and it works fine.

burhan