Advertisement
JXSeaton

sprite sheets in phaser

Dec 3rd, 2020
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <script src="//cdn.jsdelivr.net/npm/phaser@3.24.1/dist/phaser.js"></script>
  4.     </head>
  5.     <body>
  6.         <script>
  7.             var config = {
  8.                 type: Phaser.AUTO,
  9.                 backgroundColor: 0xCCFFFF ,
  10.                 width: 600,
  11.                 height: 600,
  12.                 physics: {
  13.                     default: 'arcade'
  14.                 },
  15.                 scene: {
  16.                     preload: preload,
  17.                     create: create,
  18.                     update: update
  19.                 }
  20.             };
  21.  
  22.             var gamePiece;
  23.             var keyInputs;
  24.             var game = new Phaser.Game(config);
  25.            
  26.             var upKey;
  27.      
  28.             function preload(){
  29.                 this.load.spritesheet('gamePiece', 'img/spriteSheet.png', {
  30.                     frameWidth: 60,
  31.                     frameHeight: 60
  32.                 });
  33.             }
  34.  
  35.             function create(){
  36.                 this.anims.create({
  37.                     key: "spin",
  38.                     frameRate: 4,
  39.                     frames: this.anims.generateFrameNumbers("gamePiece", {start: 0, end:11}),
  40.                     repeat: -1
  41.                 });
  42.                 this.anims.create({
  43.                     key: "up",
  44.                     frameRate: 2,
  45.                     frames: this.anims.generateFrameNumbers("gamePiece", {start: 0, end:2}),
  46.                     repeat: -1
  47.                 });
  48.                 this.anims.create({
  49.                     key: "right",
  50.                     frameRate: 2,
  51.                     frames: this.anims.generateFrameNumbers("gamePiece", {start: 3, end:5}),
  52.                     repeat: -1
  53.                 });
  54.                 this.anims.create({
  55.                     key: "down",
  56.                     frameRate: 2,
  57.                     frames: this.anims.generateFrameNumbers("gamePiece", {start: 6, end:8}),
  58.                     repeat: -1
  59.                 });
  60.                 this.anims.create({
  61.                     key: "left",
  62.                     frameRate: 2,
  63.                     frames: this.anims.generateFrameNumbers("gamePiece", {start: 9, end:11}),
  64.                     repeat: -1
  65.                 });
  66.                
  67.                 gamePiece = this.add.sprite(270, 450, 'gamePiece');
  68.                 gamePiece.anims.play("spin");
  69.                
  70.                 keyInputs = this.input.keyboard.createCursorKeys();
  71.                
  72.                 leftKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
  73.                 rightKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
  74.                 upKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);
  75.                 downKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);
  76.             }
  77.    
  78.             function update(){
  79.                 if(keyInputs.left.isDown){
  80.                     gamePiece.x = gamePiece.x - 2;
  81.                 }
  82.                 if(keyInputs.right.isDown){
  83.                     gamePiece.x = gamePiece.x + 2;
  84.                 }
  85.                 if(keyInputs.up.isDown){
  86.                     gamePiece.y = gamePiece.y - 2;
  87.                    
  88.                 }
  89.                 if(keyInputs.down.isDown){
  90.                     gamePiece.y = gamePiece.y + 2;
  91.                 }
  92.                
  93.                 if(Phaser.Input.Keyboard.JustDown(leftKey)){
  94.                     gamePiece.anims.play("left");
  95.                 }
  96.                 if(Phaser.Input.Keyboard.JustDown(rightKey)){
  97.                     gamePiece.anims.play("right");
  98.                 }
  99.                 if(Phaser.Input.Keyboard.JustDown(upKey)){
  100.                     gamePiece.anims.play("up");
  101.                 }
  102.                 if(Phaser.Input.Keyboard.JustDown(downKey)){
  103.                     gamePiece.anims.play("down");
  104.                 }
  105.                
  106.                 if(Phaser.Input.Keyboard.JustUp(leftKey)){
  107.                     gamePiece.anims.play("spin");
  108.                 }
  109.                 if(Phaser.Input.Keyboard.JustUp(rightKey)){
  110.                     gamePiece.anims.play("spin");
  111.                 }
  112.                 if(Phaser.Input.Keyboard.JustUp(upKey)){
  113.                     gamePiece.anims.play("spin");
  114.                 }
  115.                 if(Phaser.Input.Keyboard.JustUp(downKey)){
  116.                     gamePiece.anims.play("spin");
  117.                 }
  118.             }
  119.    
  120.    
  121.         </script>
  122.     </body>
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement