sinanqd10 Posted February 9, 2015 Share Posted February 9, 2015 GROUP VS GROUP Sprite will be generated after 3 seconds with velocity.x randomly, but when the sprite generated with slow velocity.x and another new sprite will generate after 3 seconds. The problem is that I cannot collide with the previous sprite that generated with slow velocity. What is my problem? Can anyone please kindly correct me? I hope that someone helps me . Thanks ! create: function() { groupPlayer = game.add.group(); groupPlayer.enableBody = true; player = groupPlayer.create(game.world.centerX - 115, game.world.centerY, 'bananaBounce'); game.physics.arcade.enable(player); player.body.fixedRotation = true; player.body.gravity.y = 1000; player.body.collideWorldBounds = true; player.body.checkCollision = true; player.body.setSize(100, 50, 8, 10); player.animations.add('play'); player.animations.play('play', 25, true); player.smoothed = true; player.anchor.set(0.5); groupPlayer.add(player); game.time.events.loop(Phaser.Timer.SECOND * 3, this.randomSprite, this);},randomSprite: function() { imgRandomGroup = game.add.group(); imgRandomGroup.enableBody = true; imgRandomGroup.physicsBodyType = Phaser.Physics.ARCADE; var imgType = Math.floor(Math.random()*5); sprites = imgRandomGroup.create(game.world.width, game.rnd.between(40, 300), 'level1'); sprites.animations.add('anim', [imgType], 10, true); sprites.animations.play('anim'); game.physics.arcade.enable(sprites); sprites.events.onOutOfBounds.add(this.removeSprite, this); sprites.body.velocity.x -= 50 + Math.random() * 300; imgRandomGroup.add(sprites); },removeSprite: function(sprite) { sprite.kill(); },killSprite: function(sprite) { sprite.kill();},update: function() { game.physics.arcade.collide(imgRandomGroup, groupPlayer, this.killSprite, null, this);} Link to comment Share on other sites More sharing options...
ForgeableSum Posted February 9, 2015 Share Posted February 9, 2015 Are you creating a new sprite group each time you create a randomSprite? You should instead add the randomSprite to the existing imgRandomGroup (created in the create function) rather than recreate it each time. Link to comment Share on other sites More sharing options...
sinanqd10 Posted February 10, 2015 Author Share Posted February 10, 2015 @treedee, thanks you Link to comment Share on other sites More sharing options...
sinanqd10 Posted February 10, 2015 Author Share Posted February 10, 2015 @treedee, Thanks you for answered. It fixed the collision. However, I still got a little bit issue about animation frame. you can see my attached file Flavorite Flavor.zip Link to comment Share on other sites More sharing options...
ForgeableSum Posted February 10, 2015 Share Posted February 10, 2015 The problem is in your killSprite function. This:killSprite: function(sprite) { var indexSprite = sprites.animations.currentFrame.index;var tire = sprites.frame = 0;var hat = sprite.frame = 1;var shoe = sprite.frame = 2;if(indexSprite === tire || indexSprite === hat || indexSprite === shoe) {console.log('reduce time');} else {console.log('add score');}sprite.kill();}Should be this:killSprite: function(sprite) { // sprite.animations.stop(false,false); var indexSprite = sprites.animations.currentFrame.index; if ((indexSprite == 0) || (indexSprite == 1) || (indexSprite == 2)) { console.log('reduce time'); } else { console.log('add score'); } sprite.kill();}It's actually a miracle the game was working with that code. The syntax is wrong for lines like "var tire = sprites.frame = 0;" You're setting the variable tire while trying to set another variable at the same time. Link to comment Share on other sites More sharing options...
sinanqd10 Posted February 11, 2015 Author Share Posted February 11, 2015 @treedee, Thanks you man for correcting the mistake. Its working now. My stupid mistakes made a messy thing . Link to comment Share on other sites More sharing options...
Recommended Posts