Rafaelx Posted September 1, 2016 Share Posted September 1, 2016 Hi all so I have a small problem, basically I'm trying to kill a moving sprite, it does not matters if it comes from left to right or right to left, but all I want to kill is the one colliding and not the whole group, so to test go to the far right and shoot the block, you will see the new block disappear but not the one that collided, is this possible, I'm providing my game.js below but I created a demo that you can download and test to get a better feel of my problem please help, thanks. Link to Demo BasicGame.Game = function (game) { // When a State is added to Phaser it automatically has the following properties set on it, even if they already exist: this.game; // a reference to the currently running game (Phaser.Game) this.add; // used to add sprites, text, groups, etc (Phaser.GameObjectFactory) this.camera; // a reference to the game camera (Phaser.Camera) this.cache; // the game cache (Phaser.Cache) this.input; // the global input manager. You can access this.input.keyboard, this.input.mouse, as well from it. (Phaser.Input) this.load; // for preloading assets (Phaser.Loader) this.math; // lots of useful common math operations (Phaser.Math) this.sound; // the sound manager - add a sound, play one, set-up markers, etc (Phaser.SoundManager) this.stage; // the game stage (Phaser.Stage) this.time; // the clock (Phaser.Time) this.tweens; // the tween manager (Phaser.TweenManager) this.state; // the state manager (Phaser.StateManager) this.world; // the game world (Phaser.World) this.particles; // the particle manager (Phaser.Particles) this.physics; // the physics manager (Phaser.Physics) this.rnd; // the repeatable random number generator (Phaser.RandomDataGenerator) // You can use any of these from any function within this State. // But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference. this.bulletTimer = 0; }; BasicGame.Game.prototype = { create: function () { //Enable physics // Set the physics system this.game.physics.startSystem(Phaser.Physics.ARCADE); //End of physics // Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out! this.createBullets(); this.createTanque(); this.makeOneBloque(); this.timerBloques = this.time.events.loop(1500, this.makeBloques, this); }, update: function () { if(this.game.input.activePointer.isDown){ this.fireBullet(); } this.game.physics.arcade.overlap(this.bullets, this.bloque, this.collisionBulletBloque, null, this); }, createBullets: function() { this.bullets = this.game.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.ARCADE; this.bullets.createMultiple(100, 'bulletSprite'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 1); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); }, fireBullet: function(){ if (this.bulletTimer < this.game.time.time) { this.bulletTimer = this.game.time.time + 1400; this.bullet = this.bullets.getFirstExists(false); if (this.bullet) { this.bullet.reset(this.tanque.x, this.tanque.y - 20); this.bullet.body.velocity.y = -800; } } }, makeOneBloque: function(){ this.bloquecs = ["bloqueSprite","bloquelSprite"]; this.bloque = this.game.add.group(); for (var i = 0; i < 4; i++){ this.bloque.createMultiple(5, this.bloquecs[Math.floor(Math.random()*this.bloquecs.length)], 0, false); } this.bloque.enableBody = true; this.game.physics.arcade.enable(this.bloque); this.bloque.setAll('anchor.x', 0.5); this.bloque.setAll('anchor.y', 0.5); this.bloque.setAll('outOfBoundsKill', true); this.bloque.setAll('checkWorldBounds', true); }, makeBloques: function(){ this.bloques = this.bloque.getFirstExists(false); if (this.bloques) { this.bloques.reset(0, 300); this.bloques.body.kinematic = true; this.bloques.body.velocity.x = 500; } }, createTanque: function() { this.tanqueBounds = new Phaser.Rectangle(0, 600, 1024, 150); this.tanque = this.add.sprite(500, 700, 'tanqueSprite'); this.tanque.inputEnabled = true; this.tanque.input.enableDrag(true); this.tanque.anchor.setTo(0.5,0.5); this.tanque.input.boundsRect = this.tanqueBounds; }, collisionBulletBloque: function(bullet, bloques) { this.bloques.kill(); this.bullet.kill(); }, quitGame: function (pointer) { // Here you should destroy anything you no longer need. // Stop music, delete sprites, purge caches, free resources, all that good stuff. // Then let's go back to the main menu. this.state.start('MainMenu'); } }; Basic.7z alessandronunes 1 Link to comment Share on other sites More sharing options...
Rafaelx Posted September 2, 2016 Author Share Posted September 2, 2016 Anyone? alessandronunes 1 Link to comment Share on other sites More sharing options...
Milton Posted September 2, 2016 Share Posted September 2, 2016 Don't use 'this.' in the collisionHandler. Your scope is wrong. alessandronunes 1 Link to comment Share on other sites More sharing options...
alessandronunes Posted September 3, 2016 Share Posted September 3, 2016 And if you use physics: sprite.kill(); sprite.body = null; Link to comment Share on other sites More sharing options...
Rafaelx Posted September 9, 2016 Author Share Posted September 9, 2016 Removing .this just makes the game crashed with undefined error Link to comment Share on other sites More sharing options...
Milton Posted September 9, 2016 Share Posted September 9, 2016 Strange. I downloaded your 7z, removed 'this.' from the collisionHandler, and it worked fine... Link to comment Share on other sites More sharing options...
Rafaelx Posted September 12, 2016 Author Share Posted September 12, 2016 On 9/9/2016 at 10:34 AM, Milton said: Strange. I downloaded your 7z, removed 'this.' from the collisionHandler, and it worked fine... Thanks man yes it must be the emulator I was using, you are right, is this Demo well coded? Link to comment Share on other sites More sharing options...
Milton Posted September 12, 2016 Share Posted September 12, 2016 2 hours ago, Rafaelx said: Thanks man yes it must be the emulator I was using, you are right, is this Demo well coded? If it works it is well coded The style is odd, maybe look at an example for a somewhat more usual format. Link to comment Share on other sites More sharing options...
Rafaelx Posted September 13, 2016 Author Share Posted September 13, 2016 Got it Guys thanks man WOOT! Link to comment Share on other sites More sharing options...
Recommended Posts