0
votes

In my platformcollision function, when the player is checking for collision on his left side everything is fine. My player doesnt go through, he can move the opposite way. However, when I put the warMage on the left and its checking if theres collision with the platform on his right side, the player immediately teleports next to the platform. I dont understand why as all I did was flip the arithmetic signs in that if statement from the other for the player to check its right side.

import flash.events.Event;
import flash.display.MovieClip;

var Key:KeyObject = new KeyObject(stage);//Help the stage checks for   keypressed objects           //Initialized variable integers
var hsp:Number = 0;// horizontal speed
var vsp:Number = 0;// vertical speed
var grav:Number = 2;//Gravity
var fric:Number  = .5;//Friction
var floor:int = 800;//Bottom of the stage
//All Booleans
var lDown:Boolean = false;
var rDown:Boolean = false;
var jumped:Boolean = false;
var attacking:Boolean = false;


warMage.gotoAndStop("idleWarmage");//Initially starts at idle state
stage.addEventListener(Event.ENTER_FRAME, keyPressed);//Listens for buttons    pressed
stage.addEventListener(Event.ENTER_FRAME, gameloop);// The physics applied  to character
stage.addEventListener(Event.ENTER_FRAME, platformCollision);

function keyPressed(e:Event):void
{
if(Key.isDown(Key.LEFT))//If we pressed the left arrow button
{

    lDown = true;//Condition to check if player is in running state
    if(lDown = true)//If we are running left
      {
         hsp -= 15;//Move left
         warMage.gotoAndStop("RunWarmage");//Play the running animation
         warMage.scaleX = -1;//Flip the image scale
      }
}else if(Key.isDown(Key.RIGHT))//If we pressed the right arrow button
{

    rDown = true;//Condition to check if player is in running state
    if(rDown = true)//If we are moving right
      {
        hsp += 15;//Move the position right
        warMage.gotoAndStop("RunWarmage");//Play the animation
        warMage.scaleX = 1//Face right
      }
}else if(Key.isDown(Key.SPACE))//If we press the spacebar
     {
         warMage.gotoAndStop("AttackWarmage");//Play teh attack animation
         warMage.x += 5; //Lunge right
         if(warMage.scaleX == -1)//If we are initially facing left
             {
                 warMage.x -= 10;//Lunge left
             }

     }else if(Key.isDown(Key.DOWN))
      {
        warMage.gotoAndStop("CrouchWarmage"); 

      }else if(Key.isDown(Key.UP) || jumped == true)//If we press the up arrow or we've jumped
      {

          warMage.y -= 60;//vertical speed goes up to 20
          jumped = true;//We know that player has jumped
          warMage.gotoAndStop("JumpWarmage");//Play teh jump animation

      }else if(jumped == false)//If we're not jumping
        {
            warMage.gotoAndStop("idleWarmage");//Return to idle position
        }
}


function gameloop(e:Event):void
{  
  warMage.y += grav;//Apply gravity to the player
  hsp *= fric;//Friction is applied to hsp to prevent infinite acceleration

  warMage.x += hsp;//The plater moves horizontal position
  if(warMage.x - warMage.width/2 < 0)//If the player goes past the left side
      {
          warMage.x = warMage.width/2;
      }
  if(warMage.x + warMage.width/2 > 1400)//If the player goes past right
      {
          warMage.x = 1400 - warMage.width/2;//Player cant go past
      } 
  if(warMage.y  < floor)//If we are above the floor
        {
         // warMage.y += grav;//Apply gravity to the player
          grav++;//Accelerate gravity in the process
          warMage.gotoAndStop("JumpWarmage");//Play the jump animation  

        } else //if(warMage.y - warMage.height/2 > floor)
          {

            jumped = false;//If we are on the floor then we're not jumping
            grav = 0;//Gravity can no longer be applied
            warMage.y = floor;//Player sits on top of the floor
          }
}

function platformCollision(e:Event):void
{
//If the player.x is less then the left side and the player can go into the box and if the warMage.y is equal to the  height
if(warMage.x - warMage.width/2 < platform.x + platform.width/2 + 2 && warMage.y - platform.y == platform.height/2) 
    {
        warMage.x = platform.x + platform.width/2 + warMage.width/2;
        //Player.x is equal to the left side of the platform

    }
if(warMage.x + warMage.width/2 > platform.x - platform.width/2 - 2 && warMage.y - platform.y == platform.height/2)
    {
        rDown = false;
        warMage.x = platform.x - platform.width/2 - warMage.width/2;
        //Player.x is equal to the left side of the platform

    }
}
1

1 Answers

0
votes

Don't you need if/ else if in here instead of 2 ifs?

If this bit is true:

if(warMage.x - warMage.width/2 < platform.x + platform.width/2 + 2 && warMage.y - platform.y == platform.height/2)

and you set

warMage.x = platform.x + platform.width/2 + warMage.width/2;

Then the 2nd if would be true right away. Now warMage.x is platform.x + platform.width/2 + warMage.width/2; So checking 2nd if:

if(warMage.x + warMage.width/2 > platform.x - platform.width/2 - 2 && warMage.y - platform.y == platform.height/2)

So warMage.x (which is now platform.x + platform.width/2 + warMage.width/2) + warMage.width/2 is definitely greater than platform.x - platform.width/2 - 2 and that's why you set

warMage.x = platform.x - platform.width/2 - warMage.width/2;

right after.

Looks like platformCollision needs rethinking.