2011-04-26 2 views
0

나는 Actionscript에서 제어 할 수있는 간단한 보트를 만들고 앞으로 나아갈 수있게하고 왼쪽과 오른쪽으로 돌리지 만 돌릴 수는 없습니다. 나는 보트가 가리키는 방향으로 가길 원해.ActionScript의 간단한 보트

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.x += speed; 
     player_mc.rotationZ += speed; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.x -= speed; 
     player_mc.rotationZ -= speed; 

    } 
    if(upKeyIsDown) 
    { 
     player_mc.y -= speed; 
    } 
} 

내가 뭘 잘못하고 있는지 말해 줄 수있는 사람에게 미리 감사드립니다.

편집 :

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotationZ += turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotationZ -= turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI/180); 
     player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI/180); 
    } 
} 

편집

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotation += turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotation -= turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     // convert our rotation to radians first 
     var rads:Number = player_mc.rotation * (Math.PI/180.0); 
     player_mc.x += speed * Math.cos(rads); 
     player_mc.y += speed * Math.sin(rads); 
    } 
} 
+0

대신'player_mc.y - = ...'를 설정하고'player_mc.y + = ...'를 설정하면됩니다. 내 대답에서 언급 한 것처럼 ... – divillysausages

답변

1

같은 시도 : 나는 지금의 코드를 테스트 할 수 있지만이 있어야한다

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotationZ -= turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotationZ += turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI/180); 
     player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI/180); 
    } 
} 

을 아이디어를 설명하기에 충분합니다. 내가 다르게하고있는 것은 왼쪽과 오른쪽 키를 사용하여 배를 조종하고 (각도를 변경하여) Up 키를 클릭 할 때 가리키는 방향으로 이동시키는 것입니다.

배를 움직일 때 X 축과 Y 축을 일정량만큼 움직일 수 없으므로 배의 방향도 고려해야합니다. 이를 위해 사인 함수와 코사인 함수를 사용하십시오.

+0

감사. 점점 가까워 지지만 이제는 위로 올렸을 때 보트는 얼마나 멀리 회전했는지에 따라 왼쪽 또는 오른쪽으로 만 이동합니다. – RapsFan1981

+0

rotationZ 속성은 각도를 사용하고 Math 함수는 라디안을 사용합니다. 수학 함수를 호출 할 때는 각도를 라디안으로 변환해야합니다 (player_mc.rotationZ * Math.PI/180). 그래도 작동하지 않으면 사용한 정확한 코드를 게시하십시오. – TokPhobia

+0

일부 편집을 했는데도 up 키를 사용하여 보트를 x 축으로 이동합니다. – RapsFan1981

-1

TokPhobia 거의 당신이 3D로이 일을하고하지 않는 한 rotation 속성은이 rotationZ

+0

동일한 문제가 있습니다. 여기에서 볼 수 있습니다 : http://hedonsoft.com/test.html – RapsFan1981

+0

실제로 작동 중입니다. 우주선 이미지의 회전을 변경하면됩니다. 오른쪽 방향을 0으로 설정하고 우주선이 위를 향하도록 (플래시 회전의 경우 -90) 디자인합니다. 오른쪽을 가리 키도록 우주선 이미지를 편집하면 괜찮습니다. – divillysausages

+0

Thanks divilly it :) – RapsFan1981

0

를 사용하여 몇 가지 간단한을 사용할 필요가 없습니다, 잘 작동합니다, 바로

private function _moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
     this.m_player.rotation += turnFactor; 

    if (leftKeyIsDown) 
     this.m_player.rotation -= turnFactor; 

    if(upKeyIsDown) 
    { 
     // convert our rotation to radians first 
     var rads:Number = this.m_player.rotation * (Math.PI/180.0); 
     this.m_player.x += speed * Math.cos(rads); 
     this.m_player.y += speed * Math.sin(rads); 
    } 
} 

Btw는 그것을했다 삼각 함수를 사용하여 X 축과 Y 축에서 오브젝트를 얼마나 많이 움직일 지 계산합니다. 보트가 A 지점 인 직각 삼각형을 만들고,이 속도는 시간이다, 그리고 배의 방향은 각도 A는 :

right triangle

는 이제 변의 길이 a와 b를 알아 내기 위해 사인과 코사인을 사용합니다. 그것은 X 축과 Y 축을 얼마나 멀리 움직일 것입니다.

관련 문제