2

2D 플랫폼 게임을 만들고 있습니다. 충돌하는 데 문제가 있습니다. 내 캐릭터가 명중 할 때 플랫폼 위에 서있을 때 2.5 초 동안 그곳에 머무를 것이고 다른 모든 플랫폼을 통해 1 층으로 떨어질 것입니다. 내 중력 기능과 충돌 기능이 제대로 작동하지 않아 무언가를해야한다고 생각합니다. 어떤 도움을 주시면 감사하겠습니다.AS3 캐릭터가 플랫폼을 통과하여 충돌이 제대로 작동하지 않습니다.

이 = fireboy1

여기 내 문자 클래스에서 중력 코드입니다 :

public var gravity:int = 0; 
public var floor:int = 461; 

public function adjust():void 
    { 
     //applying gravity 
     this.y += gravity; 
     if(this.y + this.height <floor) 
      gravity++; 
     else 
     { 
      gravity = 0; 
      this.y = floor - this.height; 
     } 

은 여기에 메인 클래스에서 내 충돌에 대한 코드입니다 :

//collision detection of platform1 
    public function platform1Collision():void 
    { 
     if(fireboy1.hitTestObject(Platform1)) 
     { 
      if(fireboy1.y > Platform1.y) 
      { 
       fireboy1.y = Platform1.y + Platform1.height; 
      } 
      else 
      { 
       fireboy1.y = Platform1.y - fireboy1.height; 
      } 
     } 
+0

try : if ((this.y + this.height) tziuka

+0

전혀 변경되지 않았습니다. ( –

+1

그냥 물리 구조 프레임 워크를 사용하는 것이 훨씬 쉽습니다 .box2d 등처럼'adjust()'와'platform1Collision()'이 모든 프레임을 실행합니까? 이상적으로는 같은 함수에 있어야하고 충돌이 발생하면 중력을 사용해서는 안됩니다 ('this.y + = gravity'). 코드의 범위를 포함하도록 질문을 업데이트하십시오 (예 :'this'는 무엇입니까? 첫 번째 블록과 두 번째 코드 블록과의 관계는 무엇입니까? – BadFeelingAboutThis

답변

0

문제 가능성이 높습니다 y 위치가 모든 프레임마다 증가합니다 (충돌과 관계 없음)

더 나은 접근 방법은 플레이어 및 각 플랫폼에 대해 하나씩 사용하는 대신 하나의 게임 루프/Enter 프레임 핸들러를 만드는 것입니다. 또한 플랫폼 및 바닥에 상대적인 플레이어 위치 계산과 함께 잘못된 수학이있었습니다.

public var gravity:int = 0; 
public var floor:int = 461; 

//add this in your constructor or init of the class 
this.addEventListener(Event.ENTER_FRAME, gameLoop); 

function gameLoop(e:Event):void { 

    //start with all your platform collisions 
    if(fireboy1.hitTestObject(Platform1)) 
    { 
     if(jumping){ //some flag that indicates the player is jumping at the moment 
      //place fireboy right under the platform 
      fireboy1.y = Platform1.y + Platform1.height; 
     }else{ 
      //place fireboy on the platform perfectly 
      fireboy1.y = (Platform1.y + Platform1.height) - fireboy1.height; //changed your math here 
      return; //there's no use checking any other platforms or doing the gravity increment, since the player is already on a platform, so exit this method now. 
     } 
    } 

    //any other platform checks (should be done in a loop for sanity) 

    //if we made it this far (no returns), then do the gravity adjustments 
    fireboy1.y += gravity; 
    if(fireboy1.y - fireboy1.height < floor){ //changed your math here 
     gravity++; 
    } else 
    { 
     gravity = 0; 
     fireboy1.y = floor - fireboy1.height; 
    } 
} 
+0

이 작동하지만 fireboy1이 밑에서 플랫폼을 치면 –

+0

플랫폼 상단으로 점프합니다. 이전 위치 (플랫폼을 플레이어의 위 또는 아래에 있는지 확인하기 위해 위치를 사용) 또는 플레이어의 방향을 추적 (위 또는 아래로 이동하는 것)하는 것이 더 나은 방법 일 수 있고 배치를 기반으로 할 수 있습니다 그. – BadFeelingAboutThis