Ali Malik Posted September 29, 2014 Share Posted September 29, 2014 (edited) See if i have a player and i want to add a gun that follows him, what should i do?Even after addingplayer.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 October 2, 2014 by Ali Malik Link to comment Share on other sites More sharing options...
CtlAltDel Posted September 29, 2014 Share Posted September 29, 2014 player.addChild(gun) that will place the gun relative to the player Link to comment Share on other sites More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 it's not working.. i can attach my file please customize as you want? can you?http://hotptc.org/testing/phaser/my_game.zip Link to comment Share on other sites More sharing options...
valueerror Posted September 29, 2014 Share Posted September 29, 2014 using P2 physics i ended up using a revolute constraints to fix the gun to my player.. really flexible solution and you can make the player drop the gun easily Link to comment Share on other sites More sharing options...
lewster32 Posted September 29, 2014 Share Posted September 29, 2014 Your gun has physics enabled on it, which does not work well when trying to use addChild on another physics enabled sprite. You either need to not use physics on the gun, or you need to set gun.body.moves = false. Link to comment Share on other sites More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 @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 More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 @valueerror, Please tell me more about revolute constraints ... Link to comment Share on other sites More sharing options...
lewster32 Posted September 29, 2014 Share Posted September 29, 2014 You need to do character.addChild(gun) and then it'll work. Revolute constraints are no good if you're using Arcade Physics. Link to comment Share on other sites More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 still same ... character = game.add.group(); character.add(player); character.add(gun); character.addChild(gun); Link to comment Share on other sites More sharing options...
lewster32 Posted September 29, 2014 Share Posted September 29, 2014 Oh wait sorry, misread - player.addChild(gun) Link to comment Share on other sites More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 By adding line player.addChild(gun); game freezes on it's start Link to comment Share on other sites More sharing options...
valueerror Posted September 29, 2014 Share Posted September 29, 2014 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 More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 http://hotptc.org/testing/phaser/index.html here is the demo Link to comment Share on other sites More sharing options...
Ali Malik Posted September 29, 2014 Author Share Posted September 29, 2014 http://jsfiddle.net/AliMalik/ru0g7pz5/1/here is the JS Fiddle link Link to comment Share on other sites More sharing options...
valueerror Posted September 30, 2014 Share Posted September 30, 2014 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 spriteyou 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 More sharing options...
Ali Malik Posted October 1, 2014 Author Share Posted October 1, 2014 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 spriteyou 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 More sharing options...
j0hnskot Posted October 1, 2014 Share Posted October 1, 2014 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 More sharing options...
Ali Malik Posted October 2, 2014 Author Share Posted October 2, 2014 Nope not working bro Link to comment Share on other sites More sharing options...
Ali Malik Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
j0hnskot Posted October 2, 2014 Share Posted October 2, 2014 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 examplegun = game.add.sprite(45, 55, 'DE'); Link to comment Share on other sites More sharing options...
Recommended Posts