“访问未定义的属性”错误AS3 FlashPunk

时间:2014-01-25 12:47:36

标签: actionscript-3 flash

我是Flash的新手,我正在尝试在FlashDevelop中学习ActionScript3的FlashPunk。 我一直在使用本教程(http://dev.tutsplus.com/tutorials/an-introduction-to-flashpunk-the-basics--active-9638),我在第10步:碰撞。

它已经说过要向World.as类添加一些代码,但是当我这样做时,我收到错误,“错误:访问未定义的属性_gameEntity”。这也发生在_box上。

我猜这与私有定义的变量有关,因此World.as无法访问它们。

    override public function update():void 
    {
        // update the entities
        var e:Entity = _updateFirst;
        while (e)
        {
            if (e.active)
            {
                if (e._tween) e.updateTweens();
                e.update();
            }
            if (e._graphic && e._graphic.active) e._graphic.update();
            e = e._updateNext;
        }
        //collisions
        super.update();

        var entityArray:Array = [];

        getType("GameEntity", entityArray);

        for each (var entity:Entity in entityArray)
        {
            entity.x = entity.x > 550 ? 550 : entity.x;
            entity.y = entity.y > 400 ? 400 : entity.y;
        }   

        if (_gameEntity.collideWith(_box, _gameEntity.x, _gameEntity.y))
        {
            trace("Collision!");
        }
    }

任何帮助表示赞赏:)

1 个答案:

答案 0 :(得分:0)

确保私有var _gameEntity在GameWorld类声明后的GameWorld.as文件中定义,之前 GameWorld和更新函数。

public class GameWorld extends World 
{
    private var _gameEntity:GameEntity;  <---------IT GOES HERE

    public function GameWorld() 
    {
        // blah blah
    }

    override public function update():void
    {
        // blah blah
    }
}

希望它有所帮助。