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
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.
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]
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]
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]
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]
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.
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]
Seems like I need to use Runtime:addEvent… after all 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
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]