【问题标题】:Displaying random sprite from Atlas Phaser显示来自 Atlas Phaser 的随机精灵
【发布时间】:2016-08-11 04:01:45
【问题描述】:

我正在尝试在 atlas 中随机添加 5 个图像中的 1 个,但它们都显示为一个在另一个之上,有没有办法解决这个问题?基本上我希望每次运行我的关卡时显示的精灵中的 1 个可以是 5 个中的任何一个,但我得到的只是同时显示的全部 5 个。

///Declaration

 this.load.atlas('Monsters', 'images/monsters.png', 'images/monsters.json');


////Where I call sprite

 this.figuritaspega = this.game.add.sprite(0, 0, 'Monsters');
        this.figuritaspega.frame = this.rnd.integerInRange(0,4);
         this.figuritaspega = this.game.add.group;
        this.figuraarriba = this.add.sprite(1015, 140, this.figuritaspega);
        this.figuraarriba.scale.set(0.9 , 0.9 );

////.json below

{"frames": [

{
	"filename": "amarillo.png",
	"frame": {"x":0,"y":0,"w":188,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":188,"h":200},
	"sourceSize": {"w":188,"h":200}
},
{
	"filename": "azul.png",
	"frame": {"x":188,"y":0,"w":240,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":240,"h":200},
	"sourceSize": {"w":240,"h":200}
},
{
	"filename": "naranja.png",
	"frame": {"x":428,"y":0,"w":162,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":162,"h":200},
	"sourceSize": {"w":162,"h":200}
},
{
	"filename": "rojo.png",
	"frame": {"x":590,"y":0,"w":190,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":190,"h":200},
	"sourceSize": {"w":190,"h":200}
},
{
	"filename": "rosa.png",
	"frame": {"x":780,"y":0,"w":231,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":231,"h":200},
	"sourceSize": {"w":231,"h":200}
}],
"meta": {
	"app": "http://www.codeandweb.com/texturepacker",
	"version": "1.0",
	"image": "monsters.png",
	"format": "RGBA8888",
	"size": {"w":1011,"h":200},
	"scale": "1",
	"smartupdate": "$TexturePacker:SmartUpdate:41785e106df91b6daf42364753f15c41:5fca3c08999ac8d93eabfac98fafaf65:8fc4d3ec51ba7bc700054b5f64cf62b1$"
}
}

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    我真的不知道你想在这里做什么,但我知道有几个编程错误。

    首先您添加一个Phaser.Sprite 并将其分配给var figuritaspega,这很好,但随后您创建一个Phaser.Group 并将它也分配给同一个var figuritaspega?我认为最好为组设置一个单独的变量。

    其次,add.group 是一个函数调用,因此您应该添加 () 以表明它是一个函数调用,而不是将函数分配给变量。

    最后,当您将精灵添加到figuraarriba 时,参数是 1015、140、this.figuritaspega,但预期的参数是 x,y,key,frame (see here),所以基本上您是在传递精灵/组 @ 987654330@ 好像它是一个精灵表?那是行不通的。

    就像我说的,我不完全确定您在这里要做什么,或者只将一个精灵添加到组中的原因是什么?但我想也许这样的事情会起作用:

    // create a sprite, random frame 0..4
    this.figuritaspega = this.game.add.sprite(0, 0, 'Monsters');
    this.figuritaspega.frame = this.rnd.integerInRange(0,4);
    
    // create a group
    this.mysprites = this.game.add.group(); // <- function call
    this.mysprites.add(this.figuritaspega);
    
    // scale entire group and reposition group
    this.mysprites.scale.set(0.9 , 0.9 );
    
    // notice that the sprite position is relative to the group position
    this.mysprites.x = 10;
    this.mysprites.y = 20;
    

    【讨论】:

    • 感谢您的提示帮助很大:),这个 (this.rnd.integerInRange(0,4); ) 并修复了我的错误。
    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2017-05-27
    • 2019-01-12
    相关资源
    最近更新 更多