我试图随机地在地图册中添加5张图片中的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$"
}
}
答案 0 :(得分:1)
我真的不知道你在这里想做什么,但我知道有一些编程错误。
首先添加Phaser.Sprite
并将其分配给var figuritaspega
哪个好,但是您创建了Phaser.Group
并将其分配给同一个var figuritaspega
?我认为为该组提供单独的变量会更好。
其次,add.group
是函数调用,因此您应该添加()
来指示它是函数调用而不是将函数赋值给变量。
最后,当您向figuraarriba
添加精灵时,参数为1015,140,this.figuritaspega,但预期参数为x,y,key,frame
(see here),所以基本上你是传递sprite / group this.figuritaspega
就好像它是spritesheet一样?那不会起作用。
就像我说的那样,我不完全确定你在这里要做什么,或者只为一个组添加一个精灵的原因是什么?但我想也许这样的事情会起作用:
// 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;