Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. var game = new Phaser.Game(1280, 720, Phaser.AUTO, null, {preload: preload, create: create, update: update});
  2. var player;
  3. var input;
  4. var spacebar;
  5. var D;
  6. var A;
  7. var S;
  8. var W;
  9. var walls;
  10. var level = [
  11. "xxxxx",
  12. "x x",
  13. "x x",
  14. "xxxxx"
  15. ];
  16.  
  17. function preload(){
  18. setGameProperties();
  19. preloadImageAssets();
  20. }
  21.  
  22. function create(){
  23. playerInit();
  24. levelInit();
  25. }
  26.  
  27. function update(){
  28. getCollisions();
  29. getPlayerInput();
  30. }
  31.  
  32. function setGameProperties(){
  33. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  34. game.scale.pageAlignHorizontally = true;
  35. game.scale.pageAlignVertically = true;
  36.  
  37. game.stage.backgroundColor = "#08ff00";
  38.  
  39. game.physics.startSystem(Phaser.Physics.ARCADE);
  40. game.world.enableBody = true;
  41.  
  42. input = game.input.keyboard.createCursorKeys();
  43.  
  44. spaceBar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  45. D = game.input.keyboard.addKey(Phaser.Keyboard.D);
  46. A = game.input.keyboard.addKey(Phaser.Keyboard.A);
  47. S = game.input.keyboard.addKey(Phaser.Keyboard.S);
  48. W = game.input.keyboard.addKey(Phaser.Keyboard.W);
  49. }
  50.  
  51. function preloadImageAssets(){
  52. game.load.spritesheet("player", "assets/player.png", 64, 64);
  53. game.load.image("wall", "assets/wall.png");
  54. }
  55.  
  56. function playerInit(){
  57. player = game.add.sprite(0,100,"player");
  58. player.body.collideWorldBounds = true;
  59. }
  60.  
  61. function levelInit(){
  62. walls = game.add.group();
  63. for(var i = level.length-1; i >= 0; i--)
  64. {
  65. for(var j = 0; j < level[i].length; j++){
  66. if (level[i][j] == "x"){
  67. var wall = game.add.sprite(25*j, 295-(25*(level.length-1-i)), "wall");
  68. wall.body.immovable = true;
  69. walls.add(wall);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. function getCollisions(){
  76. game.physics.arcade.collide(player, walls);
  77. }
  78.  
  79. function getPlayerInput(){
  80.  
  81. player.animations.add("walk_Down", [0,1,2,3],5,true);
  82. player.animations.add("walk_Left", [4,5],5,true);
  83. player.animations.add("walk_Up", [6,7,8,9],5,true);
  84. player.animations.add("walk_Right", [10,11],5,true);
  85.  
  86.  
  87. if(A.isDown){
  88. player.body.velocity.x = -200;
  89. player.animations.play("walk_Left");
  90. }
  91. else if(D.isDown){
  92. player.body.velocity.x = 200;
  93. player.animations.play("walk_Right");
  94. }
  95. else{
  96. player.body.velocity.x = 0;
  97. }
  98.  
  99. /*-- Y AXIS --*/
  100.  
  101. if(S.isDown){
  102. player.body.velocity.y = 175;
  103. player.animations.play("walk_Down");
  104. }
  105. else if(W.isDown){
  106. player.body.velocity.y = -175;
  107. player.animations.play("walk_Up");
  108. }
  109. else{
  110. player.body.velocity.y = 0;
  111. }
  112.  
  113. if(player.body.velocity.x == 0 && player.body.velocity.y == 0){
  114. player.animations.play("walk_Down");
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement