As3跳跃碰撞问题

时间:2016-04-24 22:10:18

标签: actionscript-3

  

我已经成功地使我的角色跳跃但是在平台的顶部只有完美的碰撞但是当它击中浮动平台的底部时,它穿过平台并且玩家不会从边缘掉下来这些平台。相反,他在实例的y轴上运行。此外,当游戏最初开始时,玩家在特定的y坐标上在半空中跑步。无论如何我可以扭转这些问题吗?

 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 个答案:

答案 0 :(得分:0)

当你跳起并击中平台时,如果发生阻挡,则会发生以下情况:

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;

              }
          }   

这会将warmage移动到平台上方,并将重力(和vsp)设置为0,这样他就不会掉下来。我没有看到代码中的任何地方,在将重力设置为0之后将重力设置回某些东西(除非你跳转)。我认为你应该做一些事情。

1。)您可能不应该使用hitTestPoint而是构建自己的命中检测。以下是一个示例:https://gamedev.stackexchange.com/questions/586/what-is-the-fastest-way-to-work-out-2d-bounding-box-intersection

2。)确定玩家是在平台下还是在他碰撞的平台之上,并对vsp应用适当的更改(如果玩家在上方则将其取出,或者如果玩家在下方则让玩家摔倒)。

3。)如果玩家未被任何平台阻挡,请务必将加速度(grav)设置回默认值,以便玩家再次开始下降。

4。)(可选)当您不需要时,您正在使用两个Enter Frame事件。您可能应该在每个帧的gameloop内部调用平台碰撞功能。