Jump to content

Need a little help with player


Ali Malik
 Share

Recommended Posts

See if i have a player and i want to add a gun that follows him, what should i do?
Even after adding
player.addChild(gun); it doesn't follow it.. :/

[EDITED]
Take a look at code in JSFiddle : http://jsfiddle.net/AliMalik/ru0g7pz5/1/

 

Currently using this code as Default : http://jsfiddle.net/AliMalik/snctgm5q/5/

Edited by Ali Malik
Link to comment
Share on other sites

@lewster32, i'm using this code
 

character.setAll('body.velocity.x', 0);		if (cursors.left.isDown)	{		character.setAll('body.velocity.x', -150);		player.animations.play('left');	}	else if (cursors.right.isDown)	{                character.setAll('body.velocity.x', 150);		player.animations.play('right');	}	else	{		player.animations.stop();		player.frame = 4;	}		if (cursors.up.isDown && player.body.touching.down)	{		character.setAll('body.velocity.y', -350);	}
character = game.add.group();	character.add(player);	character.add(gun);

and with gun.body.moves = false; that gun doesn't move with hero.. :/

Link to comment
Share on other sites

well ...  reading out of your code you are using arcade physics...  wich is oke but it does not support any sort of constraint  between two physics bodies afaik.

 

if you could provide a js fiddle for example or even an online demo of your code it woud be easier to help .. we could read the code and see the errormessages..

Link to comment
Share on other sites

just looked over your demo:

this.gun = this.game.add.sprite(x, y, player.x + 45, player.y + 55, 'DE');

this is wrong..   player.x + 45, player.y + 55  are your x and y coordinates..  delete x, y so it looks like this:

this.gun = this.game.add.sprite(player.x + 45, player.y + 55, 'DE');

then you are working with "character"   which is intitalized as simple var character;

 

you can not enable physics on that..   character needs to be a sprite

you also can not call character.setAll    .. in that case character needs to be a group

 

 

if you attach the gun to the player, move the player directly.. the gun should move with it..  no need for a character group then.

Link to comment
Share on other sites

just looked over your demo:

this.gun = this.game.add.sprite(x, y, player.x + 45, player.y + 55, 'DE');

this is wrong..   player.x + 45, player.y + 55  are your x and y coordinates..  delete x, y so it looks like this:

this.gun = this.game.add.sprite(player.x + 45, player.y + 55, 'DE');

then you are working with "character"   which is intitalized as simple var character;

 

you can not enable physics on that..   character needs to be a sprite

you also can not call character.setAll    .. in that case character needs to be a group

 

 

if you attach the gun to the player, move the player directly.. the gun should move with it..  no need for a character group then.

But look on other side.. when i press jump button.. then gun activates jump velocity.. and it never stops.. it goes up!

Link to comment
Share on other sites

You shouldn't allow physics on the gun.

 

You want to have a character which have a gun on it.

player = game.add.sprite(32, game.world.height - 150, 'dude'); // as you did in your code// enable physics on player and set the values as you like //then create the gun and place it where it needs to be.//keep in mind that since you will make the gun a child of the player sprite, the x,y coordinates will be relative to the player sprite and not the worldsgun = game.add.sprite( 45, 55,'gun');//then make the gun be a child of player. Don't apply any physics to it!player.addChild(gun);//gun will now follow player around.

The gun was flying and never stopped because you applied velocity to the whole group instead of only the character (there is no need for a group for only one child)

 

So change this :

	if (cursors.up.isDown && player.body.touching.down)	{		// character.body.velocity.y = -350		character.setAll('body.velocity.y', -350);	}

to this :

	if (cursors.up.isDown && player.body.touching.down)	{		player.body.velocity.y = -350;			}
Link to comment
Share on other sites

 

You shouldn't allow physics on the gun.

 

You want to have a character which have a gun on it.

player = game.add.sprite(32, game.world.height - 150, 'dude'); // as you did in your code// enable physics on player and set the values as you like //then create the gun and place it where it needs to be.//keep in mind that since you will make the gun a child of the player sprite, the x,y coordinates will be relative to the player sprite and not the worldsgun = game.add.sprite( 45, 55,'gun');//then make the gun be a child of player. Don't apply any physics to it!player.addChild(gun);//gun will now follow player around.

The gun was flying and never stopped because you applied velocity to the whole group instead of only the character (there is no need for a group for only one child)

 

 

It's not working :/ gun is still in air after created.. and not moving with player

Link to comment
Share on other sites

In this link http://jsfiddle.net/AliMalik/snctgm5q/5/

 

You don't add the gun as a child of the player .

 

Be sure to add this after the creation of the gun:

 player.addChild(gun);

If you add it, the gun will go far away from the player since you use :

gun = game.add.sprite(player.x + 45, player.y + 55, 'DE');

This will because once the gun is a child of the player it will get his x and y coordinates. So you must only pass the offset you want. For example

gun = game.add.sprite(45, 55, 'DE');
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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