Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override public function update():void
- {
- super.update();
- // For movement direction
- var movement:Point = new Point;
- /**
- * Vertical movement
- */
- var tempY:Number = y;
- // Check ladder collision (to climb up/down)
- if (this.collide(Ladder.TYPE, x, y+1) || this.collide(Ladder.TYPE, x, y)) {
- if (Input.check(Key.UP)) movement.y--;
- if (Input.check(Key.DOWN)) movement.y++;
- v.y = SPEED_X * movement.y;
- // Update Cordinate Y
- tempY += v.y;
- // align to ladder after climbing up
- if (!this.collide(Ladder.TYPE, x, tempY)) {
- if (v.y < 0) {
- tempY = Math.round(tempY);
- while (!this.collide(Ladder.TYPE, x, tempY)) {
- tempY++;
- }
- tempY--;
- v.y = 0;
- }
- }
- }
- if (!this.collide(Ladder.TYPE, x, tempY)) {
- // If no ladder, affected by Level.GRAVITY
- v.y += Level.GRAVITY;
- if (v.y > Level.MAX_Y_SPEED) v.y = Level.MAX_Y_SPEED;
- // Update Cordinate Y
- tempY += v.y;
- tempY = Math.round(tempY);
- // avoid going through a ladder when landing on it
- var landingLadder:Ladder = this.collide(Ladder.TYPE, x, tempY) as Ladder;
- if (landingLadder && v.y > 0) {
- tempY = landingLadder.y - this.height;
- }
- }
- tempY = Math.round(tempY);
- // Collision
- if (collide(Block.TYPE, x, tempY)) {
- while (collide(Block.TYPE, x, tempY)) {
- if (v.y > 0) {
- tempY--;
- } else if (v.y < 0) {
- tempY++;
- }
- }
- v.y = 0;
- }
- y = tempY;
- /**
- * Horizontal Movement
- */
- // Detect key click
- if (Input.check(Key.LEFT)) movement.x--;
- if (Input.check(Key.RIGHT)) movement.x++;
- // Update Horizontal Speed
- v.x = SPEED_X * movement.x;
- // Update Cordinate X
- x += v.x;
- x = Math.round(x);
- // Collision
- while (collide(Block.TYPE, x, y)) {
- if (v.x > 0) {
- x--;
- } else if (v.x < 0) {
- x++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment