1. Home
  2. /
  3. Defold
  4. /
  5. Lua Script Example - Player Jump

Lua Script Example - Player Jump - Defold Typing CST Test

Loading…

Lua Script Example - Player Jump — Defold Code

Adds jumping to the player with simple gravity.

-- player_jump.script
function init(self)
	self.speed = 200
	self.vspeed = 0
	self.gravity = 500
end

function update(self, dt)
	local move = vmath.vector3()
	if go.get_action("left").pressed then move.x = move.x - 1 end
	if go.get_action("right").pressed then move.x = move.x + 1 end
	if go.get_action("jump").pressed and self.vspeed == 0 then self.vspeed = -300 end

	self.vspeed = self.vspeed + self.gravity * dt
	move.y = self.vspeed * dt
	go.set_position(go.get_position() + move * self.speed * dt)
end

Defold Language Guide

Defold is a free, cross-platform 2D game engine developed by King, designed for rapid development of high-performance 2D games. It features a lightweight editor, Lua scripting, a component-based architecture, and efficient deployment across multiple platforms.

Primary Use Cases

  • ▸2D mobile games
  • ▸Rapid prototyping and game jams
  • ▸Educational games
  • ▸Casual and puzzle games
  • ▸Performance-sensitive 2D games

Notable Features

  • ▸Lightweight editor with real-time previews
  • ▸Lua scripting for game logic
  • ▸Component-based game object system
  • ▸Integrated physics engine
  • ▸Cross-platform deployment (iOS, Android, HTML5, macOS, Windows, Linux)

Origin & Creator

Defold was created by King (the company behind Candy Crush) and released as a free engine in 2016, with full source available for developers under a permissive license.

Industrial Note

Defold is particularly popular for mobile 2D games, indie projects, game jams, and studios needing lightweight, maintainable, and efficient game engines.

More Defold Typing Exercises

Defold Lua Script Example - Simple Player MovementDefold Lua Script Example - Simple ShootingDefold Lua Script Example - Enemy Follow PlayerDefold Lua Script Example - Timer ExampleDefold Lua Script Example - Mouse Click DetectionDefold Lua Script Example - Display FPSDefold Lua Script Example - Random MovementDefold Lua Script Example - Simple AnimationDefold Lua Script Example - Collision Detection

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher