piotr Posted February 18, 2016 Share Posted February 18, 2016 Hi, I'd like to allow to switch between different player animations and stats based on keyboard input. E.g. pressing the spacebar once, sets the player in an attack stance with different stats, abilities and animations. What should I be putting in "PlayState.prototype.update" in "play.js" Any help is much appreciated Thank you play.js var PlayState = function(game) { this._player = null; }; PlayState.prototype.preload = function() { this.load.spritesheet('player', 'assets/sprites/player.png', 24, 42, 45, 0,3); }; PlayState.prototype.create = function() { this.physics.startSystem(Phaser.Physics.ARCADE); this._player = new Player(this.game, SETTINGS.rest_stance.scale, SETTINGS.rest_stance.speed, SETTINGS.rest_stance.agilty, SETTINGS.rest_stance.attack, SETTINGS.rest_stance.life); this.game.add.existing(this._player); this._spacebar = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); }; PlayState.prototype.update = function() { press spacebar first time > rest_stance, rest_idle and rest_jump animations press spacebar second time = attack_stance, attack_idle and attack_jump animations press spacebar third time = defense_stance, defense_idle and defense_jump animations }; player.js var Player = function (game, scale, speed, agility, attack, life) { this._scale = scale; this._speed = speed; this._agility = agility; this._attack = attack; this._life = agility; Phaser.Sprite.call(this, game, game.width/2, game.height-150, 'player'); this.anchor.setTo(0.5, 1); this.scale.setTo(this._scale); this.animations.add('rest_idle', [0,1,2,3,4], 6, true); this.animations.add('rest_jump', [5,6,7,8,9], 6, true); this.animations.add('attack_idle', [10,11,12,13,14], 6, true); this.animations.add('attack_jump', [15,16,17,18,19], 6, true); this.animations.add('defense_idle', [20,21,22,23,24], 6, true); this.animations.add('defense_jump', [25,26,27,28,29], 6, true); }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; settings.js var SETTINGS = { rest_stance: { cost: 0, scale: 1, speed: 200, agility: 10, attack: 10, life: 100 }, attack_stance: { cost: 10, scale: 1, speed: 300, agility: 20, attack: 20, life: 150 }, defense_stance: { cost: 10, scale: 1, speed: 300, agility: 20, attack: 20, life: 200 }, }; Link to comment Share on other sites More sharing options...
AzraelTycka Posted February 18, 2016 Share Posted February 18, 2016 Hello, I would add into create a one more property ot player, let's say this._player.stance = 'rest'; then checked in update when spacebar is down and changed thi variable to the next one (rest -> attack, ...). Now when you call for an animation you just need some if conditions: if(this._player.stance === 'rest') // do rest animations else if // and so on.. The same conditions then used in wherever it's necessarry. Link to comment Share on other sites More sharing options...
piotr Posted February 18, 2016 Author Share Posted February 18, 2016 Thanks for the reply. This make sense, and seems pretty straightforward for animations. What I miss is how I change e.g. player's scale or velocity based on the stance? They are calculated in different places: scale is set with this.scale.setTo(this._scale); and velocity is calculated with this.body.velocity.x = -this._speed; in Player.prototype.update. I'm wondering if there's a better to do this, e.g. creating a new player "object" every time the space bar is pressed. Thanks Link to comment Share on other sites More sharing options...
AzraelTycka Posted February 18, 2016 Share Posted February 18, 2016 I probably don't understand whre the problem lies. If you need the player to have different properties when yo uswitch stances with spacebar then why don't you just change them as I metioned? In update you can have something like: if(space.isDown) { currentStance = stances[i+1]; } If you need to change velocity and scale with it then jsut change it there as well. if(space.isDown) { currentStance = stances[i+1]; player.velocity.x = newVelocity; player.scale = newScale; } Well, in any case, if you want to create new object on your spacebar while you just need to cycle through three objects then it might be better to just init three objects since the beginning, keep them and just cycle them in some variable such as activePlayer. fillmoreb 1 Link to comment Share on other sites More sharing options...
piotr Posted February 19, 2016 Author Share Posted February 19, 2016 (edited) Thanks. It works. This is my current code. I'm using three buttons instead of the space bar and I tween player's scale between stances. PlayState.prototype.update = function() { if(this._one.isDown) { this._player.stance = 'rest'; } if(this._two.isDown) { this._player.stance = 'attack'; } if(this._three.isDown) { this._player.stance = 'defense'; } switch(this._player.stance) { case 'attack': this.add.tween(this._player.scale).to({ x: SETTINGS.attack_stance.scale, y: SETTINGS.attack_stance.scale}, 500, Phaser.Easing.Quadratic.Out, true); this._player.animations.play('attack_idle'); break; case 'defense': this.add.tween(this._player.scale).to({ x: SETTINGS.defense_stance.scale, y: SETTINGS.defense_stance.scale}, 500, Phaser.Easing.Quadratic.Out, true); this._player.animations.play('defense_idle'); break; default: this.add.tween(this._player.scale).to({ x: SETTINGS.rest_stance.scale, y: SETTINGS.rest_stance.scale}, 500, Phaser.Easing.Quadratic.Out, true); this._player.animations.play('rest_idle'); } }; Edited February 19, 2016 by piobug code formatting is not working Link to comment Share on other sites More sharing options...
Recommended Posts