Collision Through Functions

Hi all,

I've been trying to get my game to work with collision detection and have came across a problem. I have a function that spawns a player to the screen. This function needs to be called a few times and the player respawns throughout the game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        local spawnPlayer = function()
                gameIsActive = true
                local gameChar = gameSettings["gameChar"]
                if gameChar == "player1" then
                                playerObject = display.newImage("player1.png")
                end             
                
                physics.addBody( playerObject, "kinematic", { density = 0, friction = 0, bounce = 0, } )
                
                playerObject.x = 0
                playerObject.y = 160
                playerObject.name = "player"
 
                gameGroup:insert(playerObject)
        end

You could use a Runtime eventListener instead. You have already assigned a name to the playerObject so you can use that to determine a collision.

add the collision detectors inside your spawnPlayer function right below gameGroup:insert(playerObject). That way it will only be calling those detectors when a player is actually spawned.

views:1585 update:2011/9/29 9:22:17
corona forums © 2003-2011