Here is some very basic code (main.lua) for letting a player jump through a platform and land on it.
The process is based on turning the platform into a sensor during the time the player is on its way through the platform, and then turn the platform solid again, letting the physics engine take care of the rest.
You make the player jump by pulling down and letting go.
Again this is very basic code, but I hope it will be of some help.
[lua]-- main.lua
– 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, 9.8);
physics.setPositionIterations(16);
physics.setVelocityIterations(6);
– Setup the background group
local gameGroup = display.newGroup();
gameGroup.x = 0;
– Ground object
local ground = display.newRect(0, 0, 320, 5);
ground.x = 160;
ground.y = 470;
ground.objectType = “ground”
groundPhysics = { -160, 0, 160, 0, 160, 10, -160, 10 };
physics.addBody(ground, “static”, { density=1.0, friction=0.0, bounce=0.0, shape=groundPhysics });
gameGroup:insert(ground);
– Setup a sprite for the player
playerInstance = display.newRect(0, 0, 32, 32);
playerInstance.x = 100;
playerInstance.y = 400;
playerInstance.rotation = 0;
gameGroup:insert(playerInstance);
physics.addBody(playerInstance, “dynamic”, { density=0.25, friction=0.15, bounce=0.0 });
playerInstance.isFixedRotation = true;
playerInstance.objectType = “player”;
– Setup some vars on the playerInstance
playerInstance.isFalling = false;
playerInstance.xVel = 0;
playerInstance.yVel = 0;
– Test jump platform
local jumpPlatform = display.newRect(80, 200, 100, 50);
jumpPlatform.objectType = “jumpPlatform”
gameGroup:insert(jumpPlatform);
physics.addBody(jumpPlatform, “static”, { friction=0.3} );
– The main loop
function onEnterFrame(event)
– 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 yForce = (event.y - playerInstance.y) * -0.1;
playerInstance:applyLinearImpulse( 0, yForce, playerInstance.x, playerInstance.y);
display.getCurrentStage():setFocus(nil);
t.isFocus = false;
end
end
end
local function onPreCollision(self, event)
local player = self;
local collidedWith = event.other;
if (collidedWith.objectType == “ground”) then
return true; – we don’t care about the ground
end
–check to see if player is above the platform
if (player.y < collidedWith.y) then
return true; – player should remain on platform
end
print(“preCollision”);
–set player to sensor while travelling through object
player.isSensor = true;
end
local function onCollision(self, event)
local player = self;
local collidedWith = event.other;
local phase = event.phase;
if (collidedWith.objectType == “ground”) then
return true; – we don’t care about the ground
end
if (phase == “began”) then
print(collidedWith.objectType…" began")
elseif (phase == “ended”) then
print(collidedWith.objectType…" end")
player.isSensor = false;
end
end
– Setup the event listeners
playerInstance.collision = onCollision;
playerInstance.preCollision = onPreCollision;
playerInstance:addEventListener(“collision”, playerInstance);
playerInstance:addEventListener(“preCollision”, playerInstance);
Runtime:addEventListener(“enterFrame”, onEnterFrame);
playerInstance:addEventListener(“touch”, onTouch);[/lua]
PS. The basis for this code was posted by another developer on this forum who had difficulty with platform logic that I helped out with and added to it.
[import]uid: 70847 topic_id: 19557 reply_id: 75503[/import]