Hi all!
I'm having a little problem with some code that is bugging me. This is a very quick and dirty prototype for a game I want to try - keep in mind that I haven't gotten very far yet. But i've hit this hurdle. I have a player object that can be shot into the air (using a drag gesture) - to keep it onscreen I check to see if the player object has gone past a threshold in the game world group, and then move the game world group according to keep the player onscreen.
Seems to work okay - except! When the player returns to the ground the Y position of the game group is different every time. Only by a bit, but it is quite noticeable. can anyone see where the inaccuracy is coming into it? The methodology used here is based off the egg breaker demo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | -- Setup the game -- Turn off the status bar display.setStatusBar(display.HiddenStatusBar); -- Setup graphics local physics = require("physics"); physics.start(); physics.setDrawMode("hybrid"); physics.setGravity(0, 11); -- Setup background graphics local sky = display.newImage("sky.png", true); sky = 160; sky = 240; -- Setup the background group local gameGroup = display.newGroup(); gameGroup.x = 0; -- Ground object local ground = display.newImage("ground.png", true); ground.x = 160; ground.y = 440; groundPhysics = { -160, 0, 160, 0, 160, 10, -160, 10 }; physics.addBody(ground, "static", { friction=0.3, bounce=0.0, shape=groundPhysics }); gameGroup:insert(ground); -- Setup a sprite for the player local sprite = require("sprite"); local spriteSheet1 = sprite.newSpriteSheet("starcatSpriteSheet.png", 64, 64); local spriteSet1 = sprite.newSpriteSet(spriteSheet1, 1, 1); sprite.add(spriteSet1, "sitting", 1, 1, 1000, 0); local playerInstance = sprite.newSprite(spriteSet1); playerInstance:prepare("sitting"); playerInstance:play(); playerInstance.x = 100; playerInstance.y = 400; playerInstance.rotation = 0; playerPhysics = { -8, 0, 12, 0, 12, 32, -8, 32 }; gameGroup:insert(playerInstance); physics.addBody(playerInstance, "dynamic", { density=.5, friction=0.15, bounce=0.0, shape = playerPhysics }); playerInstance.isFixedRotation = true; -- Setup some vars on the playerInstance playerInstance.isFalling = false; playerInstance.xVel = 0; playerInstance.yVel = 0; -- Debug variables local debugTextField1 = display.newText(" DEBUG: ", 50, 50, native.systemFont, 12); debugTextField1:setTextColor(255, 255, 255); debugTextField1.x = 100; debugTextField1.y = 10; local debugTextField2 = display.newText(" DEBUG: ", 50, 50, native.systemFont, 12); debugTextField2:setTextColor(255, 255, 255); debugTextField2.x = 100; debugTextField2.y = 30; local debugTextField3 = display.newText("", 50, 50, native.systemFont, 12); debugTextField3:setTextColor(255, 255, 255); debugTextField3.x = 100; debugTextField3.y = 50; -- The main loop function onEnterFrame(event) -- Get the players linear velocity playerInstance.xVel, playerInstance.yVel = playerInstance:getLinearVelocity(); if playerInstance.yVel > 0 then playerInstance.isFalling = true; else playerInstance.isFalling = false; end -- Move the game world to keep the player onscreen if playerInstance.y <= 200 then gameGroup.y = -playerInstance.y + 200; end end -- When the player makes a tap event function onTouch(event) local t = event.target; local phase = event.phase; if phase == "began" then -- Make the object the topmost object local parent = t.parent; parent:insert(t); display:getCurrentStage():setFocus(t); -- Prevents spurious messages being sent t.isFocus = true; -- Store the initial position t.x0 = event.x - t.x; t.y0 = event.y - t.y; elseif t.isFocus == true then if phase == "moved" then elseif phase == "ended" or phase == "cancelled" then local xForce = (-1 * (event.x - playerInstance.x)) * 2.75; local yForce = (-1 * (event.y - playerInstance.y)) * 4; playerInstance:applyForce( 0, yForce, playerInstance.x, playerInstance.y); display.getCurrentStage():setFocus(nil); t.isFocus = false; end end end -- Setup the event listeners Runtime:addEventListener("enterFrame", onEnterFrame); playerInstance:addEventListener("touch", onTouch); |