How to place object in the place I touch ?
local function touch(e) if e.phase == "began" then local block = display.newRect(0,0,10,10) block.x , block.y = e.target.x , e.target.y end end Runtime:addEventListener("touch",touch)
How to place object in the place I touch ?
local function touch(e) if e.phase == "began" then local block = display.newRect(0,0,10,10) block.x , block.y = e.target.x , e.target.y end end Runtime:addEventListener("touch",touch)
I didn’t know you could make touch listener on runtime. Cool.
Value of e if you print it out is:
[lua][table] Value = {
| y = 80,
| x = 47,
| time = 21157.247,
| id = “userdata: 0x105d13e44”,
| phase = “ended”,
| yStart = 80,
| xStart = 47,
| name = “touch”,
}, [/lua]
So change to e.target.x and e.target.y to e.x and e.y.
I want to make a feed function how to make player move to the food automatically
local player = display.newRect(111,111,30,30) local bg = display.newRect(111,300,500,800) bg:setFillColor(0.2,0.4,0.2) local function touch(e) if e.phase == "began" then local food = display.newCircle(0,0,5) food.x , food.y = e.x , e.y transition.to(player,{time = 2000 , x = food.x ,y = food.y}) end end bg:toBack() bg:addEventListener("touch",touch)
This code is a bit dangerous if you click fast. So put a tag on the transition and cancel it before making a new one.
[lua]
local player = display.newRect(111,111,30,30)
local bg = display.newRect(111,300,500,800)
bg:setFillColor(0.2,0.4,0.2)
local function touch(e)
if e.phase == “began” then
local food = display.newCircle(0,0,5)
food.x , food.y = e.x , e.y
transition.cancel(“playerTransition”)
transition.to(player,{tag = “playerTransition”, time = 2000 , x = food.x ,y = food.y})
end
end
bg:toBack()
bg:addEventListener(“touch”,touch)
[/lua]
I’m guessing you want to make it go to the next food automatically? Then you can chain the transition something like this:
[lua]local player = display.newRect(111,111,30,30)
local bg = display.newRect(111,300,500,800)
bg:setFillColor(0.2,0.4,0.2)
local foods = {}
local goingToFood = 0
local currentlyTransition = false
local goToNext
local function goToFood()
if foods[goingToFood+1] and not currentlyTransition then
goingToFood = goingToFood + 1
currentlyTransition = true
transition.to(player,{onComplete = goToNext, tag = “playerTransition”, time = 2000, x = foods[goingToFood].x, y = foods[goingToFood].y})
end
end
function goToNext()
currentlyTransition = false
goToFood()
end
local function touch(e)
if e.phase == “began” then
foods[#foods+1] = display.newCircle(0,0,5)
foods[#foods].x , foods[#foods].y = e.x , e.y
goToFood()
end
end
bg:toBack()
bg:addEventListener(“touch”,touch)[/lua]
If you are not familiar with lua tables I would try to study that a bunch.
I didn’t know you could make touch listener on runtime. Cool.
Value of e if you print it out is:
[lua][table] Value = {
| y = 80,
| x = 47,
| time = 21157.247,
| id = “userdata: 0x105d13e44”,
| phase = “ended”,
| yStart = 80,
| xStart = 47,
| name = “touch”,
}, [/lua]
So change to e.target.x and e.target.y to e.x and e.y.
I want to make a feed function how to make player move to the food automatically
local player = display.newRect(111,111,30,30) local bg = display.newRect(111,300,500,800) bg:setFillColor(0.2,0.4,0.2) local function touch(e) if e.phase == "began" then local food = display.newCircle(0,0,5) food.x , food.y = e.x , e.y transition.to(player,{time = 2000 , x = food.x ,y = food.y}) end end bg:toBack() bg:addEventListener("touch",touch)
This code is a bit dangerous if you click fast. So put a tag on the transition and cancel it before making a new one.
[lua]
local player = display.newRect(111,111,30,30)
local bg = display.newRect(111,300,500,800)
bg:setFillColor(0.2,0.4,0.2)
local function touch(e)
if e.phase == “began” then
local food = display.newCircle(0,0,5)
food.x , food.y = e.x , e.y
transition.cancel(“playerTransition”)
transition.to(player,{tag = “playerTransition”, time = 2000 , x = food.x ,y = food.y})
end
end
bg:toBack()
bg:addEventListener(“touch”,touch)
[/lua]
I’m guessing you want to make it go to the next food automatically? Then you can chain the transition something like this:
[lua]local player = display.newRect(111,111,30,30)
local bg = display.newRect(111,300,500,800)
bg:setFillColor(0.2,0.4,0.2)
local foods = {}
local goingToFood = 0
local currentlyTransition = false
local goToNext
local function goToFood()
if foods[goingToFood+1] and not currentlyTransition then
goingToFood = goingToFood + 1
currentlyTransition = true
transition.to(player,{onComplete = goToNext, tag = “playerTransition”, time = 2000, x = foods[goingToFood].x, y = foods[goingToFood].y})
end
end
function goToNext()
currentlyTransition = false
goToFood()
end
local function touch(e)
if e.phase == “began” then
foods[#foods+1] = display.newCircle(0,0,5)
foods[#foods].x , foods[#foods].y = e.x , e.y
goToFood()
end
end
bg:toBack()
bg:addEventListener(“touch”,touch)[/lua]
If you are not familiar with lua tables I would try to study that a bunch.