简体   繁体   English

ActionScript 3 Flash出现水平碰撞问题

[英]Horizontal collision issue with actionscript 3 flash

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. 但是,当我将warMage放在左侧并检查右侧是否与平台发生碰撞时,玩家会立即传送到平台旁边。 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. 我不明白为什么我所做的就是翻转另一方的if语句中的算术符号,以便玩家检查其右侧。

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

    }
}

Don't you need if/ else if in here instead of 2 ifs? 您是否需要if / else if在这里而不是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; 现在warMage.x是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 )+ warMage.width / 2绝对大于platform.x - platform.width/2 - 2 2-2,这就是为什么要设置

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

right after. 之后。

Looks like platformCollision needs rethinking. 看起来platformCollision需要重新考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM