2016-08-09 8 views
0

나는 atlas에서 5 개의 이미지 중 1 개를 무작위로 추가하려고하는데, 모두 하나를 다른 것 위에 표시합니다.이 문제를 해결할 수있는 방법이 있습니까? 기본적으로 표시 할 스프라이트 중 1 개는 레벨을 실행할 때마다 5 개가 될 수 있지만 원하는 것은 모두 5 개입니다.Atlas Phaser에서 임의의 스프라이트 표시

///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": [ 
 

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

답변

1

난 정말 당신이 뭘 하려는지 모르겠지만, 내가 프로그래밍 실수 몇가 알고있다.

먼저 당신은 Phaser.Sprite 추가 var에 좋은 figuritaspega에 할당,하지만 당신은 Phaser.Group를 작성하고 같은 VAR figuritaspega에 해당를 할당? 나는 그룹에 대해 별도의 변수를 갖는 것이 더 낫다고 생각한다.

두 번째로 add.group이 함수 호출이므로 함수에 변수를 할당하는 대신 함수 호출임을 나타 내기 위해 ()을 추가해야합니다.

그리고 마지막으로, 당신은 figuraarriba에 스프라이트를 추가 할 때 매개 변수는 1015, 140, this.figuritaspega하지만, 예상 매개 변수는이 spritesheet 인 것처럼 그래서 기본적으로 당신이 스프라이트/그룹 this.figuritaspega를 전달하는, x,y,key,frame (see here를)입니다 ? 그것은 작동하지 않습니다.

내가 말했듯이, 당신이 여기서 무엇을하려고하는지, 그룹에 단 하나의 스프라이트 만 추가하는 이유는 무엇인지 확실하지 않습니다. 하지만이 같은 아마 뭔가 작동합니다 같아요 팁에 대한

// 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; 
+0

덕분에 많이 :)이 (this.rnd.integerInRange (0.4) 를) 도움이 내 오류가 속임수를 썼는지 고정. – Rafahc

관련 문제