So I have a question about using different physics bodies based on the image of my character based on directional movement. Right now, I can only add a new physics outline when I remove the first one, and the ones I need for the other animations never actually transition. When I try to remove the physics body and add a new one to an animation, it does it constantly and not based on the movement of the character. I’ve tried a loop but it causes the game to freeze. Is there a way to change the physics bodies based on the direction and animation that I am on using the graphics.newOutline() with physics? Or should I use one of the two other options, which are; use the physics editor from code and web for each image in the sprite sheet, or use something like an oval for the physics body for all images? Here is the code that I am trying to use listed below:
--sprite sheets local SheetInfoforward = { width = 68, height = 88, numFrames = 2, sheetContentWidth = 143, sheetContentHeight = 93} local birdsheet = graphics.newImageSheet("bird@1x.png", SheetInfoforward) local SheetInfobackwards = { width = 67, height = 86, numFrames = 3, sheetContentWidth = 213, sheetContentHeight = 90} local birdsheet2 = graphics.newImageSheet("birdBack.png", SheetInfobackwards) local SheetInfoup = { width = 66, height = 101, numFrames = 3, sheetContentWidth = 201, sheetContentHeight = 101} local birdsheet3 = graphics.newImageSheet("birdUp.png", SheetInfoup) local SheetInfodown = { width = 55, height = 83, numFrames = 3, sheetContentWidth = 59, sheetContentHeight = 257} local birdsheet4 = graphics.newImageSheet("birdDown.png", SheetInfodown) local sequenceData2 = {{ name="moving", sheet = birdsheet, start=1, count=2, time=800, loopCount=0 }, { name="moving2", sheet =birdsheet2, start=1, count=3, time=800, loopCount=0 }, { name="moving3", sheet =birdsheet3, start=1, count=3, time=800, loopCount=0 }, { name="moving4", sheet =birdsheet4, start=1, count=3, time=800, loopCount=0 }} --display the sprite sheets local bird1 = display.newSprite( birdsheet, sequenceData2 ) bird1.x = display.contentWidth/2 ; bird1.y = display.contentHeight/2 physics.addBody( bird1, "dynamic", { density=0, friction=0.0, bounce=0.0 } ) bird1:play() bird1.type = "player" -- Listens for the Analog stick function moveAnalog(e) if e.phase == "began" then MyStick.x = e.x MyStick.y = e.y e.target = MyStick.Thumb e.phase = "began" MyStick.onDrag(e) elseif e.phase == "moved" then timer.createTimer(0, transition.cancel(tapAnalog)) end end Runtime:addEventListener("touch", moveAnalog) physics.setDrawMode( "hybrid" ) -- Moves bird around the level function main( event ) -- MOVE THE SHIP MyStick:move(bird1, 8.0, false) --Removes physics from bird physics.removeBody(bird1) --depending on direction bird changes sprite sheetr and physicsoutline if MyStick:getAngle()\>= 45 and MyStick:getAngle()\<= 135 then local letterSheet = graphics.newImageSheet( "bird@1x.png", SheetInfoforward ) local frameIndex = 1 local letterOutline = graphics.newOutline( 3, letterSheet, frameIndex ) physics.addBody( bird1, "dynamic", { outline=letterOutline, density=0, friction=0.0, bounce=0.0 } ) bird1.isFixedRotation = true bird1:setSequence( "moving") bird1:play() elseif MyStick:getAngle()\>= 225 and MyStick:getAngle()\<= 315 then local letterSheet2 = graphics.newImageSheet( "birdBack.png", SheetInfobackwards ) local frameIndex2 = 1 local letterOutline2 = graphics.newOutline( 3, letterSheet2, frameIndex2 ) physics.addBody( bird1, "dynamic", { outline=letterOutline2, density=0, friction=0.0, bounce=0.0 } ) bird1.isFixedRotation = true bird1:setSequence( "moving2") bird1:play() elseif MyStick:getAngle()\> 315 and MyStick:getAngle()\<= 360 or MyStick:getAngle()\> 1 and MyStick:getAngle()\< 45 then local letterSheet3 = graphics.newImageSheet( "birdUp.png", SheetInfoup ) local frameIndex3 = 1 local letterOutline3 = graphics.newOutline( 3, letterSheet3, frameIndex3 ) physics.addBody( bird1, "dynamic", { outline=letterOutline3,density=0, friction=0.0, bounce=0.0 } ) bird1.isFixedRotation = true bird1:setSequence( "moving3") bird1:play() elseif MyStick:getAngle()\> 135 and MyStick:getAngle()\< 225 then local letterSheet4 = graphics.newImageSheet( "birdDown.png", SheetInfodown ) local frameIndex4 = 1 local letterOutline4 = graphics.newOutline( 3, letterSheet4, frameIndex4 ) physics.addBody( bird1, "dynamic", {outline=letterOutline4, density=0, friction=0.0, bounce=0.0 } ) bird1.isFixedRotation = true bird1:setSequence( "moving4") bird1:play() end end Runtime:addEventListener( "enterFrame", main )
Thanks for reading!