[RESOLVED]Player sprite movement fails - using Levelhelper with Corona SDK

Hello,

I am using Levelhelper and Corona SDK to help me learn and understand programming.

My problem is not being able to move my player sprite using force instead of simple x/y displacement. This in order to use the physics set up with Levelhelper/Spritehelper. Can anyone have a look at the code and help point me in the right direction to make my player sprite move by tapping the buttons/point out what I am doing wrong in the code?

There are 4 buttons, currently only one of them is set up to move the player by force, the other 3 simply displace the player sprite.

Please see the code below:

module(…, package.seeall)


– IMPORTS

local ui = require (“ui”)
local physics = require(“physics”)
physics.start()
physics.setDrawMode( “hybrid” )
display.setStatusBar( display.HiddenStatusBar )

require “LevelHelperLoader”
system.activate( “multitouch” )

local localGroup = display.newGroup()


–Loads level made in Levelhelper

loader = LevelHelperLoader:initWithContentOfFile(“Scenes/room1.plhs”)
loader:instantiateObjectsInGroup(physics, localGroup)

–Loads player sprite made in Levelhelper

local player = loader:spriteWithUniqueName(“player”)


–Player vs wall collision detection

local touchWall = true

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

if(event.other.tag == LevelHelper_TAG.GROUND) then
touchWall = true
end

–print( self.tag … ": collision began with " … event.other.tag )

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

–print( self.tag … ": collision ended with " … event.other.tag )

end
end

player.collision = onLocalCollision
player:addEventListener( “collision”, player )


local displaceX = -32
local displaceY = 64


–Player movement buttons

local onButtonLeftEvent = function(event)
Player:applyLinearImpulse( -20, 0, Player.x, Player.y )
– player.x = player.x - 32
end

local onButtonRightEvent = function(event)
player.x = player.x + 32
end

local onButtonUpEvent = function(event)
player.y = player.y - 32
end

local onButtonDownEvent = function(event)
player.y = player.y + 32
end


–Player movement buttons, graphics

local buttonLeft = ui.newButton{
default = “Images/left.png”,
over = “Images/left.png”,
onEvent = onButtonLeftEvent
}

local buttonRight = ui.newButton{
default = “Images/right.png”,
over = “Images/right.png”,
onEvent = onButtonRightEvent
}

local buttonUp = ui.newButton{
default = “Images/up.png”,
over = “Images/up.png”,
onEvent = onButtonUpEvent
}

local buttonDown = ui.newButton{
default = “Images/down.png”,
over = “Images/down.png”,
onEvent = onButtonDownEvent
}

local onUpdate = function(event)
end

Runtime:addEventListener(“enterFrame”, onUpdate)




–local touchListener = function( event )
–end
–Runtime:addEventListener( “touch”, touchListener )


local function performCleanUp()

–Runtime:removeEventListener(“enterFrame”, onUpdate)
–Runtime:removeEventListener(“touch”, touchListener)

end

local function initVars ()


– Inserts

localGroup:insert(player)

localGroup:insert(buttonUp)
localGroup:insert(buttonDown)
localGroup:insert(buttonRight)
localGroup:insert(buttonLeft)

player.x = 480
player.y = 448

buttonLeft.x = displaceX + 80
buttonLeft.y = displaceY + 586

buttonRight.x = displaceX + 221
buttonRight.y = displaceY + 586

buttonDown.x = displaceX + 150
buttonDown.y = displaceY + 650

buttonUp.x = displaceX + 150
buttonUp.y = displaceY + 520
end


– CLEAN

function clean ( event )

end


– NEW

function new()


– Initiate variables

initVars()


– MUST return a display.newGroup()

return localGroup

end [import]uid: 72520 topic_id: 23067 reply_id: 323067[/import]

Check in the properties of your sprite that it is not a static object. I know levelhelper / spritehelper has static set as the default. Set the object type to dynamic. [import]uid: 118687 topic_id: 23067 reply_id: 92320[/import]

It was the player sprite property that was the cause of the error. I had set it to kinematic instead of dynamic. Now it works as it should. :slight_smile: [import]uid: 72520 topic_id: 23067 reply_id: 92363[/import]