(Solved)Velocity while touching, need help is stuck

Hello!

a newbie here, this is my first post! I have a sprite which falls to the ground and i want it to go up while touching the screen, and then fall down again, but i only succeed to get it to jump up little. It only reacts every time i click with the mouse in the simulator and not while i am holding the mouse button down.

This is my code:

function spriteInstance2:touch(event)
if (event.phase == “began”) then
print(event.x)
spriteInstance2:setLinearVelocity(0, -100)

end

end

Runtime:addEventListener(“touch”, spriteInstance2)

i know that i only listens after the began phase so of course it won’t continue to push my sprite up but really need help to do it because i just can’t get my head around it?

Sorry for my bad english=)

Kind regards Isak

(Edit)

Solved it before anyone answered but i thought i should tell how i did and i just used a timer with touch ended like this:

function spriteInstance2:touch(event)

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

tmr = timer.performWithDelay(100, vel, -1)

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

timer.cancel(tmr)

end

end

function vel(event)
spriteInstance2:setLinearVelocity(0, -event.count * 50)
end

Runtime:addEventListener(“touch”, spriteInstance2)
This way it keeps pushing the object upwards as long as button is pressed and stops then i release, it also goes faster and faster the longer the button is pressed.

maybe someone else can find this useful=)

Kind regards Inkoqnito [import]uid: 90942 topic_id: 15772 reply_id: 315772[/import]

Hello and welcome Inkoqnito!

For future reference, please post your code within lua tags. These are < lua > and < / lua > without the spaces. (Makes it much easier to read.)

Congratulations on solving your problem :slight_smile:

Peach [import]uid: 52491 topic_id: 15772 reply_id: 58351[/import]

Hey Peach!

Thanks and sorry didn’t know about that will do that next time:)

About the code i changed it later to use force instead of velocity it made more sense and worked like a charm:)

Have a good day!

/Inkoqnito [import]uid: 90942 topic_id: 15772 reply_id: 58743[/import]

No worries, just makes it easier to read :slight_smile:

Thanks for posting your solution too, others may be interested in the future.

Peach :slight_smile: [import]uid: 52491 topic_id: 15772 reply_id: 58830[/import]