Advertisement
vencinachev

Player-Run

Dec 16th, 2021
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2. const Game = new Phaser.Game(1000, 800, Phaser.AUTO, 'game-canvas', { preload, create, update })
  3.  
  4.  
  5. let player;
  6. let speed = 5;
  7. let state = 'right';
  8.  
  9. function preload() {
  10.     Game.load.image('bg', 'images/bg.jpg');
  11.     Game.load.spritesheet('playerSH', 'images/playerss.png', 576 / 9, 256/ 4);
  12. }
  13.  
  14. function create() {
  15.     let background = Game.add.sprite(0, 0, 'bg');
  16.     background.width = Game.width;
  17.     background.height = Game.height;
  18.  
  19.     player = Game.add.sprite(0, 50, 'playerSH')
  20.     player.anchor.setTo(0.5);
  21.     player.width = 80;
  22.     player.height = 100;
  23.     player.animations.add('walk-up', [0, 1, 2, 3, 4, 5, 6, 7, 8], 10, true)
  24.     player.animations.add('walk-down', [18, 19, 20, 21, 22, 23, 24, 25, 26], 10, true)
  25.     player.animations.add('walk-left', [9, 10, 11, 12, 13, 14, 15, 16, 17], 20, true)
  26.     player.animations.add('walk-right', [27, 28, 29, 30, 31, 32, 33, 34, 35], 10, true)
  27. }
  28.  
  29. function update() {
  30.     if (state == 'right'){
  31.         player.angle = 180;
  32.         player.x += speed;
  33.         player.animations.play('walk-left');
  34.         if (player.x >= Game.width - player.width / 2){
  35.             state = 'down';
  36.         }
  37.     } else if (state == 'down'){
  38.         player.angle = 270;
  39.         player.y += speed;
  40.         if (player.y >= Game.height - player.width / 2){
  41.             state = 'left';
  42.         }
  43.     } else if (state == 'left'){
  44.         player.angle = 0;
  45.         player.x -= speed;
  46.         if (player.x <= player.width / 2){
  47.             state = 'up';
  48.         }
  49.     } else if (state == 'up') {
  50.         player.angle = 90;
  51.         player.y -= speed;
  52.         if (player.y <= player.width / 2){
  53.             state = 'right';
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement