0
votes

I have successfully made my character jump and but have only perfect collision on the top of the platform but when it hits the bottom of the floating platforms, it goes through the platforms and the player doesn't fall off the edge of these platforms. Instead he runs on the y axis of the instances. Also, when the game initially starts, the player is running in mid air on a specific y coordinate. Is there anyway i can reverse the issues?

 import flash.events.KeyboardEvent;
 import flash.events.Event;
 import flash.text.engine.EastAsianJustifier;   
 import flash.sensors.Accelerometer;

 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  = 4//Friction

 //Arrays list for platforms


//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
          {
             warMage.x -= 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
          {
            warMage.x += 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.UP) || jumped == true)//If we press the up arrow or we've jumped
          {

              vsp = -25;;//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)//This checks the laws per frame
 {

     if(warMage.x < 0)//Sets room boundaries for left
    {
        warMage.x = 0;
    }
if(warMage.x > 1000)//Sets room boundaries for right
    {
        warMage.x = 1000;   
    }

 }

 function platformCollision(e:Event):void//Collision for the platforms
 {
          vsp += grav;
          if(!multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true) && jumped == true)
              {

                  grav++;
                  warMage.y += vsp;
                  warMage.gotoAndStop("JumpWarmage");//Play teh jump animation
              }
          for(var i:Number = 0;i < 34; i++)
          {
          if(multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true))//Check if the player hits any platforms in the array
              {
                  warMage.y --;//Place the character a pixel above the platforms
                  grav = 0;//Gravity isnt applied
                  vsp = 0;
                  jumped = false;

              }
          }       
          }
1

1 Answers

0
votes

When you jump up and hit the platform the following if block will happen:

if(multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true))//Check if the player hits any platforms in the array
              {
                  warMage.y --;//Place the character a pixel above the platforms
                  grav = 0;//Gravity isnt applied
                  vsp = 0;
                  jumped = false;

              }
          }   

This will move the warmage above the platform and set gravity (and vsp) to 0 so he never falls off. I don't see anywhere in your code where you are setting gravity back to something after setting it to 0 (unless you jump). I think you should do a few things.

1.) You should probably not use hitTestPoint and instead build your own hit detection. Here's an example to go on: https://gamedev.stackexchange.com/questions/586/what-is-the-fastest-way-to-work-out-2d-bounding-box-intersection

2.) Determine whether the player is under the platform or above the platform he collided with and apply the appropriate change to vsp (0 it out if the player is above or make the player fall if he is below).

3.) If the player isn't being blocked by any platform make sure to set your acceleration (grav) back to it's default value so the player will start falling again.

4.) (optional) You are using two Enter Frame events when you don't really have to. You should probably call the platform collision function from inside of your gameloop every frame.