azrafe7

ScrollingTower_2_Update

Jan 9th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     override public function update():void
  2.     {
  3.         super.update();
  4.         // For movement direction
  5.         var movement:Point = new Point;
  6.        
  7.         /**
  8.          * Vertical movement
  9.          */
  10.         var tempY:Number = y;
  11.         // Check ladder collision (to climb up/down)
  12.         if (this.collide(Ladder.TYPE, x, y+1) || this.collide(Ladder.TYPE, x, y)) {
  13.             if (Input.check(Key.UP)) movement.y--;
  14.             if (Input.check(Key.DOWN)) movement.y++;
  15.             v.y = SPEED_X * movement.y;
  16.        
  17.             // Update Cordinate Y
  18.             tempY += v.y;
  19.            
  20.             // align to ladder after climbing up
  21.             if (!this.collide(Ladder.TYPE, x, tempY)) {
  22.                 if (v.y < 0) {
  23.                     tempY = Math.round(tempY);
  24.                     while (!this.collide(Ladder.TYPE, x, tempY)) {
  25.                         tempY++;
  26.                     }
  27.                     tempY--;
  28.                     v.y = 0;
  29.                 }
  30.             }
  31.         }
  32.  
  33.         if (!this.collide(Ladder.TYPE, x, tempY)) {
  34.             // If no ladder, affected by Level.GRAVITY
  35.             v.y += Level.GRAVITY;
  36.             if (v.y > Level.MAX_Y_SPEED) v.y = Level.MAX_Y_SPEED;
  37.  
  38.             // Update Cordinate Y
  39.             tempY += v.y;
  40.            
  41.             tempY = Math.round(tempY);
  42.            
  43.             // avoid going through a ladder when landing on it
  44.             var landingLadder:Ladder = this.collide(Ladder.TYPE, x, tempY) as Ladder;
  45.             if (landingLadder && v.y > 0) {
  46.                 tempY = landingLadder.y - this.height;
  47.             }
  48.         }
  49.        
  50.         tempY = Math.round(tempY);
  51.        
  52.         // Collision
  53.         if (collide(Block.TYPE, x, tempY)) {
  54.             while (collide(Block.TYPE, x, tempY)) {
  55.                 if (v.y > 0) {
  56.                     tempY--;
  57.                 } else if (v.y < 0) {
  58.                     tempY++;
  59.                 }
  60.             }
  61.             v.y = 0;
  62.         }
  63.        
  64.         y = tempY;
  65.        
  66.        
  67.         /**
  68.          * Horizontal Movement
  69.          */
  70.        
  71.         // Detect key click
  72.         if (Input.check(Key.LEFT)) movement.x--;
  73.         if (Input.check(Key.RIGHT)) movement.x++;
  74.        
  75.         // Update Horizontal Speed
  76.         v.x = SPEED_X * movement.x;
  77.          
  78.         // Update Cordinate X
  79.         x += v.x;
  80.        
  81.         x = Math.round(x);
  82.        
  83.         // Collision
  84.         while (collide(Block.TYPE, x, y)) {
  85.             if (v.x > 0) {
  86.                 x--;
  87.             } else if (v.x < 0) {
  88.                 x++;
  89.             }
  90.         }
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment