Detect finger on a moving object

Hello,

I’m new in Corona (started about a week ago, coming from Gamesalad) and I got stuck trying to program this:

I have a star drawn with a polyline moving around the screen. My game consist of the following: the user must touch the star and follow its shape with the finger touching the screen. If it goes out of the star borders, game over. Or if the finger is lifted, game over.

Any idea how to program this

Thank you very much! Corona is a great application, but I need more practise. [import]uid: 40363 topic_id: 7222 reply_id: 307222[/import]

Check out touch listeners:

http://developer.anscamobile.com/content/events-and-listeners [import]uid: 10903 topic_id: 7222 reply_id: 25459[/import]

Hello,

thank you, but I am completely lost. I wrote following to my program:

“local function listener(event)
print(event.name…“occurred”)
return true
end”

and then at the end

“star1:addEventListener( “touch”, listener )
star2:addEventListener( “touch”, listener )
star3:addEventListener( “touch”, listener )”

star1, 2 and 3 are polylines. I don’t know how to handle this. I don’t know even if the function works. Basiacally, I want to have a variable that changes to 1 when the finger is on the star (not when it is pressed like a button, just when it is on the star). Then, when the finger is out of the star borders or the user does not touch the screen, the variable changes and I can detect it is out of the borders. Anyone could help me? I would appreciate much. I am lost with this…

For ex-Gamesalad users, I need the same function as when the mouse is inside the object…

Thank you very much in advance. [import]uid: 40363 topic_id: 7222 reply_id: 25553[/import]

Can you post your full code? or at least the relevant section using the code tag.

[import]uid: 10903 topic_id: 7222 reply_id: 25555[/import]

OK,

this is a simple version of my code. I just draw 2 lines. The user must to touch the line and keep his/her finger on the line. What I am trying to program is following: detect if the finger gets out of the lines borders (game over) or if the finger stops touching the screen (game over). I also need that the detection passes from one star to the other, with the finger always touching the screen.

Thank you very much. I really have no idea how to solve this.

yspeed = 5

star1x = 0
star1y = 0
star2x = 0
star2y = 0

star1 = display.newLine( 265,-160, 300,300)
star1:setColor( 255, 102, 102, 250 )
star1.width = 70

star2 = display.newLine( 320,-200, 500,-400)
star2:setColor( 255, 102, 102, 250 )
star2.width = 70

local function animate(event)

star1y = star1y + yspeed

star1:translate( star1x-star1.x,star1y-star1.y)

if star1.y>=968 then
star1:removeSelf()
end

star2y = star2y + yspeed

star2:translate( star2x-star2.x,star2y-star2.y)

if star2.y>=968 then
star2:removeSelf()
end

end

Runtime:addEventListener( “enterFrame”, animate );
[import]uid: 40363 topic_id: 7222 reply_id: 25629[/import]

Maybe something like this? The user drags the yellow square over the red to follow its path.

[code]
local abs = math.abs

local star1 = display.newRect( 320,250,50,300)

local star2 = display.newRect( 320,200, 400,50)

local touchpad = display.newRect(320,550,50,50)
touchpad:setFillColor(255,0,0)

local touchpad2 = display.newRect(320,550,50,50)
touchpad2:setFillColor(255,255,0)

local function padmovesecond()
local padmove2 = transition.to( touchpad, {time=4000, alpha=1, x=touchpad.x+350, y=touchpad.y } )
end

local padmove = transition.to( touchpad, {time=4000, alpha=1, x=touchpad.x, y=touchpad.y-350, onComplete=padmovesecond } )

function touchListener4 (touch)
local phase = touch.phase

if (phase == “moved”) then
–print(“touch”)
touchpad2.x = touch.x
touchpad2.y = touch.y
elseif (phase == “ended”) or (phase == “cancelled”) then
print(“end”)
end
end

touchpad2:addEventListener(“touch”, touchListener4)

local function listener(event)

if abs(touchpad.x - touchpad2.x) > 25 or abs(touchpad.y - touchpad2.y) > 25 then
print(“bad”)
else
print(“good”)
end

end

Runtime:addEventListener(“enterFrame”, listener)
[/code] [import]uid: 10903 topic_id: 7222 reply_id: 25639[/import]

Hello,

First of all, thank you very much for the code.

I copied the code into a new main.lua file and run it. I see the white path and the red square following it, and the yellow square that I can move with the mouse (touch). I see from the code (excuse if I am wrong) I should get some kind of text message telling me if I am following correctly the red square. But I am getting no message so, I cannot check if it works well.

Anyway, I think it won’t suit into my program. I try to explain: as I could see, you check if the touching coordinates are near the red pad coordinates. In my game, I cannot have such a red pad. Imagine I have a path going down from the top of the screen and that my finger must “follow” the path. The path in my case is a polyline. Is there a way to check if the touching coordinates are into the polyline borders?

Thanks again for your quick response and support.

Regards
[import]uid: 40363 topic_id: 7222 reply_id: 25741[/import]

I’m really not sure how to work with poly-lines. Someone else could probably help you better. [import]uid: 10903 topic_id: 7222 reply_id: 25746[/import]

OK, thank you very much for your support. [import]uid: 40363 topic_id: 7222 reply_id: 25754[/import]