2016-04-27 12 views
0

私のplatformcollision関数では、プレイヤーが左側の衝突をチェックしている間はすべて正常です。私のプレーヤーは、それをやり直すことはできません。しかし、戦闘機を左に置いて、右のプラットフォームと衝突しているかどうかを確認すると、プレイヤーは直ちにプラットフォームの横に移動します。なぜ私はプレイヤーがその右側を確認するために、他のプレイヤーからのif文で算術記号を反転させるのか、理解できないのです。actionscript 3の横の衝突の問題flash

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 

    } 
} 

答えて

0

あなたがもし/他の場合は、ここでは代わりに2つのIFSの必要はありませんか。

このビットがtrueの場合:

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; 

を設定します。 は今warMage.xはplatform.x + platform.width/2 + warMage.width/2; その場合は第二のチェックされています(今platform.x + platform.width/2 + warMage.width/2です)

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

のでwarMage.x + warMage.width/2は間違いplatform.x - platform.width/2 - 2よりも大きく、あなたが

warMage.x = platform.x - platform.width/2 - warMage.width/2; 
を設定する理由です

の直後です。

platformCollisionのように見えます。

+0

ありがとうございました。この2dプラットフォームのコードは非常に困難です。 – user199845