title: AS3: Running over blocks in the game
body: <p>I'm making a game in flash actionscript 3, it is almost done, but I have some problems.</p>
  
  <p>Consider that in this game you are a character (for the moment a black square) and you have to get as high as possible by jumping on blocks that are falling. What I can do at the moment is jump and have a collision with the blocks (left - right) and jump onto the blocks.</p>
  
  <p>When 2 blocks fall exactly next to each other (Block A falls next to Block B) Then I jump on Block A and I want to Run to Block B (just run without jumping) I get something very weird, I can't walk on blocks.</p>
  
  <p>Here is the code where the problem should be:</p>
  
  <pre><code>package Classes
  </code></pre>
  
  <p>{
      import flash.events.*;
      import flash.display.MovieClip;
      import flash.display.Stage;
      import flash.ui.Keyboard;
      import Classes.KeyObject;</p>
  
  <pre><code>public class Player extends MovieClip
  {
      var isJumping:Boolean = false;
      var jumpPower:int = 0;
      var ground:int;
      var stageRef:Stage;
      var key:KeyObject;
      var isDead:Boolean = false;
      var allBlocks:Array;
      var blockX:int;
      var blockY:int;
      var blockWidth:int;
      var blockHeight:int;
      var steps:int = 10;
      var upPressed:Boolean = false;
      public function Player(stageReff:Stage,allBlocks:Array)
      {
  
          stageRef = stageReff;
          key = new KeyObject(stageRef);
          this.allBlocks = allBlocks;
          ground = stageRef.stageHeight;
          addEventListener(Event.ENTER_FRAME,update);
          //addEventListener(KeyboardEvent.KEY_UP, keyReleased);
      }
  
      function isHit()
      {
          for (var j = 0; j &lt; allBlocks.length; j++)
          {
              if (this.hitTestObject(allBlocks[j]) &amp;&amp; allBlocks[j] != isOnTopOf())
              {
                  blockX = allBlocks[j].x;
                  blockY = allBlocks[j].y;
                  blockWidth = allBlocks[j].width;
                  blockHeight = allBlocks[j].height;
                  return true;
              }
          }
          return false;
      }
  
      function isHitTop()
      {
          for (var j = 0; j &lt; allBlocks.length; j++)
          {
              if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
              {
                  jumpPower = 0;
                  isJumping = false;
                  this.y = allBlocks[j].y - allBlocks[j].height;
                  return true;
              }
          }
          return false;
      }
  
      function isHitBottom()
      {
          for (var j = 0; j&lt;allBlocks.length; j++)
          {
              if (this.hitTestObject(allBlocks[j].hitBoxBottom))
              {
                  if (isJumping)
                  {
                      this.y = allBlocks[j].y + this.height;
                      jumpPower=0;
                      isJumping = false;
                      isDead = false;
                      trace(isDead);
                  }
  
  
                  else
                  {
  
                      isDead = true;
                      trace(isDead);
                  }
              }
          }
      }
  
      function isOnTopOf()
      {
          for (var j = 0; j &lt; allBlocks.length; j++)
          {
              if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
              {
                  trace("is WEL onTopOf");
                  return allBlocks[j];
              }
          }
          trace("is NIET onTopOf");
          return null;
      }
  
  
      function update(evt:Event):void
      {
          if (!isDead)
          {
              isHitTop();
              isHitBottom();
              if (key.isDown(Keyboard.UP))
              {
                  //Voorkomen dat hij 2x springt
                  if (! isJumping)
                  {
                      jumpPower = 12;
                      isJumping = true;
                  }
              }
  
              if (key.isDown(Keyboard.LEFT))
              {
                  if (this.x &gt; stageRef.stageWidth - stageRef.stageWidth)
                  {
                      this.x -=  steps;
                      if (isHit() == true)
                      {
                          this.x = blockX + blockWidth + 1;
                          //trace("is gehit maar staat er niet op");
                      }
  
                  }
              }
              else if (key.isDown(Keyboard.RIGHT))
              {
                  if (this.x &lt; stageRef.stageWidth - this.width)
                  {
                      this.x +=  steps;
                      if (isHit() == true)
                      {
                          this.x = blockX - this.width - 1;
                          this.y = blockY;
                      }
                  }
              }
  
  
              if (isJumping)
              {
                  this.y -=  jumpPower;
                  jumpPower -=  2;
                  isHitTop();
                  isHitBottom();
              }
  
  
              if (! isJumping &amp;&amp; ! isHitTop())
              {
                  this.y +=  7;
              }
  
  
              if (this.y &gt; ground)
              {
                  this.y = ground;
                  isJumping = false;
              }
          }
          else
          {
              removeEventListener(Event.ENTER_FRAME,update);
          }
      }
  }
  }
  </code></pre>