Sapper Posted July 17, 2014 Share Posted July 17, 2014 function AddPlayer (uname, startX, startY, player, game) { var x = startX; var y = startY; //add player sprite this.player = game.add.sprite(x, y, 'players'); //scale player down to appropriate size this.player.scale.x = .4; this.player.scale.y = .4; this.player.anchor.set(0.5, 0.5); // give other players physics game.physics.arcade.enable(this.player); // user info this.game = game; this.health = 3; this.alive = true; this.new_x = null; this.new_y = null; //assign 'name' bar above character this.name = uname; this.player.dname = game.add.text(x, y, this.name, { font: "12px Arial", fill: "#330088", align: "center" }); console.log('Added player to client: '+this.name); this.lastPosition = { x: x, y: y } };When I apply the arcade physics, the sprite seems to shoot off the game world in a matter of milliseconds. The sprite isn't in contact with anything else. the AddPlayer() function is being pushed when I receive new information from the socket server. Any ideas? Link to comment Share on other sites More sharing options...
Edricus Posted July 17, 2014 Share Posted July 17, 2014 Does your world happen to have a gravity set on it? Link to comment Share on other sites More sharing options...
Sapper Posted July 17, 2014 Author Share Posted July 17, 2014 No, I haven't touched gravity at all. Link to comment Share on other sites More sharing options...
lewster32 Posted July 17, 2014 Share Posted July 17, 2014 This usually happens because the sprite is not added at the expected phase of the frame (each frame has an execution order where physics are calculated, objects are moved and then rendered in order) and then moved due to anchor or scale being applied, and the movement is interpreted as a sudden change in velocity. Maybe try setting the anchor first, then the scale? Also, try doing this.player.velocity.set(0) at the end - though this may not work for the same reason the object is flying off in the first place. A better way to do this would be to have the creation or enabling of physics deferred to the update loop, so you could maybe keep an array of initialised players, and in the update loop, check that array - if there are any in the array, enable physics on each and remove them from the array. Link to comment Share on other sites More sharing options...
Sapper Posted July 17, 2014 Author Share Posted July 17, 2014 This usually happens because the sprite is not added at the expected phase of the frame (each frame has an execution order where physics are calculated, objects are moved and then rendered in order) and then moved due to anchor or scale being applied, and the movement is interpreted as a sudden change in velocity. Maybe try setting the anchor first, then the scale? Also, try doing this.player.velocity.set(0) at the end - though this may not work for the same reason the object is flying off in the first place. A better way to do this would be to have the creation or enabling of physics deferred to the update loop, so you could maybe keep an array of initialised players, and in the update loop, check that array - if there are any in the array, enable physics on each and remove them from the array. Well, I attempted to use both of your suggestions, but to no avail. For curiosity purposes I tried applying the P2 physics system instead of Arcade and that worked flawlessly, but I'm not really looking to use P2 as everything else in my game uses Arcade. I'm stumped! Link to comment Share on other sites More sharing options...
lewster32 Posted July 17, 2014 Share Posted July 17, 2014 I think in general, it's expected that sprites are created and have their physics applied within the update phase of the frame execution. The deferred method may be best for your situation. That or create a pool of dead player sprites with physics enabled in the create method, then rather than adding the sprite in your AddPlayer method, just grab one from the pool and revive it. Sapper 1 Link to comment Share on other sites More sharing options...
Sapper Posted July 18, 2014 Author Share Posted July 18, 2014 I think in general, it's expected that sprites are created and have their physics applied within the update phase of the frame execution. The deferred method may be best for your situation. That or create a pool of dead player sprites with physics enabled in the create method, then rather than adding the sprite in your AddPlayer method, just grab one from the pool and revive it. Thanks for your help. I used your suggestion for using an array to apply physics and while it didn't work before, I added this.player.body.reset() (see code) after applying the physics and now it works perfectly. for (var i = 0; i < init_physics.length; i++) { if (init_physics[i] == this.name) { log('Attemping initiation of physics for '+init_physics[i]); // give other players physics game.physics.arcade.enable(this.player); this.player.body.reset(this.player.x, this.player.y); init_physics.splice(i, 1); } } lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts