2017-05-18 2 views
0

"터렛"이라는 자식 클래스를 부모 클래스 "게임"에서 "leftArrow"및 "rightArrow"변수에 액세스하여 터렛을 회전 할 수있게하려고합니다. 코드가 컴파일되고 있지만 어떤 이유로 부모 변수 값의 변경을 인식하지 못합니다.부모 변수에 액세스하는 자식

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.*; 

    public class Game extends MovieClip 
    { 

     // variables etc 
     public var leftArrow, rightArrow; 


     public function Game() 
     { 
      // add an event listener to spawn a new ship every frame 
      addEventListener(Event.ENTER_FRAME, loop); 
      addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown); 
      addEventListener(KeyboardEvent.KEY_DOWN,keyPressedUp); 

     } 

     function keyPressedDown(event:KeyboardEvent) 
     { 
      if (event.keyCode == 37) // left arrow 
      { 
       leftArrow = true; 
      } 
      if (event.keyCode == 39) // right arrow 
      { 
       rightArrow = true; 
      } 
     } 

     function keyPressedUp(event:KeyboardEvent) 
     { 
      if (event.keyCode == 37) // left arrow 
      { 
       leftArrow = false; 
      } 
      if (event.keyCode == 39) // right arrow 
      { 
       rightArrow = false; 
      } 
     } 

     function loop(e:Event) 
     { 


      // only spawn a ship if there are less than 10 already on screen 
      if (numChildren < 10) 
      { 
       // make a new instance of the Ship class 
       var s = new Ship(); 

       // add the ship to the display list 
       addChild(s); 

       // position and rotate the ship 
       s.x = Math.random() * stage.stageWidth; 
       s.y = Math.random() * stage.stageHeight; 

       s.rotation = Math.random() * 360; 
      } 
     } 
    } 
} 

을 그리고 여기에 자식 클래스의 : 다음은 상위 클래스이다

package 
{ 

    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.utils.getTimer; 

    public class Turret extends MovieClip 
    { 

     public function Turret() 
     { 
      addEventListener(Event.ENTER_FRAME, update); 
     } 



     function update(e:Event) 
     { 

      // make the turret move with key presses 
      if (MovieClip(parent).leftArrow) 
      { 
       trace("Made it here"); 
       this.rotation += 5; 
      } 

      if (MovieClip(parent).rightArrow) 
      { 
       this.rotation += 5; 
      } 
     } 
    } 
} 
+0

당신에게 확실히 뭔가 있습니까 포커스가 (시도 :이 키보드 이벤트에 대한 단계를 구독 할 수있는 좋은 움직임이 왜 단계는 모든 디스플레이 나무와 확실히 그것을 통해 전달합니다 키보드 이벤트를 버블 링하는 의 루트이기 때문에 즉,이다 'stage.focus = this'를 Game 클래스 생성자에서 사용), leftArrow 및 rightArrow 변수가 실제로 키보드에 반응하고 있는지 확인하십시오. – BadFeelingAboutThis

답변

0

문제는 당신이 생각하는 것과 다를 수 있습니다. 키보드 이벤트는 표시 객체로 이동하지 않으며 포커스가있는 대화 형 객체로 인식됩니다. 마우스를 사용하면 더 간단하지만 키보드는 다소 까다 롭습니다.

 public function Game() 
     { 
      if (stage) onStage(); 
      else addEventListener(Event.ADDED_TO_STAGE, onStage); 
     } 

     private function onStage(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, onStage); 

      // add an event listener to spawn a new ship every frame 
      addEventListener(Event.ENTER_FRAME, loop); 

      stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp); 
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown); 

     } 
관련 문제