Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import net.flashpunk.*;
- import net.flashpunk.FP;
- import net.flashpunk.Entity;
- import net.flashpunk.graphics.Image;
- import net.flashpunk.utils.Input;
- import net.flashpunk.utils.Key;
- import net.flashpunk.graphics.Spritemap;
- import flash.display.BitmapData;
- import flash.geom.Point;
- /**
- * ...
- * @author JaydenB
- */
- public class PlayerReference extends Entity
- {
- private var v:Point = new Point();
- private var a:Point = new Point();
- public var speed:Number;
- public var jumpvelocity:Number;
- public var colour:uint;
- private var doubleJump:int = 0;
- private var _gravity:Number;
- public function PlayerReference()
- {
- _gravity = GameWorld.gravity;
- a.y = _gravity;
- type = "player";
- setHitbox(16, 16);
- graphic = new Image(new BitmapData(16, 16, false, colour));
- }
- override public function update():void
- {
- var hInput:int;
- if (Input.check("left"))
- {
- hInput -= 1;
- }
- if (Input.check("right"))
- {
- hInput += 1;
- }
- if (Input.pressed("jump"))
- {
- jump();
- }
- v.x = hInput * speed;
- v.y += a.y;
- updateCollision();
- super.update();
- }
- private function updateCollision():void
- {
- x += v.x * FP.elapsed;
- if (collide("level", x, y))
- {
- if (FP.sign(v.x) > 0)
- {
- //hit left of block
- v.x = 0;
- x = Math.floor(x / GameWorld.tileSize) * GameWorld.tileSize;
- }
- else
- {
- //hit right of block
- v.x = 0;
- x = (Math.floor(x / GameWorld.tileSize) * GameWorld.tileSize) + GameWorld.tileSize;
- }
- }
- y += v.y * FP.elapsed;
- if (collide("level", x, y))
- {
- if (FP.sign(v.y) > 0)
- {
- //land on top of block
- v.y = 0;
- y = Math.floor(y / GameWorld.tileSize) * GameWorld.tileSize;
- doubleJump = 0;
- }
- else
- {
- //hit bottom of block
- v.y = 0;
- y = (Math.floor(y / GameWorld.tileSize) * GameWorld.tileSize) + GameWorld.tileSize;
- }
- }
- }
- private function jump():void
- {
- if (doubleJump < 2)
- {
- v.y = jumpvelocity;
- doubleJump++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement