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