2014-01-09 3 views
0

나는 종류의 스페이스 인베이더 게임을 완성하려고 노력 중이다. 임의의 위치에서 별을 생성하고 무대 아래로 스크롤 한 다음 각 별이 사라진 후 새 별을 추가하려고합니다. 문제는 indexOf 메소드에 있다고 생각합니다.이 메소드는 별 y를 찾기 위해 사용하려고했습니다. 내가 초보자 :이야,이 바보 같은 실수가 될 수 있습니다 알고AS3 index of array object 속성

나의 현재 메인 클래스 :

public class Main extends Sprite 
{ 
    private var ship:Ship = new Ship(); 
    private var numStars:int = 80; 
    private var starArray:Array = new Array(); 

    public function Main():void 
    { 
     stage.addChild(ship); 
     ship.x = stage.stageWidth/2 - ship.width/2; 
     ship.y = stage.stageHeight/2 - ship.height/2; 
     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); 
     stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); 
     stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
     for (var i:int = 0; i < numStars; i++) 
     { 
      createStar(); 
     } 
    } 
    public function createStar():void 
    { 
     var newStar:Star = new Star(); 
      starArray.push(newStar); 
      stage.addChildAt(newStar,1); 
      newStar.x = Math.random() * stage.stageWidth; 
      newStar.y = Math.random() * stage.stageHeight; 
      newStar.alpha = Math.random(); 
      newStar.rotation = Math.random()*360; 
      newStar.scaleX = Math.random(); 
      newStar.scaleY = Math.random(); 
    } 

    public function keyDownHandler(e:KeyboardEvent):void 
    { 
     if (e.keyCode == Keyboard.UP) 
     { 
      ship.accelerationY = -0.3; 
     } 
     if (e.keyCode == Keyboard.DOWN) 
     { 
      ship.accelerationY = 0.3; 
     } 
     if (e.keyCode == Keyboard.LEFT) 
     { 
      ship.accelerationX = -0.3; 
     } 
     if (e.keyCode == Keyboard.RIGHT) 
     { 
      ship.accelerationX = 0.3; 
     } 
    } 

    public function keyUpHandler(e:KeyboardEvent):void 
    { 
     if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) 
     { 
      ship.accelerationX = 0; 
      ship.accelerationY = 0; 
     } 
     if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) 
     { 
      ship.accelerationY = 0; 
      ship.accelerationX = 0; 
     } 
    } 

    public function enterFrameHandler(e:Event):void 
    { 
     //acceleration 
     ship.vx += ship.accelerationX; 
     ship.vy += ship.accelerationY; 
     //friction 
     ship.vx *= ship.friction; 
     ship.vy *= ship.friction; 
     if (Math.abs(ship.vx) < 0.1) 
     { 
      ship.vx = 0; 
     } 
     if (Math.abs(ship.vy) < 0.1) 
     { 
      ship.vy = 0; 
     } 

     ship.rotation = ship.vx * 2; 
     //set speed limit 
     if (ship.vx > ship.speedLimit) 
     { 
      ship.vx = ship.speedLimit; 
     } 
     if (ship.vx < -ship.speedLimit) 
     { 
      ship.vx = -ship.speedLimit; 
     } 
     if (ship.vy > ship.speedLimit) 
     { 
      ship.vy = ship.speedLimit; 
     } 
     if (ship.vy < -ship.speedLimit) 
     { 
      ship.vy = -ship.speedLimit; 
     } 
     //set stage boundaries 
     if (ship.x < 0) 
     { 
      ship.x = 0; 
     } 
     if (ship.y < 0) 
     { 
      ship.y = 0; 
     } 
     if (ship.x + ship.width > stage.stageWidth) 
     { 
      ship.x = stage.stageWidth - ship.width; 
     } 
     if (ship.y + ship.height > stage.stageHeight) 
     { 
      ship.y = stage.stageHeight - ship.height; 
     } 
     ship.x += ship.vx; 
     ship.y += ship.vy; 

     //star enter frame code 
     for (var i:int = 0; i < numStars; i++) 
     { 
      starArray[i].y += 0.5 + Math.random() * 2; 
     } 
     if (starArray.indexOf(starArray.y) > stage.stageHeight) //if y property of any star is higher than stage height, create a new star 
     { 
      createStar(); 
     } 
    }  

} 

답변

0

내가 시간 기반의 애니메이션을 TweenLite 같은 트윈 유틸리티를 조사하는 것이 좋습니다 : (http://www.greensock.com/tweenlite/를)

은 개체 풀링, 새 개체를 만드는 대신 개체를 다시 보는 것이 좋습니다. 새로운 프로그래머로서 배우기 좋은 것.

---------change these lines------------- 

//star enter frame code 
for (var i:int = 0; i < numStars; i++) 
{ 
    starArray[i].y += 0.5 + Math.random() * 2; 
} 
if (starArray.indexOf(starArray.y) > stage.stageHeight) //if y property of any star is higher than stage height, create a new star 
{ 
    createStar(); 
} 

---------to this------------- 

//star enter frame code 
for (var i:int = 0; i < numStars; i++) 
{ 
    var star:Star = starArray[i]; 
    star.y += 0.5 + Math.random() * 2; 
    if (star.y>stage.stageHeight){ 
     //dont create a new star -- memory leak 
     //move the same star to a new random location 
     star.y = 0; 
    } 
} 
+0

멋지고 간단합니다! 고맙습니다 ! – chilliq

+0

개봉 된 여러분, 제 권고를 살펴보고, 시간을 절약하고 좋은 습관을 배우십시오. – mihai

0

대신 단지 새로운 스타를 만드는, 왜 그냥 상단에 교체 : 문제는 내가 다음과 같은 변화에 제대로 작동 프로그램을 가지고

을 자리하고있는 곳

당신에 대한 올바른 화면 바깥 쪽?
indexOf의 경우 전달되는 개체 배열의 인덱스 만 반환합니다. 그리고이 경우 배열의 값이 y입니다.이 속성에는 해당 속성이 없습니다.
대신 게임 루프에서 위치 확인 코드를 for 루프로 이동하십시오. 그렇게하면 경계 외부에있는 별의 색인 (예 : i 변수)을 얻었을 경우 위치를 조정하고 메모리를 절약 할 수 있습니다.

for (var i:int = 0; i < numStars; i++) 
{ 
    starArray[i].y += 0.5 + Math.random() * 2; 
    if(starArray[i].y > stage.stageHeight) 
    { 
     // Repositions the star between x: 0 to stageWidth, y: -5 to -15 
     starArray[i].y = Math.random() * -10 - 5; 
     starArray[i].x = Math.random() * stage.stageWidth; 
    } 
}