Phaser JS - 按钮无法从图像精灵

时间:2017-05-24 12:00:32

标签: javascript phaser-framework

作为我使用phaser练习的一部分,我正在尝试创建一个基本的点击游戏。但是,我无法按下按钮来显示我想要的图像。

这是我的主要 gameplay.js

class Gameplay extends Phaser.State {

init()
{
  var btnUp = this.game.add.sprite(0, 0, 'btn_beerUp');
  var btnDn = this.game.add.sprite(0, 0, 'btn_beerDn');
  this.clickButton = this.game.add.button(this.game.world.centerX - 100, this.game.world.centerY + 230, 'game', this.pauseGame, this, btnUp.key, btnUp.key, btnDn.key);
  this.clickButton.anchor.set(0.5, 0.5);
}

这是我的 preloader.js (理想情况下,所有图片都已加载):

class Preload extends Phaser.State {
  create() {
      this.game.load.image('btn_beerDn', 'res/img/btn_beerClickerDn.png');
      this.game.load.image('btn_beerUp', 'res/img/btn_beerClickerUp.png');
  }

它没有按预期工作。游戏只是将btnUpbtnDn添加为场景中的图像,并且不对该按钮执行任何操作。

我也尝试了以下内容:

this.clickButton = this.game.add.button(blah-blah, 'btn_beerUp', 'btn_beerUp', 'btn_beerDn');

this.clickButton = this.game.add.button(blah-blah, 'res/img/btn_beerClickerUp.png', 'res/img/btn_beerClickerUp.png', 'res/img/btn_beerClickerDn.png');

但它们都不起作用 - 按钮仍然显示一个带有“x”的按钮。

在线样本主要处理我可以查看的地图册。虽然我最终会进一步使用atlas表格来制作按钮,但似乎很愚蠢,我不能简单地使用png工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

根据the documentation,如果您不打算传递框架或框架名称,则必须使用loadTexture

如果查看Action On Click示例,您将看到如何为每个按钮状态添加操作。

在您的情况下,您想要更改此行:

this.clickButton = this.game.add.button(this.game.world.centerX - 100, this.game.world.centerY + 230, 'game'
    , this.pauseGame, this, btnUp.key, btnUp.key, btnDn.key);

如下所示:

this.clickButton = this.game.add.button(this.game.world.centerX - 100, this.game.world.centerY + 230, 'game'
    , this.pauseGame, this);
this.clickButton.onInputOut.add(this.out, this);
// TODO add your other input events.

out: function () {
    this.clickButton.loadTexture(btnUp.key);
}
// TODO add functions for your other input events.

完整的工作示例:



var mainState = {
    preload: function() {
    	// Load the three sprites that they can choose between.
      this.load.crossOrigin = 'anonymous';
      this.load.image('ball', 'https://raw.githubusercontent.com/photonstorm/phaser-examples/master/examples/assets/sprites/orb-blue.png');
      this.load.image('ball2', 'https://raw.githubusercontent.com/photonstorm/phaser-examples/master/examples/assets/sprites/orb-green.png');
      this.load.image('ball3', 'https://raw.githubusercontent.com/photonstorm/phaser-examples/master/examples/assets/sprites/orb-red.png');
    },

    create: function() {
      // This won't work, since the passed items aren't valid frameNames.
      //this.clickButton = this.game.add.button(this.game.world.centerX, this.game.world.centerY, 'ball', this.buttonClick, this, 'ball2', 'ball', 'ball3', 'ball');
      // This follows what the documentation states, if you're not using a spritesheet.
      this.clickButton = this.game.add.button(this.game.world.centerX, this.game.world.centerY, 'ball', this.buttonClick, this);
      
      this.clickButton.onInputOver.add(this.buttonOver, this);
      this.clickButton.onInputOut.add(this.buttonOut, this);
      this.clickButton.onInputDown.add(this.buttonDown, this);
      this.clickButton.onInputUp.add(this.buttonUp, this);

			this.clickButton.anchor.setTo(0.5);
    },
    update: function() {
    },
    buttonClick: function() {
      alert('clicked');
 		},
    buttonOver: function() {
      this.clickButton.loadTexture('ball2');
    },
    buttonOut: function() {
      this.clickButton.loadTexture('ball');
    },
    buttonDown: function() {
      this.clickButton.loadTexture('ball3');
    },
    buttonUp: function() {
      this.clickButton.loadTexture('ball');
    }
};

var game = new Phaser.Game(200, 200);

game.state.add('main', mainState);

game.state.start('main');

<script src="https://cdn.jsdelivr.net/npm/phaser-ce@2.7.10"></script>
&#13;
&#13;
&#13;

也保存为a JSFiddle