Before I describe my problem, I would like to apologize for the vague title. I couldn’t think of how to describe my problem briefly.
Anyway…
I have a game that involves creating a circle object, inserting it into a map loaded with ponytiled, and adding it to the physics engine. When the object touches an obstacle, the object is destroyed and an overlay is shown that allows the player to modify certain aspects of the game. Then, another, identical object is put in the place of the original when the overlay is hidden. The problem I am having is that whenever the second object comes along, it moves ever so slightly before it begins to be affected by the physics engine (normally it shifts up).
Normally, I wouldn’t be bugged by this as the movement is slight. However, I have noticed that this leads to some unpredictable behavior on certain levels. Due to the nature of the game, it would not work for me to reuse the same object; it must be a new one each time.
This should be all the applicable code:
In scene1:
local playerLib = require "libs.player" local player = playerLib.player --creates player object function scene:create( event ) sceneGroup = self.view physics.start() physics.pause() local mapData = require ("maps.level".. composer.getVariable("levelNumber")) map = tiled.new(mapData, "maps") start = map:findObject("start") player.circle = display.newCircle(start.x, start.y, 10) --non applicable code here sceneGroup:insert(map) sceneGroup:insert(player.circle) end local function reset() physics.removeBody(player.circle) player.circle.x = start.x player.circle.y = start.y timer.performWithDelay(30, goToScene2()) --transitions to scene2 end local function onObstacleCollision(event) --non applicable code here timer.performWithDelay( 10, reset ) end local function scene:show(event) goToScene2() end
scene2:
function scene:hide( event ) local sceneGroup = self.view local parent = event.parent player.circle = display.newCircle(start.x, start.y, 10) parent.view:insert(player.circle) physics.addBody(player.circle, { density=1, friction=0, bounce=player.bounce }) physics.setGravity(player.Xgravity, player.Ygravity) physics.start() end
I’m not sure what the problem could be. Any thoughts? If something in my code doesn’t make sense, just let me know and I will do my best to clarify.
Thanks