Touch event - help

What I’m trying to do is when a finger touches the screen(anywhere) it makes an object constantly float upwards in a linear fashion until the finger is removed from the screen. When the finger is removed the upward impulse is removed.

This is obviously wrong, but you can see what I’m trying to do.

[lua]function floatUpward(event)
while event.phase ~= “ended” do
myObject:applyLinearImpulse(0,-0.5,myObject.x, myObject.y);
end
end

Runtime:addEventListener(“touch” , floatUpward)[/lua]

Can anyone help? [import]uid: 48009 topic_id: 8653 reply_id: 308653[/import]

Here is a quick sample I wrote and tested out.

local physics = require("physics")  
physics.start()  
  
local box = display.newRect(0, 0, 50, 50)  
box.x = display.contentWidth / 2  
box.y = display.contentHeight - 25  
  
physics.addBody(box, "kinematic", {density=0, bounce=0, friction=0})  
function floatUpward(e)  
 if (e.phase == "began") then  
  
 box:setLinearVelocity(0, -200)  
  
 elseif (e.phase == "ended") then  
  
 box:setLinearVelocity(0, 0)  
  
 end  
end  
  
Runtime:addEventListener("touch", floatUpward)  
  

This requires the physics library to work correctly and the -200 can be adjusted to change the speed of the change in velocity.

Keep in mind -200 means it moves up on the screen, but a positive number like 200 will move down on the screen.

Hope this helps! [import]uid: 9968 topic_id: 8653 reply_id: 31060[/import]

Awesome, that’s perfect! Thanks a bunch. [import]uid: 48009 topic_id: 8653 reply_id: 31068[/import]

No problem! Glad it worked for you! [import]uid: 9968 topic_id: 8653 reply_id: 31109[/import]