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