Jump to content

Swapping between 2 animated textures


justgage
 Share

Recommended Posts

I'm very much a n00b at Phazer and I come from a gamemaker background. Basically I'm making a platformer in which the player has a walking animation and a standing animation and I don't know how to properly switch between the two in a way that preserves the animation. all the animations are in seperate pngs. Here's what I got so far:

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() {   game.load.image('sky', 'assets/sky.png');   game.load.image('ground', 'assets/platform.png');   game.load.image('star', 'assets/star.png');   game.load.spritesheet('df-run', 'assets/Darkfire-run.png', 32, 32);   game.load.spritesheet('df', 'assets/Darkfire.png', 32, 32);}var player;var platforms;var cursors;function create() {...   // The player and its settings   player = game.add.sprite(32, game.world.height - 150, 'df');   player.animations.add('df-standing');   player.animations.play('df-standing', 50, true);   player.anchor.x =0.5;   player.anchor.x =0.5;...   //  Our controls.   cursors = game.input.keyboard.createCursorKeys();   cursors.right.onDown.add(function () {      console.log("down right", 0);      player.loadTexture('df-run');   });   cursors.left.onDown.add(function () {      player.loadTexture('df-run', 0);   });   cursors.right.onUp.add(function () {      console.log("up right");      player.loadTexture('df', 0, false);   });}function update() {...}
Link to comment
Share on other sites

I figured out the way to make it happen, for some reason you need to switch the texture before you set up the new animation. See the example code below:

function preload() {   // both of these are animated    game.load.spritesheet('df-run', 'assets/kamen-walk.png', 30, 30);   game.load.spritesheet('df', 'assets/kamen-stand.png', 20, 20);}function create() {   player = game.add.sprite(32, game.height - 150, 'df');   player.animations.add('stand'); // animate initial texture   // switch over to create the animation for the 2nd texture   // Animations seem to be associated with whatever texture is loaded at the time   player.loadTexture('df-run');   player.animations.add('run');   // switch back to standing to be the default at the start of the game   player.animations.play('stand', 20, true);    cursors.right.onDown.add(function () {      // switch the texture and animate it.       player.animations.play('run', 20, true);   });   cursors.left.onDown.add(function () {      // same      player.animations.play('run', 20, true);   });   cursors.right.onUp.add(function () {      player.animations.play('stand', 20, true); // same   });   cursors.left.onUp.add(function () {      player.animations.play('stand', 20, true); // change back to standing   });}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...