How can a function detect the position of a moving object???
Helppp!!
How can a function detect the position of a moving object???
Helppp!!
How are you moving the object? Perhaps posting some code would help.
function circleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(circle) else if event.phase == "ended" then circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y) display.getCurrentStage():setFocus(nil) end end end circle:addEventListener("touch", circleTouch)
[lua]
local function circleTouch(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(circle)
elseif event.phase == “moved” then
print(event.x,event.y)
elseif event.phase == “ended” then
circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y)
display.getCurrentStage():setFocus(nil)
end
end
circle:addEventListener(“touch”, circleTouch)
[/lua]
And how can i do appear the text “you win!”, when the circle passes between two other objects???
have a look at physics.rayCast here. http://docs.coronalabs.com/api/library/physics/rayCast.html
You basically draw an invisible line between the two objects using the rayCast and if the circle hits this invisible line then you can use it to display your “You win” text.
stick your raycast code in an Runtime enterFrame listener so it checks if something hits the line every frame.
ok…THANKS!!
How have i to write the x and the y of the invisible line??
you can do this to check when the circle passes between two other objects in the Runtime enterFrame Listener
[lua]
local function checkforhit(e)
local from_x = yourobject1.x
local from_y = yourobject1.y
local to_x = yourobject2.x
local to_y = yourobject2.y
local hits = physics.rayCast( from_x, from_y, to_x, to_y, “closest” )
if hits[1].object == yourcircle then
print (“You Win”)
end
end
Runtime:addEventListener(“enterFrame”,checkforhit)
[/lua]
Helppp, i can find the error:
local W = display.contentWidth local H = display.contentHeight local physics = require ("physics") physics.start() physics.setGravity(0, 5) physics.setScale(50) local circle = display.newCircle(367, 870, 50) physics.addBody(circle,"dinamic", {density = 1.0, friction = 0.3, bounce = 0.2}) local ground = display.newRect(0, 1280, 720, 1) physics.addBody(ground, "static", {}) local roof1 = display.newRect(0, 0, 180, 1) physics.addBody(roof1, "static", {}) local roof2 = display.newRect(540, 0, 180,1) physics.addBody(roof2, "static", {}) local wallL = display.newRect(0,0,1, 1280) physics.addBody(wallL, "static", {}) local wallR = display.newRect(720, 0, 1, 1280) physics.addBody(wallR, "static", {}) local bar1 = display.newRect(720/4,0, 15, 100) physics.addBody(bar1, "static", {}) local bar2 = display.newRect(540, 0, 15, 100) physics.addBody(bar2, "static", {}) local from\_x = bar1.x local from\_y = bar1.y local to\_x = bar2.x local to\_y = bar2.y local hits = physics.rayCast( from\_x, from\_y, to\_x, to\_y, "closest" ) if hits[1].object == circle then print ("You Win") end circle:addEventListener("Runtime", enterFrame) function circleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(circle) else if event.phase == "ended" then circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y) display.getCurrentStage():setFocus(nil) end end end circle:addEventListener("touch", circleTouch)
I can’t find the error!!!
How are you moving the object? Perhaps posting some code would help.
function circleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(circle) else if event.phase == "ended" then circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y) display.getCurrentStage():setFocus(nil) end end end circle:addEventListener("touch", circleTouch)
[lua]
local function circleTouch(event)
if event.phase == “began” then
display.getCurrentStage():setFocus(circle)
elseif event.phase == “moved” then
print(event.x,event.y)
elseif event.phase == “ended” then
circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y)
display.getCurrentStage():setFocus(nil)
end
end
circle:addEventListener(“touch”, circleTouch)
[/lua]
And how can i do appear the text “you win!”, when the circle passes between two other objects???
have a look at physics.rayCast here. http://docs.coronalabs.com/api/library/physics/rayCast.html
You basically draw an invisible line between the two objects using the rayCast and if the circle hits this invisible line then you can use it to display your “You win” text.
stick your raycast code in an Runtime enterFrame listener so it checks if something hits the line every frame.
ok…THANKS!!
How have i to write the x and the y of the invisible line??
you can do this to check when the circle passes between two other objects in the Runtime enterFrame Listener
[lua]
local function checkforhit(e)
local from_x = yourobject1.x
local from_y = yourobject1.y
local to_x = yourobject2.x
local to_y = yourobject2.y
local hits = physics.rayCast( from_x, from_y, to_x, to_y, “closest” )
if hits[1].object == yourcircle then
print (“You Win”)
end
end
Runtime:addEventListener(“enterFrame”,checkforhit)
[/lua]
Helppp, i can find the error:
local W = display.contentWidth local H = display.contentHeight local physics = require ("physics") physics.start() physics.setGravity(0, 5) physics.setScale(50) local circle = display.newCircle(367, 870, 50) physics.addBody(circle,"dinamic", {density = 1.0, friction = 0.3, bounce = 0.2}) local ground = display.newRect(0, 1280, 720, 1) physics.addBody(ground, "static", {}) local roof1 = display.newRect(0, 0, 180, 1) physics.addBody(roof1, "static", {}) local roof2 = display.newRect(540, 0, 180,1) physics.addBody(roof2, "static", {}) local wallL = display.newRect(0,0,1, 1280) physics.addBody(wallL, "static", {}) local wallR = display.newRect(720, 0, 1, 1280) physics.addBody(wallR, "static", {}) local bar1 = display.newRect(720/4,0, 15, 100) physics.addBody(bar1, "static", {}) local bar2 = display.newRect(540, 0, 15, 100) physics.addBody(bar2, "static", {}) local from\_x = bar1.x local from\_y = bar1.y local to\_x = bar2.x local to\_y = bar2.y local hits = physics.rayCast( from\_x, from\_y, to\_x, to\_y, "closest" ) if hits[1].object == circle then print ("You Win") end circle:addEventListener("Runtime", enterFrame) function circleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(circle) else if event.phase == "ended" then circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y) display.getCurrentStage():setFocus(nil) end end end circle:addEventListener("touch", circleTouch)