代码之家  ›  专栏  ›  技术社区  ›  P. Frank

动画在Phaser中不启动一次。js公司

  •  0
  • P. Frank  · 技术社区  · 8 年前

    我使用相位器2.6.1。

    我已经创建了一个动画。这很好,但有时动画没有启动。我不知道为什么。

    在里面 create() 功能,我为精灵表添加动画:

    this.car = game.add.sprite(game.world.centerX,500, 'car');
    this.car.animations.add('mover',[0,1,2,3],1000, true);
    this.car.fixedToCamera = true;
    

    当重叠时,我启动函数来设置动画:

    game.physics.arcade.overlap(this.car, this.borders, this.canMove, null, this);
    

    这是我的功能:

    },canMove: function(car, objet){
        if(crash == 0){
            crash = 1;
            _this.car.animations.play("mover");
                    console.log("fire")
            setTimeout(function(){
                _this.car.animations.stop();
                _this.car.frame = 0;
                crash = 0;
            },1000)
        }
    }
    

    当汽车与边界重叠时,会触发此功能。

    这个 console.log() 被触发,但动画突然没有启动。我可以重新启动我的页面很多时间,突然动画没有启动,即使 安慰日志() 已触发。

    1 回复  |  直到 8 年前
        1
  •  0
  •   P. Frank    8 年前

    问题是因为我首先用以下工具初始化了我的车:

    this.car = game.add.sprite(game.world.centerX,500, 'car');
    this.car.animations.add('mover',[0,1,2,3],1000, true);
    this.car.fixedToCamera = true;
    _this = this
    

    在我的功能中,我直接用 _this

    我用过 _这个 因为里面 setTimeout() , this 属性不等于 文件的

    我不知道为什么,但如果我先初始化 在我打电话给 这不太好。为了解决我的问题,我从 _这个 直接地

    _this = this;
    _this.car = game.add.sprite(game.world.centerX,500, 'car');
    _this.car.animations.add('mover',[0,1,2,3],1000, true);
    _this.car.fixedToCamera = true;