Platformer
In the editor
Section titled “In the editor”- Floor and walls: check Solid on the object, or paint solid tiles.
- Player: in the Physics section, Gravity
0.25and Max fall4.5.
In code
Section titled “In code”function onUpdate(self) self.hspeed = axis("esquerda", "direita") * 1.5 if pressed("pular") and self:onGround() then self.vspeed = -4.5 end self:moveAndStop()endvoid onUpdate() override { hspeed = axis("esquerda", "direita") * 1.5f; if (pressed("pular") && onGround()) vspeed = -4.5f; moveAndStop();}moveAndStop() applies gravity and moves by hspeed/vspeed, stopping at solids. Call it once per step.
Variations
Section titled “Variations”Face the movement direction and a shorter jump on release:
if self.hspeed ~= 0 then self.flipX = self.hspeed < 0 endif released("pular") and self.vspeed < 0 then self.vspeed = self.vspeed * 0.5 endif (hspeed != 0) flipX = hspeed < 0;if (released("pular") && vspeed < 0) vspeed *= 0.5f;Enemy that patrols back and forth:
function onUpdate(self) self.hspeed = self.flipX and -0.5 or 0.5 self:moveAndStop() if self:hitWall() then self.flipX = not self.flipX endendvoid onUpdate() override { hspeed = flipX ? -0.5f : 0.5f; moveAndStop(); if (hitWall()) flipX = !flipX;}hitFloor()andhitCeiling()are true on the step the instance hit the floor or the ceiling.- If the collision box changes between animations, pick a sprite under Mask, in the object Inspector: collision then always uses its box.
- The Platformer template, in New project, has a ready level.
Reference: Movement.
