Sapper Posted July 16, 2014 Share Posted July 16, 2014 I'm creating a multiplayer game with Phaser and using my own PHP socket and I can't seem to get the client side character to position correctly on the screen.function create() { websocket = new WebSocket(wsUri); // Modify the world and camera bounds game.world.setBounds(-1000, -1000, 2000, 2000); land = game.add.tileSprite(0, 0, 800, 600, 'earth'); land.fixedToCamera = true; //create player and add his walking animations player = game.add.sprite(150, 150, 'dude'); player.scale.x = .4; player.scale.y = .4; // We need to enable physics on the player game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.enable(player); player.body.collideWorldBounds = true; players = []; game.camera.follow(player); game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300); game.camera.focusOnXY(150, 150); player.name = ''; player.dname = game.add.text(player.x, player.y, "guest", { font: "12px Arial", fill: "#800028", align: "center" }); cursors = game.input.keyboard.createCursorKeys(); setEventHandlers();}function addPlayer (uname, startX, startY) { players.push(new AddPlayer(uname, startX, startY, player, game));}function AddPlayer (uname, startX, startY, player, game) { var x = startX; var y = startY; this.game = game; this.health = 3; this.player = player; this.alive = true; this.player = game.add.sprite(x, y, 'players'); this.name = uname; this.player.scale.x = .4; this.player.scale.y = .4; 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 }}; Basically, in simple terms: player = game.add.sprite(150, 150, 'dude'); in the create() function and: this.player = game.add.sprite(150, 150, 'dude'); in the addPlayer(); function (which is populated by the server at a starting point of 150, 150) yields two completely different locations on the map as evidenced by: Literally, I'm going crazy over how such a phenomenon could happen. Any ideas? Link to comment Share on other sites More sharing options...
Sapper Posted July 16, 2014 Author Share Posted July 16, 2014 I think I've figured out what is actually causing the discrepancy and it's applying the game physics system to the sprite. Why would this cause a discrepancy in position? Link to comment Share on other sites More sharing options...
lewster32 Posted July 16, 2014 Share Posted July 16, 2014 Adding P2 physics to a sprite sets its anchor to 0.5, 0.5. Because the first one has physics and the second does not, the first has its anchor altered from the default of 0, 0. Sapper 1 Link to comment Share on other sites More sharing options...
Sapper Posted July 16, 2014 Author Share Posted July 16, 2014 Thank you so much. lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts