1
votes

I've been trying to create a simple game where the player controls a physics body that increases in size when it collides with other objects. From what I understand it's not possible to scale the actual physics body directly, instead I'm trying to create a new one using the parameters of the original one. However, when created a new physics body of the appropriate size has been created, I lose the ability to move it around. Any form of input would be highly appriciated, as I can't figure out what to do (I'm still very new to this). Here's what I've got so far:

function movePlayer(event)

if "began" == event.phase then
    player.isFocus = true

    player.x0 = event.x - player.x
    player.y0 = event.y - player.y
elseif player.isFocus == true then
    if "moved" == event.phase then
        player.x = event.x - player.x0
        player.y = event.y - player.y0
        stayOnScreen( player )
    elseif "ended" == phase or "cancelled" == phase then
        player.isFocus = false
    end
end

return true
end


function checks()

if player.resize == true then
    local player2 = createPlayer(player.x, player.y, player.xScale, player.yScale, player.rotation)

    if player.isFocus == true then
        player2.isFocus = player.isFocus
        player2.x0 = player.x0
        player2.y0 = player.y0
    end
    player2.resize = false
    player:removeSelf()
    player = player2
end 
end

player:addEventListener("touch", movePlayer)
Runtime:addEventListener( "enterFrame", checks)

Thanks in advance!

Edit: Here's the code that generates a new player and adds a physics body:

function createPlayer( x, y, xScale, yScale, rotation)
local player = display.newImageRect("images/p1.png", 71, 71)
player.x = x
player.y = y
player.xScale = xScale
player.yScale = yScale

local playerCollisionFilter = { categoryBits = 2, maskBits = 5 }
local playerBodyElement = { filter=playerCollisionFilter, radius = (player.xScale * (player.width *0.5)) }


player.objectType = "player"
physics.addBody ( player, "dynamic", playerBodyElement )
player.isBullet = true
player.isSleepingAllowed = false
player.rotation = rotation
player.resize = false

return player
end
2

2 Answers

1
votes

It looks like a problem with your "touch" event handler to me. Specifically, this line:

player:addEventListener("touch", movePlayer)

is only executed once, when the main Lua file is load. That handler needs to be removed from player and added to player2 when you're making the change to player2 in the checks() function.

0
votes

After you do player:removeSelf(), the physics engine will no longer have a reference to player (because the removeSelf also unregisters from physics). So you have to make player (player2) a physics body:

physics.addBody(player2, ...)

where ... represents same parameters as used when you added player the first time.

Update:

OK so you've already taken care of addBody. I can't see any obvious problem with your code, but you are using the same var name "player" in a few places so its hard to be sure. Try using event.target instead of player in handler, see if that helps. For maintainability consider naming the locals like newPlayer, a give unique name for the global like rocket or person whatever your player actually is.