Hey, I am currently building a game for some school work (for Wednesday!) and I have run into a problem. I can’t get the player to move. I just cant work out why it won’t move. When I click to drag it around the screen in the simulator it just crashes with an error ‘compare to nil’ I am trying to get the player to move around in the boundaries of the screen by being dragged around by the player.
Here is my code:
[lua]
local background
local timeLastEnemy = 0
local player
local topside
local bottomside
local enemy
local diffValue = 3000
local score = 0
local scoreText
– create a grey rectangle as the backdrop
local background = display.newRect( 0, 0, screenW, screenH )
background:setFillColor( 24,64,64 )
– Load and position the player
player = display.newImage(“crate.png” , 60 , 60)
player.y = display.contentCenterY
player.x = display.contentHeight - player.contentHeight
–physics.addBody(player, “kinematic”, {bounce = 0})
physics.addBody(player, “static”, {density = 2.0, friction = 0.5, bounce=0.3})
player.name = “player”
– Listen to collisions
player.collision = onCollision
player:addEventListener(“collision”, player)
–gameLayer:insert(player)
function scene:createScene( event )
local group = self.view
local topside = display.newImageRect( “crate.png”, 500, 0 )
topside.x = 250
topside.y = -1
physics.addBody(topside, {friction=0.5})
topside.bodyType = “static”
topside.myName = “topside”
local bottomside = display.newImageRect( “crate.png”, 500, 0 )
bottomside.x = 250
bottomside.y = 320
physics.addBody(bottomside, {friction=0.5})
bottomside.bodyType = “static”
bottomside.myName = “bottomside”
local timeLastEnemy = 0
function loop ( event )
if event.time - timeLastEnemy >= math.random(diffValue, 5000) then
– make a enemy (off-screen), position it, and rotate slightly
local enemy = display.newImageRect( “crate.png”, 30, 30 )
enemy.x, enemy.y = 550, math.random(20, 300)
enemy.rotation = math.random (-90, 90)
timeLastEnemy = event.time
diffValue = diffValue - 5
print(diffValue)
– add physics to the enemy
physics.addBody( enemy, { density=1.0, friction=0.3, bounce=0.3 } )
– all display objects must be inserted into group
– gameLayer:insert(player)
– group:insert( background )
– group:insert( crate )
end
end
local function playerMovement(event)
– Only move to the screen boundaries
if event.x >= halfPlayerWidth and event.x <= display.contentWidth - halfPlayerWidth then
– Update player x axis
player.x = event.x
if event.y >= halfPlayerWidth and event.y <= display.contentWidth - halfPlayerWidth then
– Update player y axis
player.y = event.y
end
end
end
– Player will listen to touches
player:addEventListener(“touch”, playerMovement)
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
physics.setGravity( -1, 0 )
end
Runtime:addEventListener(“enterFrame”, loop)
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
[/lua]
I am just using place holder images at the moment while I work it all out. I managed to get collision working and the spawn rate gradually increases throughout the game. Once I get the player movement sorted I am going to have it so when an enemy gets past the player it will end the game.
Thanks
EDIT:
I managed to solve this issue (see below) but it has left me with another. I am unsure how to restrict it to just the screen boundaries. Any idea how to do this?
here is the code I adjusted to solve the movement problem
[lua]
local function playerMovement(event)
player.x = event.x
player.y = event.y
end
[/lua] and then I have to put the listener in the game loop. :)
