mkbgr Posted December 24, 2020 Share Posted December 24, 2020 Hello everyone, When I put that codes on update() function,ship is not render or create anymore.But when I don't use codes that handling player inputs it's creating the ship. var config = { type: Phaser.AUTO, parent: 'phaser-example', width: 800, height: 600, physics: { default: 'arcade', arcade: { fps:60, debug: false, gravity: { y: 0 } } }, scene: { preload: preload, create: create, update: update } }; var sprite; var cursors; var text; var game = new Phaser.Game(config); function preload() { this.load.image('ship', 'assets/PNG/playerShip2_orange.png'); this.load.image('otherPlayer', 'assets/PNG/playerShip1_blue.png'); } function create() { var self = this; this.socket = io(); this.otherPlayers = this.physics.add.group(); this.socket.on('currentPlayers', function (players) { Object.keys(players).forEach(function (id) { if (players[id].playerId === self.socket.id) { addPlayer(self, players[id]); } else { addOtherPlayers(self, players[id]); } }); }); this.socket.on('newPlayer', function (playerInfo) { addOtherPlayers(self, playerInfo); }); this.socket.on('userDisconnect', function (playerId) { self.otherPlayers.getChildren().forEach(function (otherPlayer) { if (playerId === otherPlayer.playerId) { otherPlayer.destroy(); } }); }); this.cursors = this.input.keyboard.createCursorKeys(); this.socket.on('playerMoved', function (playerInfo) { self.otherPlayers.getChildren().forEach(function (otherPlayer) { if (playerInfo.playerId === otherPlayer.playerId) { otherPlayer.setRotation(playerInfo.rotation); otherPlayer.setPosition(playerInfo.x, playerInfo.y); } }); }); } function addPlayer(self, playerInfo) { self.ship = self.physics.add.image(playerInfo.x, playerInfo.y, 'ship').setOrigin(0.5, 0.5).setDisplaySize(53, 40); if (playerInfo.team === 'blue') { self.ship.setTint(0x0000ff); } else { self.ship.setTint(0xff0000); } self.ship.setDrag(100); self.ship.setAngularDrag(100); self.ship.setMaxVelocity(200); } function update() { if (this.ship) { var x = this.ship.x; var y = this.ship.y; var r = this.ship.rotation; if (this.ship.oldPosition && (x !== this.ship.oldPosition.x || y !== this.ship.oldPosition.y || r !== this.ship.oldPosition.rotation)) { this.socket.emit('playerMovement', { x: this.ship.x, y: this.ship.y, rotation: this.ship.rotation }); } // save old position data this.ship.oldPosition = { x: this.ship.x, y: this.ship.y, rotation: this.ship.rotation }; if (this.cursors.left.isDown) { this.ship.setAngularVelocity(-150); } else if (this.cursors.right.isDown) { this.ship.setAngularVelocity(150); } else { this.ship.setAngularVelocity(0); } if (this.cursors.up.isDown) { this.physics.velocityFromRotation(this.ship.rotation + 1.5, 100, this.ship.body.acceleration); } else { this.ship.setAcceleration(0); } this.physics.world.wrap(this.ship, 5); } } function addOtherPlayers(self, playerInfo) { const otherPlayer = self.add.sprite(playerInfo.x, playerInfo.y, 'otherPlayer').setOrigin(0.5, 0.5).setDisplaySize(53, 40); if (playerInfo.team === 'blue') { otherPlayer.setTint(0x0000ff); } else { otherPlayer.setTint(0xff0000); } otherPlayer.playerId = playerInfo.playerId; self.otherPlayers.add(otherPlayer); } game.js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); var players = {}; app.use(express.static(__dirname + '/public')); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function (socket) { console.log('a user connected'); // create a new player and add it to our players object players[socket.id] = { rotation: 0, x: Math.floor(Math.random() * 700) + 50, y: Math.floor(Math.random() * 500) + 50, playerId: socket.id, team: (Math.floor(Math.random() * 2) == 0) ? 'red' : 'blue' }; // send the players object to the new player socket.emit('currentPlayers', players); // update all other players of the new player socket.broadcast.emit('newPlayer', players[socket.id]); socket.on('disconnect', function () { console.log('user disconnected'); // remove this player from our players object delete players[socket.id]; // emit a message to all players to remove this player io.emit('userDisconnect', socket.id); }); // when a player moves, update the player data socket.on('playerMovement', function (movementData) { players[socket.id].x = movementData.x; players[socket.id].y = movementData.y; players[socket.id].rotation = movementData.rotation; // emit a message to all players about the player that moved socket.broadcast.emit('playerMoved', players[socket.id]); }); }); server.listen(8081, function () { console.log(`Listening on ${server.address().port}`); }); server.js Link to comment Share on other sites More sharing options...
Recommended Posts