Touch, create and follow?

All

New user as I am I need to ask for some help.

The problem I have is on the touch event… And what I’m trying to do is to add a object and at the same time I want this object to follow the finger…

like you point at the screen and a object show up and then you move the finger on the screen and the object follows it.
The sample for event.x (below) is great but as you can see it creates the objects at start of the app… And i don’t want that :slight_smile:

local circle = display.newCircle(50, 50, 100)
circle:setFillColor(0,255,00)
function moveCircle( event )
  circle.x = event.x
  circle.y = event.y
end
Runtime:addEventListener(“touch”, moveCircle)

I’ve been playing around with event.phase began, moved but that does not help at all.

Does anyone have any tips how to do this?

Cheers
Rotter [import]uid: 31201 topic_id: 5838 reply_id: 305838[/import]

Try this…
create a rectangle the size of your desired touchpad area (say 300x460). Name it tPad and make its alpha 0 and center it on screen.

Then change your listener from Runtime to tPad. This will allow the event to occur only when the user touches rather than all the time…saving on the processor.

This is how we made our crosshair targeting work in Garden of Orbs. (www.furiousapps.com) [import]uid: 9492 topic_id: 5838 reply_id: 19993[/import]

hmm can’t see how that will help at all. As I said I like to create an item when on “touch” and when I move the finger it should follow.

[import]uid: 31201 topic_id: 5838 reply_id: 19997[/import]

My solution connects your touch to an extremely lightweight object (a transparent vector rectangle)…then the event is triggered via that objects detection. Plus…a good rule of thumb is…NEVER use a RUNTIME call…unless you absolutely have to.

Try it…it works. Then by just making the tPad visible and not visible turns the sensor on and off and you don’t have to worry about shutting the runtime call on and off all the time. [import]uid: 9492 topic_id: 5838 reply_id: 19998[/import]

I believe your’s won’t work because you are not connecting “Touch” to anything.

Runtime generally goes with “enterframe” and is used to process a function or series of function in EVERY FRAME of the app’s render processing…

such as

function rotateObject()
object.rotate (1)
end

Runtime:addEventListener (“enterFrame”, rotateObject)

This would rotate the object 1 degree every frame…so at 30 FPS…the object would rotate 30 degrees per second in a fluid motion. [import]uid: 9492 topic_id: 5838 reply_id: 20003[/import]

Synthesis

Tried your suggesting but that generates 1000 of objects :slight_smile:

Example code:

local myRectangle = display.newRect(0, 0, 300, 460)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)

function moveCircle( event )
local circle = display.newCircle(50, 50, 10)
circle:setFillColor(0,255,00)

circle.x = event.x
circle.y = event.y
end

myRectangle:addEventListener(“touch”, moveCircle) [import]uid: 31201 topic_id: 5838 reply_id: 20093[/import]

Like this:

local myRectangle = display.newRect(0, 0, 300, 460)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)

local circle = display.newCircle(50, 50, 10)
circle:setFillColor(0,255,00)

function moveCircle( event )
circle.x = event.x
circle.y = event.y
end

myRectangle:addEventListener(“touch”, moveCircle)

You need to draw the circle outside of the function.
The function is only used to render the circle’s location in realtime to the Touch’s event X.Y. Once touch is released, the realtime update stops. [import]uid: 9492 topic_id: 5838 reply_id: 20094[/import]

yes

that is pretty much the same code as my first example and the behavior is the same it will only move the already created circle and not create a new circle when i touch.

[import]uid: 31201 topic_id: 5838 reply_id: 20110[/import]

if you want to create the circle on the touch then do this:

local myRectangle = display.newRect(0, 0, 300, 460)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)
function moveCircle( event )
if (event.phase == “began”) then
local circle = display.newCircle(50, 50, 10)
circle:setFillColor(0,255,00)
end

circle.x = event.x
circle.y = event.y

if (event.phase == “ended”) then
circle:removeSelf()
circle=nil
end

end

myRectangle:addEventListener(“touch”, moveCircle)

I think this will work…I didn’t test it…but it should. I made it so when you let go…the circle is removed. You can take that out if you want. I didn’t realize you were trying to spawn the circle on touch. I thought you just wanted to move it. [import]uid: 9492 topic_id: 5838 reply_id: 20112[/import]

no does not work
it creates the circle ok but when moving i just get " attempt to index global circle (a nil value) [import]uid: 31201 topic_id: 5838 reply_id: 20113[/import]

Got it to work (AT LAST) See code below.

Seems like I need to use Runtime:addEvent… after all :slight_smile: unless someone have a better solution.

Thanks synthesis for your effort.

–Code

local myRectangle = display.newRect(0, 0, 300, 460)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)
function CreateCircle( event )
local phase = event.phase
if “began” == phase then
circle = display.newCircle(event.x, event.y, 10)
circle:setFillColor(0,255,00)
Runtime:addEventListener(“touch”, MoveCircle)
end
if “ended” == phase then
circle:removeSelf()
end
end

function MoveCircle( event )
print (event.name…“occured”)
circle.x = event.x
circle.y = event.y
end
myRectangle:addEventListener(“touch”, CreateCircle) [import]uid: 31201 topic_id: 5838 reply_id: 20116[/import]

I recommend that if you are going to use a runtime listener…that you remove it when you don’t need it…
see modifications below:


Runtime:addEventListener(“touch”, MoveCircle)
end
if “ended” == phase then
Runtime:removeEventListener(“touch”, MoveCircle) – <<< ADD THIS LINE
circle:removeSelf()
end
end [import]uid: 9492 topic_id: 5838 reply_id: 20207[/import]

hi… please use [lua] [/lua] when posting code…

here’s my solution. this will create a circle and move it as long as the touch is pressed. then remove it when touch ends

[lua]local circle

local function moveCircle( event )

circle.x = event.x
circle.y = event.y

end
local function createCircle(event)

if(event.phase==“began”) then

circle = display.newCircle(50, 50, 100)
circle:setFillColor(0,255,00)

moveCircle(event)

elseif(event.phase==“moved”) then

moveCircle(event)

elseif(event.phase==“ended”) then

circle:removeSelf()

end

end

Runtime:addEventListener(“touch”, createCircle)[/lua] [import]uid: 6645 topic_id: 5838 reply_id: 20216[/import]

This works (just tested it)…The only problem with it before was that circle should not be local. No Runtime listeners needed.

[lua]local myRectangle = display.newRect(0, 0, 300, 460)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)

function moveCircle( event )
if (event.phase == “began”) then
circle = display.newCircle(50, 50, 10)
circle:setFillColor(0,255,00)
end

circle.x = event.x
circle.y = event.y

if (event.phase == “ended”) then
circle:removeSelf()
circle=nil
end

end

myRectangle:addEventListener(“touch”, moveCircle)[/lua]
[import]uid: 9492 topic_id: 5838 reply_id: 20229[/import]

synthesis

make sense Thanks a mill. for your help.

[import]uid: 31201 topic_id: 5838 reply_id: 20663[/import]