Here is my movement code in my platformer engine
Bare in mind that it is rough stuff!
Function MovePlayer()
REM // User input /////////
if GetAccelerometerExists()=0
REM // On computer //
Move_Direction# = GetDirectionX()
Jump_Direction# = GetDirectionY()
else
REM // On mobile device //
Multi_Touch()
Move_Direction# = BT_Left_State + BT_Right_State
Jump_Direction# = BT_Up_State
endif
REM // Moving left //
if Move_Direction#<0.0
check=PhysicsRayCast( Player.X, Player.Y, Player.X - vdX(Player.Radius*1.6) , Player.Y )
if GetRayCastSpriteID( )<>Solids
if GetSpritePhysicsVelocityX( Player.ID )>0-40
xForce#=-0.5
REM // Make the player start moving faster in the begining //
if GetSpritePhysicsVelocityX( Player.ID )>0-25 then xForce#=xForce#*4
endif
endif
SetSpriteFlip ( Player.ID, 1, 0 )
endif
REM // Moving right //
if Move_Direction#>0.0
check=PhysicsRayCast( Player.X, Player.Y, Player.X + vdX(Player.Radius*1.6) , Player.Y )
if GetRayCastSpriteID( )<>Solids
if GetSpritePhysicsVelocityX( Player.ID )<40
xForce#=0.5
REM // Make the player start moving faster in the begining //
if GetSpritePhysicsVelocityX( Player.ID )<25 then xForce#=xForce#*4
endif
endif
SetSpriteFlip ( Player.ID, 0, 0 )
endif
if Player.Grounded>0 and Player.Jumping=0
if Jump_Direction#<>0 then if CanJump()=0 then if GetSpritePhysicsVelocityY( Player.ID )=>0-10.0
Player.Jumping=1
yForce#=0.0-90.0
PlaySound(Player.SND_Jump,80)
endif
else
if GetSoundsPlaying(Player.SND_Jump)=0 then if Player.Grounded>0
Player.Jumping=0
endif
if Player.Jumping=1 then if GetSpritePhysicsVelocityY( Player.ID )>0-45.0 then if GetSpritePhysicsVelocityY( Player.ID )<0.0
if Jump_Direction#<>0 then if CanJump()=0
Player.Jumping=2
yForce#=0.0-45.0
endif
endif
endif
REM // Animate player //////////////////////
if Player.Grounded>0 then if Player.Jumping=0
REM // If player moving animate movement //
if Move_Direction#<>0.0
if GetSpriteCurrentFrame( Player.ID )<3 then PlaySprite( Player.ID , 5, 1, 3, 6 )
Resumesprite( Player.ID)
else // Idle animation //
if GetSpriteCurrentFrame( Player.ID )>2 then PlaySprite( Player.ID , 3, 1, 1, 2 )
endif
else // Jumping animation //
StopSprite( Player.ID )
SetSpriteFrame(Player.ID,5)
endif
REM // Remove friction if on ground //
if Move_Direction#<>0
SetSpritePhysicsFriction( Player.ID , 0.0 )
else
SetSpritePhysicsFriction( Player.ID , 15.0 )
endif
REM // Make the player change direction faster with impulse force
check=PhysicsRayCast( Player.X, Player.Y, Player.X - vdX(Player.Radius*2) , Player.Y )
if Move_Direction#<0 and GetSpritePhysicsVelocityX( Player.ID )>0 then if check=0 then SetSpritePhysicsImpulse( Player.ID,Player.X, Player.Y,xForce#*8,0 )
check=PhysicsRayCast( Player.X, Player.Y, Player.X + vdX(Player.Radius*2) , Player.Y )
if Move_Direction#>0 and GetSpritePhysicsVelocityX( Player.ID )<0 then if check=0 then SetSpritePhysicsImpulse( Player.ID,Player.X, Player.Y,xForce#*8,0 )
REM // Move player
SetSpritePhysicsImpulse( Player.ID,Player.X, Player.Y,xForce#,yForce# )
Player.CollidingID=0
if GetSpriteFirstContact( Player.ID ) then Player.CollidingID = GetSpriteContactSpriteID2( )
Endfunction
Android 2.3 Gingerbread , ZTE Skate , 480x800 , 800 mhz cpu , Samsung Galaxy Y , 240x320 , 832 mhz cpu
Android 4.0 Sandwich , Dmtech 3g 9738B , 1024x768 , 9.7 inches , cortex A8 1.2 cpu , 1 gb ram.