码:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Stage;
stage.addEventListener(KeyboardEvent.KEY_UP,released);
stage.addEventListener(KeyboardEvent.KEY_DOWN,pressed);
addEventListener(Event.ENTER_FRAME,loop);
var isRight = false;
var isLeft = false;
var isSpace = false;
var gravity = 10;
function released(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.RIGHT){
isRight = false;
}
if(event.keyCode == Keyboard.LEFT){
isLeft = false;
}
}
function pressed(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.RIGHT){
isRight = true;
}
if(event.keyCode == Keyboard.LEFT){
isLeft = true;
}
if(event.keyCode == Keyboard.SPACE){
isSpace = true;
}
}
function loop(e:Event)
{
if(player.y <= 496){
player.y += gravity;
}
if(isRight){
player.x += 10;
}
if(isLeft){
player.x += 10;
}
if(isSpace){
if(player.y >= 466){
player.y -= 1;
gravity = -10;
}
else{
gravity = gravity * -1;
isSpace = false;
}
}
}
完整错误是:
TypeError:错误#1009:无法访问空对象引用的属性或方法。 at Untitled_2_fla :: MainTimeline / loop()[Untitled_2_fla.MainTimeline :: frame1:38]
我不明白它试图告诉我什么。 Plz的帮助。谢谢。
我认为在播放器对象初始化之前调用循环函数。所以你应该添加一些小代码。参考这个。
function loop(e:Event)
{
if(!player) return;
if(player.y <= 496){
player.y += gravity;
}
if(isRight){
player.x += 10;
}
if(isLeft){
player.x += 10;
}
if(isSpace){
if(player.y >= 466){
player.y -= 1;
gravity = -10;
}
else{
gravity = gravity * -1;
isSpace = false;
}
}
}