Moving an object

Im trying to make a object move to a location touched on the screen but my transition.to doesn’t seem to be working. Here is what I have:

[lua] local frog = display.newImageRect(“frog.png”,160,160)

frog.anchorX = .5
frog.anchorY = .5
frog.x = display.contentCenterX
frog.y = display.contentCenterY + 400
local function jump(event)
transition.cancel(frog.trans)
frog.trans = transition.to(frog,{time=2000, x=event.x, y=event.y})
end

Runtime:addEventListener(“tap”,jump)[/lua]

Any ideas where Im going wrong?

I don’t use the tap event often.  Does it work if you make it a touch event?  I’m wondering if you can put a tap event on the Runtime.

I changed it to “touch” instead of “tap” and it still does not work.

In your jump function, can you put ‘print(event.x); print(event.y);’? This will at least let you know where you’re trying to move the frog to, if anywhere.

when I add that code into the function it does not display anything when the screen is touched/clicked

Hi @ssccard.

Your code runs ok on my side.

Probably other part of your code caused the conflict and prevent the tap/touch event from being captured.

burhan

here is the full code…

[lua]local storyboard = require(“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(event)

local sceneGroup = self.view

background = display.newImageRect(“water.png”,640,1136)
background.anchorX = .5
background.anchoryY = .5
background.x = display.contentCenterX
background.y = display.contentHeight / 2
sceneGroup:insert(background)

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

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)
end

local function jump(event)
transition.cancel(frog.trans)
frog.trans = transition.to(frog,{time=2000, x=event.x, y=event.y})
print(event.x); print(event.y)
end

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
local img = display.newImageRect(“lily.png”, imgSize, imgSize)
img.x = x
img.y = y
img.anchorX = 0
img.anchorY = 0
print(x,y)

end

Runtime:addEventListener(“touch”,jump)[/lua]

Hi,

your jump function and event listener are out of the createScene function.

It is not even in the enterScene function which cause them not to be called.

try this. ( you may name this e.g. game.lua and call from main.lua)

local storyboard = require("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local sceneGroup = self.view background = display.newImageRect("water.png",640,1136) background.anchorX = .5 background.anchoryY = .5 background.x = display.contentCenterX background.y = display.contentHeight / 2 sceneGroup:insert(background) lily = display.newImageRect("lily.png",160,160) lily.anchorX = .5 lily.anchorY = .5 lily.x = display.contentCenterX lily.y = display.contentCenterY + 400 sceneGroup:insert(lily) 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) transition.cancel(frog.trans) frog.trans = transition.to(frog,{time=2000, x=event.x, y=event.y}) print(event.x); print(event.y) end Runtime:addEventListener("tap",jump) 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 local img = display.newImageRect("lily.png", imgSize, imgSize) img.x = x img.y = y img.anchorX = 0 img.anchorY = 0 print(x,y) end end scene:addEventListener( "createScene", scene ) return scene

Good Luck!

burhan

WOOHOO that worked! Thanks!

I don’t use the tap event often.  Does it work if you make it a touch event?  I’m wondering if you can put a tap event on the Runtime.

I changed it to “touch” instead of “tap” and it still does not work.

In your jump function, can you put ‘print(event.x); print(event.y);’? This will at least let you know where you’re trying to move the frog to, if anywhere.

when I add that code into the function it does not display anything when the screen is touched/clicked

Hi @ssccard.

Your code runs ok on my side.

Probably other part of your code caused the conflict and prevent the tap/touch event from being captured.

burhan

here is the full code…

[lua]local storyboard = require(“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(event)

local sceneGroup = self.view

background = display.newImageRect(“water.png”,640,1136)
background.anchorX = .5
background.anchoryY = .5
background.x = display.contentCenterX
background.y = display.contentHeight / 2
sceneGroup:insert(background)

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

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)
end

local function jump(event)
transition.cancel(frog.trans)
frog.trans = transition.to(frog,{time=2000, x=event.x, y=event.y})
print(event.x); print(event.y)
end

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
local img = display.newImageRect(“lily.png”, imgSize, imgSize)
img.x = x
img.y = y
img.anchorX = 0
img.anchorY = 0
print(x,y)

end

Runtime:addEventListener(“touch”,jump)[/lua]

Hi,

your jump function and event listener are out of the createScene function.

It is not even in the enterScene function which cause them not to be called.

try this. ( you may name this e.g. game.lua and call from main.lua)

local storyboard = require("storyboard") local scene = storyboard.newScene() function scene:createScene(event) local sceneGroup = self.view background = display.newImageRect("water.png",640,1136) background.anchorX = .5 background.anchoryY = .5 background.x = display.contentCenterX background.y = display.contentHeight / 2 sceneGroup:insert(background) lily = display.newImageRect("lily.png",160,160) lily.anchorX = .5 lily.anchorY = .5 lily.x = display.contentCenterX lily.y = display.contentCenterY + 400 sceneGroup:insert(lily) 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) transition.cancel(frog.trans) frog.trans = transition.to(frog,{time=2000, x=event.x, y=event.y}) print(event.x); print(event.y) end Runtime:addEventListener("tap",jump) 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 local img = display.newImageRect("lily.png", imgSize, imgSize) img.x = x img.y = y img.anchorX = 0 img.anchorY = 0 print(x,y) end end scene:addEventListener( "createScene", scene ) return scene

Good Luck!

burhan

WOOHOO that worked! Thanks!