Adobe Animate CC动态文本空错误

时间:2016-12-15 15:32:51

标签: actionscript-3 flash adobe

我收到此错误

" TypeError:错误#1009:无法访问空对象引用的属性或方法。     在sole_fla :: MainTimeline / game()"

我似乎无法在我创建的动态文本框中显示我的分数,我将其命名为" scoretext"

这是我的代码

import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;


addEventListener(Event.ENTER_FRAME, game);
addEventListener(MouseEvent.CLICK, onClick);

var score:int = 0;
var high:int = 0;

const gravity:Number = 2;
const force: Number = 30;
const lyfe: Number = 100;
var yspeed: Number = 249;
var life: Number = 0;

function onClick(event:MouseEvent):void
{
     //just testing if mouse input is detected
     trace("The event handler works!");
}





//game main loop
function game(event: Event) {


    score = 0;
    life = lyfe;
    yspeed = yspeed + gravity;
    player.y = yspeed;

    if(player.y - player.height/2 < 0)
            player.y = player.height/2;

    for (var i = 0; i < numChildren; i++) {


    //test if mons hit player
    if (mons.hitTestObject(player)){
        life = life - 10;

        trace("hit");
    }
    //test if starz hit player
    if (starz.hitTestObject(player)){

       //I believe this is the part where it gets the error
       //this is my scoretext dynamic text box to display the score
        scoretext.text = score.toString();
        ++score;

    }


}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler_2);

function fl_KeyboardDownHandler_2(event:KeyboardEvent):void
{
if(event.keyCode == 32){

    yspeed = yspeed - force;
    trace("Key Code Pressed: " + event.keyCode);
    player.gotoAndPlay(41);
}

}

1 个答案:

答案 0 :(得分:0)

更改帧与持久事件侦听器不能很好地结合在一起。 执行侦听器,但如果对象不在当前帧上,则对对象的引用为null

在应用程序中切换状态时,通过删除事件侦听器来正确终止先前的状态。