2011-04-12 6 views
0

math.random을 사용하여 무대 상단에서 임의로 개체를 놓습니다. 하나의 객체로 작업했습니다. 그러나 객체 수를 6 개로 늘리려면 다음 코드를 추가했습니다. 그러나 나는 "붙어있어"무대 상단에 6 개의 객체가 있습니다. 여기서 내가 뭘 잘못하고 있니? 도움에 감사드립니다.개체가 스테이지 상단에서 멈춰서 떨어지지 않습니다.

  private function bombInit(): void { 
       roachBombArray = new Array(); 
     for (var i:uint =0; i < numBombs; i++) { 
       roachBomb= new RoachBomb(); 
        roachBomb.x = Math.random() * stage.stageWidth; 
        roachBomb.vy = Math.random() * 2 -1; 
        roachBomb.y = -10; 
        addChild(roachBomb); 
        roachBombArray.push(roachBomb); 
     } 

        addEventListener(Event.ENTER_FRAME, onEntry); 
       } 

       private function onEntry(event:Event):void { 
        for (var i:uint = 0; i< numBombs; i++) { 

       var roachBomb = roachBombArray[i]; 

       vy += ay; 
       roachBombArray[i] += vy; 


       if (roachBombArray[i] > 620) { 
       removeChild(roachBombArray[i]); 
       removeEventListener(Event.ENTER_FRAME, onEntry); 

답변

0

첫 번째 폭탄이되는 더 이상 ENTER_FRAME 이벤트를 수신하고 폭탄 중 하나를 업데이트하고 지적이 닿지 않는 오른쪽 떨어져 갈 때 당신은 당신의를 enterFrame 리스너를 제거하고 있습니다.

모든 청각에 애니메이션을 적용 할 때까지이 청취자를 제거하고 싶지는 않습니다.

UPDATE : ... 당신은 RoachBomb에 속도를 추가하는 대신하려고하는

public class BombDropper extends Sprite { 

    private static const GRAVITY:int = 1; // Set gravity to what you want in pixels/frame^2 
    private static const BOTTOM_OF_SCREEN:int = 620; 
    private var numBombs:int = 6; 
    private var roachBombArray:Array; 

    // ... constructor and other class stuff here 

    private function bombInit(): void 
    { 
     roachBombArray = new Array(); 

     for (var i:int =0; i < numBombs; ++i) 
     { 
      var roachBomb:RoachBomb = new RoachBomb(); 
      roachBomb.x = Math.random() * stage.stageWidth; 
      roachBomb.vy = Math.random() * 2 -1; 
      roachBomb.y = -10; 
      this.addChild(roachBomb); 
      roachBombArray.push(roachBomb); 
     } 

     this.addEventListener(Event.ENTER_FRAME, onEntry); 
    } 

    private function onEntry(event:Event):void 
    { 
     for each (var roachBomb:RoachBomb in roachBombArray) 
     { 
      roachBomb.vy += GRAVITY; 
      roachBomb.y += vy; 

      if (roachBomb.y > BOTTOM_OF_SCREEN) 
      { 
       this.removeChild(roachBomb); 
       roachBombArray.splice(roachBombArray.indexOf(roachBomb),1); 
       if (roachBombArray.length == 0) 
       { 
        this.removeEventListener(Event.ENTER_FRAME, onEntry); 
       } 
      } 
     } 
    } 
} 
+0

오, 어리석은! 나는 그것을 제거했으나 지금은 1034를 변환 할 수 없다. 나는이 오류를 지금 연구 할 것이다 .... 고마워, Adam. – londonbird

+0

아마도 DisplayObject로 removeChild (roachBombArray [i])가 필요할 수도 있습니다. 배열은 입력되지 않으므로 컴파일러는 roachBombArray [i]가 무엇인지 알지 못합니다. –

+0

알았어. 나도 removeChild (roachBombArray [i] .roachBomb)를했는데 둘 다 똑같은 결과를 얻었다 ... 그 폭탄은 여전히 ​​무대의 꼭대기에 붙어있어 움직이지 않을 것이다. lol – londonbird

1

을 나는 일이 에단의 관찰을 incorperating,보고 기대하는 당신은 당신이 선언 로컬 roachBomb를 사용한다고하는 방법 RoachBomb y 위치로 이동합니다.

var roachBomb = roachBombArray[i]; 

하지만 당신은 그것을 조작하지 :

roachBombArray[i] += vy; 

roachBombArray[i].y += vy; 

은 또한 로컬 변수를 생성해야한다. 아마 이런 일을하려고하셨습니까?

var roachBomb:RoachBomb = roachBombArray[i]; // I added the type to the local variable 
roachBomb.vy += ay; 
roachBomb.y += vy; // Manipulate the local variable 
if (roachBomb.y > 620) { 
removeChild(roachBomb); 
} 
+0

roachBombArray [i] + = vy; Heh, 예, 그것은 또한 문제가 될 것입니다 :-) –

+0

roachBomb.vy + = ay이어야합니다; 나는 프레임 당 numBombs 시간을 증가시키는 모든 폭탄에 대해 하나의 공통점을 선언하도록 의도하지 않았 음을 확신합니다! 모든 사람들에게 공통적 인 것은 (비록 중력이 일정하고 모든 것) 의미가 있습니다. –

+0

아 예. 감사합니다 아담, 나는 당신의 수정으로 내 대답을 업데이 트했습니다. –

관련 문제