TypeError:错误#1009:无法访问null对象引用as3的属性或方法

问题描述 投票:-1回答:1

码:

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的帮助。谢谢。

actionscript-3 flash sprite
1个回答
0
投票

我认为在播放器对象初始化之前调用循环函数。所以你应该添加一些小代码。参考这个。

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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.