piotr Posted October 24, 2016 Share Posted October 24, 2016 Hi all, I have a player sprite that collides with a gun sprite and picks it up. Clicking a button makes the gun drop again on the ground. The code works fine except that the player has always to move some pixels from where the overlap happened otherwise the gun will not drop (the gun sprite is not showing, the function is triggered). I guess my issue is with the positioning code, but I can't figure out what's wrong: this.weapon.x += this.player.x + 50; this.weapon.y += this.player.y - 20; Full code is here Playstate var playState = { create: function() { game.physics.arcade.gravity.y = 1200; this.player = null; this.game.input.keyboard.addKeyCapture([ //working code, omitting for brevity ]); this.cursors = this.game.input.keyboard.createCursorKeys(); this.createWorld(); this.createPlayer(); this.createWeaponCollectible(); game.add.button(game.width - 95, game.height - 20, 'button', this.dropWeapon, this, 2, 1, 0); }, update: function() { game.physics.arcade.collide(this.player, this.layerDirt); game.physics.arcade.collide(this.weapon, this.layerDirt); game.physics.arcade.overlap(this.player, this.weapon, this.collectWeapon, null, this); }, createPlayer: function() { this.player = new Player(this.game,'player'); this.game.add.existing(this.player); }, createWeaponCollectible: function() { this.weapon = new WeaponCollectible(this.game); this.game.add.existing(this.weapon); this.weapon.reset(200,30); }, collectWeapon: function (player, weapon) { //arguments are needed to remove children from group if(!this.player.hasWeapon) { weapon.x = 0; weapon.y = 0; player.addChild(weapon); player.setHasWeapon(true); weapon.visible = false; } }, dropWeapon: function () { if(this.player.hasWeapon) { console.log("it dropped!"); //check if function is triggered this.game.world.add(this.weapon); this.weapon.x += this.player.x + 50; this.weapon.y += this.player.y - 20; this.player.setHasWeapon(false); this.weapon.visible = true; } }, createWorld: function(){ //working code, omitting for brevity }, }; Create weapon var WeaponCollectible = function (game,powerup) { this.powerup = powerup; Phaser.Sprite.call(this, game, 0,0,'weapon01'); game.physics.enable(this, Phaser.Physics.ARCADE); this.anchor.setTo(0.5, 0.5); this.scale.setTo(1); }; WeaponCollectible.prototype = Object.create(Phaser.Sprite.prototype); WeaponCollectible.prototype.constructor = WeaponCollectible; Link to comment Share on other sites More sharing options...
piotr Posted October 31, 2016 Author Share Posted October 31, 2016 Ping Link to comment Share on other sites More sharing options...
samme Posted October 31, 2016 Share Posted October 31, 2016 Is it possible that the item still overlaps the player at the time it's dropped? Link to comment Share on other sites More sharing options...
piotr Posted November 1, 2016 Author Share Posted November 1, 2016 Yes, that's very possible. It's just not clear to me, why the formula works only after I move the player, that means why the weapon overlaps the player around the spot where the collision happened and doesn't some pixels after. Link to comment Share on other sites More sharing options...
Recommended Posts