Jopidia Posted January 3, 2018 Share Posted January 3, 2018 This thread might come across as a little different... Phaser has a lot of great tools and functions that I can use in my games, but I'm having trouble learning exactly what these functions do and how to utilize them. So far, I've been checking the API Documentation and Phaser Search Docs, but I have trouble finding functions that solve my specific problem at the time. I'm not really interested in going through a bunch of tutorials, because I don't believe them to be an effective method of learning how to make games. For reference, they're fine. I'm just curious as to what other people do to look for tools/help. I'm very inexperienced to Phaser AND making games in general, so any help (resources, tips, etc.) would be greatly appreciated! Here's a good example issue: Right now, I'm making a simple overhead game where my sprite can move using WASD. Everything is working fine and dandy - however, when the sprite moves diagonally (up and right at the same time), it moves faster (more pixels per second) than it does when moving straight up, down, left, or right (code shown below). What resources would you look through to find tools to solve a problem like that? Or would the best way be to change the x and y velocity when two directions are pushed at once so that the diagonal speed is equal to the max velocity (in other words, use math)? Please excuse my noobism. create: function() { // Prepares keyboard for input keyboard = this.game.input.keyboard; player1 = this.game.add.sprite(16, 16, "player1"); game.physics.enable(player1, Phaser.Physics.ARCADE); player1.body.collideWorldBounds = true; }, update: function() { // Sprite can move (W, A, S, D) if (keyboard.isDown(Phaser.Keyboard.A)) { player1.body.velocity.x = -175; } else if (keyboard.isDown(Phaser.Keyboard.D)) { player1.body.velocity.x = 175; } else { player1.body.velocity.x = 0; } if (keyboard.isDown(Phaser.Keyboard.W)) { player1.body.velocity.y = -175; } else if (keyboard.isDown(Phaser.Keyboard.S)) { player1.body.velocity.y = 175; } else { player1.body.velocity.y = 0; } }, Link to comment Share on other sites More sharing options...
Adel Posted January 25, 2018 Share Posted January 25, 2018 I'm a beginner too, to handle 8 direction movements in a vertical space shooter I'm building to learn more about phaser API what I used is // Player extends Phaser.Sprite /** * Player movement handler * @param {Object} direction */ Player.prototype.move = function(direction) { this.body.velocity.x = direction.left ? -1 * Player.SPEED : direction.right ? Player.SPEED : 0; this.body.velocity.y = direction.up ? -1 * Player.SPEED : direction.down ? Player.SPEED : 0; }; // An instance of Player {Phaser.Sprite} is created in PlayState {Phaser.State} create hook PlayState.init = function() { this.keys = this.game.input.keyboard.addKeys({ left: Phaser.KeyCode.LEFT, right: Phaser.KeyCode.RIGHT, up: Phaser.KeyCode.UP, down: Phaser.KeyCode.DOWN, shoot: Phaser.KeyCode.SPACEBAR }); }; PlayState.update = function() { this._handleArrowsDirections(); }; PlayState._handleArrowsDirections = function() { this.player.move({ up: this.keys.up.isDown, down: this.keys.down.isDown, left: this.keys.left.isDown, right: this.keys.right.isDown }); }; Link to comment Share on other sites More sharing options...
Recommended Posts