0
votes

I am trying to establish some boundaries for my platform game. What is happening is when my player hits the "wall" at the right it jumps across the stage to the opposite side. When I hit the left side to begin with, however, it does what it is supposed to and stops at the wall. This is my code:

stage.addEventListener(Event.ENTER_FRAME,handleCollision);

function handleCollision( e:Event ):void{

   if(player.hitTestObject(wall2))
   { 
        player.x = stage.x + player.width/2;
   } 

} stage.addEventListener(Event.ENTER_FRAME,Collision);

function Collision( e:Event ):void{

   if(player.hitTestObject(side))
   { 
        player.x = stage.x + player.width/2;
   } 

} (I know that they do not need to be separated, I just figured I would try it to see if it would work. The one on the top works perfectly, the one on the bottom has something wrong with it) Thanks!

1
is stage.x a changing value that varies depending on where you are? If stage.x is static these would both evaluate to the same position and it would make sense the way you are seeing it happen.Josh

1 Answers

0
votes

Use stage.width instead of stage.x as Josh suggested. Also make sure for the right wall it is "stage.width-(player.width/2)"

Try something like

function collideWalls():void{
  if(player.x<0){
    //collision is on the left wall
    player.x = player.width*.5;
  }else if(player.x>stage.width-(player.width*.5)){
    //collision is on the right wall
    player.x = stage.width-(player.width*.5);
  }
}

And you only need to put that in one Enter Frame event handler rather than the two you have which appear to be for each collision that occurs.