2017-11-30 40 views
0

나는 그들이 직면 한 각도를 향해 설정된 속도로 배를 이동해야하는 게임을 만들고 있습니다. 나는이 코드를 사용하여 게임에서 다른 곳으로 이동시키지 만, 배열에서 그들을 갖는 것은 복잡한 일이 있다고 가정한다.AS3 - 각도에 상대적인 배열 객체 이동

도움을 주시면 감사하겠습니다.

var ship1 = this.addChild(new Ship()); 
var ship2 = this.addChild(new Ship()); 
var ship3 = this.addChild(new Ship()); 
var ship4 = this.addChild(new Ship()); 

var shipSpeed1 = 10; 

var shipArray: Array = []; 

shipArray.push(ship1, ship2, ship3, ship4); 

for (var i: int = 0; i < shipArray.length; i++) { 
var randomX: Number = Math.random() * stage.stageHeight; 
var randomY: Number = Math.random() * stage.stageHeight; 

shipArray[i].x = randomX; 
shipArray[i].y = randomY; 

shipArray[i].rotation = 90; 

shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI/180)) * shipSpeed1; 
shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI/180)) * shipSpeed1; 

} 

나는 또한이 기능을 동일한 기능에 포함 시켰지만이 기능을 사용할 수 없습니다. 다시 한번 내가 처음이 작업

if (shipArray[i].x < 0) { //This allows the boat to leave the scene and 
enter on the other side. 
    shipArray[i].x = 750; 
} 
if (shipArray[i].x > 750) { 
    shipArray[i].x = 0; 
} 
if (shipArray[i].y < 0) { 
    shipArray[i].y = 600; 
} 
if (shipArray[i].y > 600) { 
    shipArray[i].y = 0; 
} 
+0

코드는 단수 배를 이동하는 데 사용 보여줍니다. 지금 당장은 코드가 초기 배치 이상으로 움직이지 않습니다. – BadFeelingAboutThis

+0

@BadFeelingAboutThis var ship = evt.currentTarget; \t ship.x + = Math.sin (ship.rotation * (Math.PI/180)) * randomSpeed ​​(4, 15); // 임의 번호를 사용하여 배 제어하기 \t ship.y - = Math.cos (ship.rotation * (Math.PI/180)) * randomSpeed ​​(4, 15); 그 똑같은. – AndyGUY

답변

0

있었다, 당신은 산란/초기화 상과 갱신 단계로 코드를 분리해야합니다. 다음과 같은

뭔가 :

var shipArray: Array = []; 

//spawn and set initial values (first phase) 
//spawn 4 new ships 
var i:int; 
for(i=0;i<4;i++){ 
    //instantiate the new ship 
    var ship:Ship = new Ship(); 
    //add it to the array 
    shipArray.push(ship); 

    //set it's initial x/y value 
    ship.x = Math.random() * (stage.stageWidth - ship.width); 
    ship.y = Math.random() * (stage.stageHeight - ship.height); 

    //set it's initial rotation 
    ship.rotation = Math.round(Math.random() * 360); 

    //set the ships speed (dynamic property) 
    ship.speed = 10; 

    //add the new ship to the display 
    addChild(ship); 
} 

//NEXT, add an enter frame listener that runs an update function every frame tick of the application 
this.addEventListener(Event.ENTER_FRAME, gameUpdate); 

function gameUpdate(e:Event):void { 
    //loop through each ship in the array 
    for (var i: int = 0; i < shipArray.length; i++) { 
     //move it the direction it's rotated, the amount of it's speed property 
     shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI/180)) * shipArray[i].speed; 
     shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI/180)) * shipArray[i].speed; 
    } 
} 
+0

새로운 문제는 무엇입니까? 배는 스크린에서 나옵니까? – BadFeelingAboutThis