在flash中,我收到一个错误,它给了我两个错误。
场景1,图层'好人',第1帧,第4行1120:访问未定义的属性PressSPACEKey。
场景1,图层'好人',第1帧,第4行1120:访问未定义属性ReleaseSPACEKey。
我正在尝试使用键盘键移动好人。
我将所有Mouse
更改为Keyboard
。我仔细研究了一下,确保没有鼠标。
这是我的代码:
var KeyboardIsDown = false;
var velocity = 0
var score = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressSPACEKey);;
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseSPACEKey);
function pressed (n:KeyboardEvent)
{
KeyboardIsDown = true;
}
function unpressed (n:KeyboardEvent)
{
KeyboardIsDown = false;
}
addEventListener(Event.ENTER_FRAME, mainLoop);
function mainLoop (e:Event)
{
output.text = "Score: " + score;
score += 1
if (KeyboardIsDown)
{
if (velocity < -10)
{
velocity = -10;
}
gg_mc.y -= velocity
}
else
{
gg_mc.y += velocity;
}
velocity += 0.03
; //yes, this is on a new line
for (var I = 0; I < numChildren; I++)
{
if (getChildAt(I) is bad || getChildAt(I) is B)
{
var b = getChildAt(I) as MovieClip;
if (b.hitTestObject(gg_mc))
{
var explosion = new boom ();
explosion.x = gg_mc.x;
explosion.y = gg_mc.y;
addChild(explosion);
}
}
}
}
答案 0 :(得分:1)
你只需将这两个短语放在那里,但你从未指明它们是什么。
错误基本上意味着:
WTF是PressSPACEKey ???
addEventListener是一个将另一个 FUNCTION 作为第二个参数的函数。所以无论PressSPACEKey
是什么,它都应该是一个函数。但是,你再也没有在任何地方宣布它。
完全与此无关,您的代码中有两个 FUNCTIONS ,名为pressed
和unpressed
。
我不确定为什么我添加了这句最后一句,我也怀疑为什么我帖子中的某些单词是粗体和大写的。不知道,真的......
答案 1 :(得分:1)
错误地说明属性不正确。
var KeyboardIsDown = false;
var velocity = 0
var score = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unpressed);
function pressed (n:KeyboardEvent)
{
KeyboardIsDown = true;
}
function unpressed (n:KeyboardEvent)
{
KeyboardIsDown = false;
}
addEventListener(Event.ENTER_FRAME, mainLoop);
function mainLoop (e:Event)
{
output.text = "Score: " + score;
score += 1
if (KeyboardIsDown)
{
if (velocity < -10)
{
velocity = -10;
}
gg_mc.y -= velocity
}
else
{
gg_mc.y += velocity;
}
velocity += 0.03
; //yes, this is on a new line
for (var I = 0; I < numChildren; I++)
{
if (getChildAt(I) is bad || getChildAt(I) is B)
{
var b = getChildAt(I) as MovieClip;
if (b.hitTestObject(gg_mc))
{
var explosion = new boom ();
explosion.x = gg_mc.x;
explosion.y = gg_mc.y;
addChild(explosion);
}
}
}
}