Jump to content

Picking up and dropping a gun sprite, issues with child/parent positioning


piotr
 Share

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...