Permalink
Browse files

Changed physics to fixed step and added a complete example

  • Loading branch information...
1 parent 93fff4e commit a55cb6e14a25d80cc9af749d36765a1407948bab unknown committed Feb 18, 2011
View
12 Assets.as
@@ -0,0 +1,12 @@
+package
+{
+
+ public class Assets
+ {
+
+ [Embed(source = 'assets/character.png')] public static const GfxCharacter:Class;
+ [Embed(source = 'assets/block.png')] public static const GfxBlock:Class;
+
+ }
+
+}
View
18 Main.as
@@ -0,0 +1,18 @@
+package
+{
+ import net.flashpunk.Engine;
+ import net.flashpunk.FP;
+ import com.matttuttle.GameWorld;
+
+ public class Main extends Engine
+ {
+
+ public function Main()
+ {
+ super(480, 260, 30, true);
+ FP.world = new GameWorld();
+ }
+
+ }
+
+}
View
BIN assets/block.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN assets/character.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
24 com/matttuttle/Block.as
@@ -0,0 +1,24 @@
+package com.matttuttle
+{
+
+ import net.flashpunk.*;
+ import net.flashpunk.graphics.*;
+
+ public class Block extends Entity
+ {
+
+ public function Block(x:int, y:int)
+ {
+ this.x = x;
+ this.y = y;
+
+ graphic = new Image(Assets.GfxBlock);
+ setHitbox(32, 32);
+
+ // This block is collidable with a physics character
+ type = "solid";
+ }
+
+ }
+
+}
View
70 com/matttuttle/Character.as
@@ -1,25 +1,40 @@
package com.matttuttle
{
+ import net.flashpunk.*;
+ import net.flashpunk.graphics.*;
+ import net.flashpunk.utils.*;
+
// Example character class using simple physics
public class Character extends PhysicsEntity
{
- private static const kMoveSpeed:uint = 60;
- private static const kJumpSpeed:uint = 600;
+ private var sprite:Spritemap = new Spritemap(Assets.GfxCharacter, 32, 32);
+
+ private static const kMoveSpeed:uint = 5;
+ private static const kJumpForce:uint = 25;
- public function Character(_x:int, _y:int)
+ public function Character(x:int, y:int)
{
- x = _x;
- y = _y;
+ this.x = x;
+ this.y = y;
+
+ sprite.add("right_idle", [0, 0, 0, 1], 0.1, true);
+ sprite.add("right_walk", [2, 3, 4], 0.25, true);
+ sprite.add("right_jump", [5]);
+
+ sprite.add("left_idle", [6, 6, 6, 7], 0.1, true);
+ sprite.add("left_walk", [8, 9, 10], 0.25, true);
+ sprite.add("left_jump", [11]);
- // Set hitbox and sprite here
+ graphic = sprite;
+ setHitbox(32, 32);
// Set physics properties
- gravity.y = 40;
- maxVelocity.y = 600;
- maxVelocity.x = 400;
- drag.x = 25;
+ gravity.y = 2.6;
+ maxVelocity.y = kJumpForce;
+ maxVelocity.x = kMoveSpeed * 2;
+ friction.x = friction.y = 1.6;
// Define input keys
Input.define("left", Key.A, Key.LEFT);
@@ -38,9 +53,42 @@ package com.matttuttle
acceleration.x = kMoveSpeed;
if (Input.pressed("jump") && onGround)
- velocity.y = -kJumpSpeed;
+ acceleration.y = -kJumpForce;
+
+ // Make animation changes here
+ setAnimation();
super.update();
+
+ // Always face the direction we were last heading
+ if (velocity.x < 0)
+ facing = LEFT;
+ else if (velocity.x > 0)
+ facing = RIGHT;
+ }
+
+ private function setAnimation():void
+ {
+ var animation:String;
+
+ if (facing == LEFT)
+ animation = "left_";
+ else
+ animation = "right_";
+
+ if (onGround)
+ {
+ if (velocity.x == 0)
+ animation += "idle";
+ else
+ animation += "walk";
+ }
+ else
+ {
+ animation += "jump";
+ }
+
+ sprite.play(animation);
}
}
View
23 com/matttuttle/GameWorld.as
@@ -0,0 +1,23 @@
+package com.matttuttle
+{
+
+ import flash.text.engine.TextBlock;
+ import net.flashpunk.*;
+
+ public class GameWorld extends World
+ {
+
+ public function GameWorld()
+ {
+ add(new Character(0, 0));
+
+ // Fill with blocks
+ for (var i:int = 0; i < FP.screen.width / 32; i++)
+ {
+ add(new Block(i * 32, FP.screen.height - 32));
+ }
+ }
+
+ }
+
+}
View
48 com/matttuttle/PhysicsEntity.as
@@ -15,13 +15,12 @@ package com.matttuttle
// Define variables
public var velocity:Point = new Point(0, 0);
public var acceleration:Point = new Point(0, 0);
- public var drag:Point = new Point(0, 0);
+ public var friction:Point = new Point(0, 0);
public var maxVelocity:Point = new Point(0, 0);
public var gravity:Point = new Point(0, 0);
public var onGround:Boolean = false;
- public var onWall:Boolean = false;
- public var facing:uint = LEFT;
+ public var facing:uint = RIGHT;
public var solid:String = "solid";
public function PhysicsEntity()
@@ -30,19 +29,22 @@ package com.matttuttle
override public function update():void
{
+ applyAcceleration();
+ applyVelocity();
+
onGround = false;
if (collide(solid, x, y + 1))
onGround = true;
applyGravity();
- applyAcceleration();
- applyDrag();
- applyVelocity();
+ applyFriction();
+ checkMaxVelocity();
super.update();
}
public function applyGravity():void
{
+ //increase velocity based on gravity
velocity.x += gravity.x;
velocity.y += gravity.y;
}
@@ -81,51 +83,53 @@ package com.matttuttle
}
}
- private function applyDrag():void
+ private function applyFriction():void
{
+ // If we're on the ground, apply friction
if (onGround)
{
- var d:Number = drag.x * FP.elapsed;
- if (velocity.x - d > 0)
+ if (velocity.x > 0)
{
- velocity.x -= d;
+ velocity.x -= friction.x;
+ if (velocity.x < 0)
+ {
+ velocity.x = 0;
+ }
}
- else if (velocity.x + d < 0)
+ if (velocity.x < 0)
{
- velocity.x += d;
- }
- else
- {
- velocity.x = 0;
+ velocity.x += friction.x;
+ if (velocity.x > 0)
+ {
+ velocity.x = 0;
+ }
}
}
-
- // Apply Y drag??
}
private function applyVelocity():void
{
var i:int;
- checkMaxVelocity();
-
- for (i = 0; i < Math.abs(velocity.x * FP.elapsed); i++)
+ for (i = 0; i < Math.abs(velocity.x); i++)
{
if (collide(solid, x + FP.sign(velocity.x), y))
{
velocity.x = 0;
+ break;
}
else
{
x += FP.sign(velocity.x);
}
}
- for (i = 0; i < Math.abs(velocity.y * FP.elapsed); i++)
+ for (i = 0; i < Math.abs(velocity.y); i++)
{
if (collide(solid, x, y + FP.sign(velocity.y)))
{
velocity.y = 0;
+ break;
}
else
{

0 comments on commit a55cb6e

Please sign in to comment.