Remove instat jump to position

Hello im newbie to the corona i want to ask why my object jump directly to cordinate’s, i want to walk to there not instantly change his to new x and y.

display.setStatusBar( display.HiddenStatusBar ) local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) system.setAccelerometerInterval( 10 ) local menuItems local background local playBtn local destx local desty local obj --Main-- function main() --Methods-- mainMenu() end function mainMenu() menuItems = display.newGroup() background = display.newImage( "bg.png" ) playBtn = display.newImage( "playbtn.png" ) playBtn.name = "playbutton" playBtn.x, playBtn.y = display.contentWidth / 2 + 40, display.contentHeight / 2 menuItems:insert( background ) menuItems:insert( playBtn ) playBtn:addEventListener( "tap", loadGame ) end function loadGame(event) if event.target.name == "playbutton" then transition.to(menuItems,{time = 210, alpha=0, onComplete = addGameScreen}) playBtn:removeEventListener( "tap", loadGame ) end end function addGameScreen() demoLevel() end function demoLevel() background = display.newRect( 0, 0, display.contentWidth + 120, display.contentHeight ) background:setFillColor ( 255, 255, 255 ) obj = display.newImage("rect.PNG") obj.x, obj.y = 32, 32 physics.addBody( obj, { density=3.0, friction=0.5} ); Runtime:addEventListener ( "enterFrame", move ) Runtime:addEventListener ( "tap", onTap ) end function move( ) if destx ~= nil then obj.x = obj.x + destx - obj.x end if desty ~= nil then obj.y = obj.y + desty - obj.y end end function onTap( event ) destx = event.x desty = event.y print("".. destx - obj.x .. " " .. obj.y - desty .."") end main()

I want to ask about structure of code too

There are two things to think about.

1.  The reason it jumps is your move function changes the old X to a new X and doesn’t do anything to actually move the object.  Corona SDK is “frame” based.  That means that 30 times per second the screen is updated (1 frame) (unless you have your FPS set to 60, in which case it’s 60 times per second).  When you start at another  x, y and you change it to another x, y that all happens in a single frame.  For your object to move you have to use various techniques to move them a little bit at a time.  This can happen in one of three ways:  Use physics to move them,  Use transitions to move them, or use incremental x, y adjustments each frame (over time) using the enterFrame event.  If you just set the values, then it’s going to jump.

  1. Look at your math:

obj.x = obj.x + destx - obj.x 

This is the same a A - A + B.  The two A’s cancel out and you just have B left.  So in effect you are doing:

obj.x = destx

I feel that you were trying to do an incremental move here.  I would suggest you start with the transition.to() API Call.  research that and see if that gets you started.

Yeah, i know its necessary to update object x every frame my question is how to do that, i try with for loop but same thing happens.

 if destx ~= nil then --obj.x = obj.x + destx - obj.x if obj.x \> destx then resX = obj.x - destx for i=0, resX do obj.x = obj.x - 1 end end if obj.x \< destx then resX = destx - obj.x for i=0, resX do obj.x = obj.x + 1 end end end

Yes i fixed it :slight_smile:

Not sure how you’ve fixed it (I’m one of those people who just loves seeing “I’ve fixed it” posts in forums  ;)).

For anyone who wants to know a way to do it, I do the following (assuming everything else is the same as gman2’s scenario).

Instead of:

obj.x = obj.x + destx - obj.x

I would do this

obj.x = obj.x + 0.5 \* (destx - obj.x)

This will move it gradually towards the target position. Changing the 0.5 will change the speed. This also means that the object will move faster across a larger distance, and slow down as it gets nearer.

@Alan

Yes man thanks your way is a lot better than main.

But your method is need some improvements :slight_smile:

There are two things to think about.

1.  The reason it jumps is your move function changes the old X to a new X and doesn’t do anything to actually move the object.  Corona SDK is “frame” based.  That means that 30 times per second the screen is updated (1 frame) (unless you have your FPS set to 60, in which case it’s 60 times per second).  When you start at another  x, y and you change it to another x, y that all happens in a single frame.  For your object to move you have to use various techniques to move them a little bit at a time.  This can happen in one of three ways:  Use physics to move them,  Use transitions to move them, or use incremental x, y adjustments each frame (over time) using the enterFrame event.  If you just set the values, then it’s going to jump.

  1. Look at your math:

obj.x = obj.x + destx - obj.x 

This is the same a A - A + B.  The two A’s cancel out and you just have B left.  So in effect you are doing:

obj.x = destx

I feel that you were trying to do an incremental move here.  I would suggest you start with the transition.to() API Call.  research that and see if that gets you started.

Yeah, i know its necessary to update object x every frame my question is how to do that, i try with for loop but same thing happens.

 if destx ~= nil then --obj.x = obj.x + destx - obj.x if obj.x \> destx then resX = obj.x - destx for i=0, resX do obj.x = obj.x - 1 end end if obj.x \< destx then resX = destx - obj.x for i=0, resX do obj.x = obj.x + 1 end end end

Yes i fixed it :slight_smile:

Not sure how you’ve fixed it (I’m one of those people who just loves seeing “I’ve fixed it” posts in forums  ;)).

For anyone who wants to know a way to do it, I do the following (assuming everything else is the same as gman2’s scenario).

Instead of:

obj.x = obj.x + destx - obj.x

I would do this

obj.x = obj.x + 0.5 \* (destx - obj.x)

This will move it gradually towards the target position. Changing the 0.5 will change the speed. This also means that the object will move faster across a larger distance, and slow down as it gets nearer.

@Alan

Yes man thanks your way is a lot better than main.

But your method is need some improvements :slight_smile: