Skip to content

Commit 130040f

Browse files
authored
simplify jumping, add variable jumping (#49)
1 parent 2a327a8 commit 130040f

File tree

6 files changed

+153
-57
lines changed

6 files changed

+153
-57
lines changed

_proofs/movement/jumping.md

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,17 @@ complexity: 2
66
seealso: [Collision, Movement/Velocity, Movement/Acceleration]
77
tags: [movement, jumping, game mechanic]
88
---
9-
There are many ways to implement a jumping mechanic, but the simplest method is to add negative <var>y</var> {% api flixel.FlxObject.velocity %} temporarily to a {% api flixel.FlxObject %} with positive <var>y</var> {% api flixel.FlxObject.acceleration %}, using some kind of timer to limit the jump time.
9+
There are many ways to implement a jumping mechanic, but the simplest method is to add negative <var>y</var> {% api flixel.FlxObject.velocity %} temporarily to a {% api flixel.FlxObject %} with positive <var>y</var> {% api flixel.FlxObject.acceleration %}.
1010

1111

1212
```haxe
13-
FlxG.collide(box, sprite);
14-
15-
var jumpPressed:Bool = pad.buttonA.pressed;
16-
17-
if (jumping && !jumpPressed)
18-
jumping = false;
13+
// call super.update first, as it sets touching flags to NONE
14+
super.update(elapsed);
1915
20-
/*
21-
* Reset jumpTimer when touching the floor
22-
* Note: This sprite's touching flags are set via FlxG.collide,
23-
* and are reset to false on super.update
24-
*/
25-
if (sprite.isTouching(DOWN) && !jumping)
26-
jumpTimer = 0;
27-
28-
if (jumpTimer >= 0 && jumpPressed)
29-
{
30-
jumping = true;
31-
jumpTimer += elapsed;
32-
}
33-
else
34-
jumpTimer = -1;
16+
// If sprite is resting on box it will set its touching to DOWN
17+
FlxG.collide(box, sprite);
3518
36-
// hold button to jump higher (up to 0.25s)
37-
if (jumpTimer > 0 && jumpTimer < 0.25)
19+
// jump if touching the box, and jump is pressed
20+
if (sprite.isTouching(DOWN) && !pad.buttonA.pressed)
3821
sprite.velocity.y = -300;
3922
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Variable Jumping
3+
concept: Movement
4+
order: 5
5+
complexity: 2
6+
seealso: [Collision, Movement/Velocity, Movement/Jumping]
7+
tags: [movement, jumping, game mechanic]
8+
---
9+
A more advanced jumping technique is to allow the object to jump higher if the button held, there's multiple ways to achieve this, but one common way it maintain the jump velocity for a brief time, so long as the button is held.
10+
11+
```haxe
12+
// call super.update first, as it sets touching flags to NONE
13+
super.update(elapsed);
14+
15+
// If sprite is resting on box it will set its touching to DOWN
16+
FlxG.collide(box, sprite);
17+
18+
// Reset jumpTimer when touching the floor and jump is pressed
19+
if (sprite.isTouching(DOWN) && pad.buttonA.justPressed)
20+
{
21+
jumpTimer = 0;
22+
}
23+
24+
// hold button to jump higher (up to 0.25s)
25+
if (pad.buttonA.pressed)
26+
{
27+
jumpTimer += elapsed;
28+
29+
if (jumpTimer < 0.25)
30+
{
31+
sprite.velocity.y = -300;
32+
}
33+
}
34+
else
35+
{
36+
// stop jumping after they release
37+
jumpTimer = 0.25;
38+
}
39+
```

demos/movement/jumping/source/PlayState.hx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class PlayState extends FlxState
1111
var box:FlxSprite;
1212
var pad:FlxVirtualPad;
1313

14-
var jumpTimer:Float = 0;
15-
var jumping:Bool = false;
16-
1714
override function create()
1815
{
1916
super.create();
@@ -36,41 +33,14 @@ class PlayState extends FlxState
3633

3734
override function update(elapsed:Float)
3835
{
39-
// call super.update first, because it will set touching flags to NONE
36+
// call super.update first, as it sets touching flags to NONE
4037
super.update(elapsed);
4138

4239
// If sprite is resting on box it will set its touching to DOWN
4340
FlxG.collide(box, sprite);
4441

45-
var jumpPressed:Bool = pad.buttonA.pressed;
46-
47-
if (jumping && !jumpPressed)
48-
{
49-
jumping = false;
50-
}
51-
52-
/*
53-
* Reset jumpTimer when touching the floor
54-
* Note: This sprite's touching flags are set via FlxG.collide,
55-
* and are reset to false on super.update
56-
*/
57-
if (sprite.isTouching(DOWN) && !jumping)
58-
{
59-
jumpTimer = 0;
60-
}
61-
62-
if (jumpTimer >= 0 && jumpPressed)
63-
{
64-
jumping = true;
65-
jumpTimer += elapsed;
66-
}
67-
else
68-
{
69-
jumpTimer = -1;
70-
}
71-
72-
// hold button to jump higher (up to 0.25s)
73-
if (jumpTimer > 0 && jumpTimer < 0.25)
42+
// jump if touching the box, and jump is pressed
43+
if (sprite.isTouching(DOWN) && pad.buttonA.justPressed)
7444
{
7545
sprite.velocity.y = -300;
7646
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project xmlns="http://lime.software/project/1.0.2"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://lime.software/project/1.0.2 http://lime.software/xsd/project-1.0.2.xsd">
4+
5+
<app title="variable-jumping" file="variable-jumping" main="Main" version="0.0.1" company="HaxeFlixel" />
6+
<set name="SWF_VERSION" value="11.8" />
7+
<window width="320" height="240" fps="60" hardware="true" vsync="false" />
8+
<window if="desktop" orientation="landscape" fullscreen="false" resizable="true" />
9+
<window if="mobile" orientation="landscape" fullscreen="true" width="0" height="0" />
10+
<window background="null" if="html5" />
11+
<set name="BUILD_DIR" value="export" />
12+
<source path="source" />
13+
<source path="../../shared/" />
14+
<assets path="../../demo-assets" rename="assets" />
15+
<haxelib name="flixel"/>
16+
<!--haxelib name="flixel-addons" /-->
17+
<!--haxelib name="flixel-ui" /-->
18+
<haxedef name="FLX_NO_TOUCH" unless="mobile" />
19+
<haxedef name="FLX_NO_GAMEPAD" />
20+
<haxedef name="FLX_NO_FOCUS_LOST_SCREEN" />
21+
<haxedef name="FLX_NO_DEBUG" unless="debug" />
22+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package;
2+
3+
import flixel.FlxSnippet;
4+
import openfl.display.Sprite;
5+
6+
class Main extends Sprite
7+
{
8+
public function new()
9+
{
10+
super();
11+
addChild(new FlxSnippet(0, 0, PlayState));
12+
}
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package;
2+
3+
import flixel.FlxG;
4+
import flixel.FlxSprite;
5+
import flixel.FlxState;
6+
import flixel.ui.FlxVirtualPad;
7+
8+
class PlayState extends FlxState
9+
{
10+
static inline var JUMP_TIME = 0.25;
11+
12+
var sprite:FlxSprite;
13+
var box:FlxSprite;
14+
15+
var pad:FlxVirtualPad;
16+
17+
var jumpTimer:Float = 0;
18+
19+
override function create()
20+
{
21+
super.create();
22+
23+
box = new FlxSprite("assets/bigbox.png");
24+
box.screenCenter(X);
25+
box.y = FlxG.height * 0.75 - box.height / 2;
26+
box.immovable = true;
27+
add(box);
28+
29+
sprite = new FlxSprite("assets/sprite.png");
30+
sprite.screenCenter(X);
31+
sprite.acceleration.y = 900;
32+
sprite.maxVelocity.y = 300;
33+
add(sprite);
34+
35+
pad = new FlxVirtualPad(FlxDPadMode.NONE, FlxActionMode.A);
36+
add(pad);
37+
}
38+
39+
override function update(elapsed:Float)
40+
{
41+
// call super.update first, as it sets touching flags to NONE
42+
super.update(elapsed);
43+
44+
// If sprite is resting on box it will set its touching to DOWN
45+
FlxG.collide(box, sprite);
46+
47+
// Reset jumpTimer when touching the floor and jump is pressed
48+
if (sprite.isTouching(DOWN) && pad.buttonA.justPressed)
49+
{
50+
jumpTimer = 0;
51+
}
52+
53+
// hold button to jump higher (up to 0.25s)
54+
if (pad.buttonA.pressed)
55+
{
56+
jumpTimer += elapsed;
57+
58+
if (jumpTimer < JUMP_TIME)
59+
{
60+
sprite.velocity.y = -300;
61+
}
62+
}
63+
else
64+
{
65+
// stop jumping after they release
66+
jumpTimer = JUMP_TIME;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)