Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var game = new Phaser.Game(1280, 720, Phaser.AUTO, null, {preload: preload, create: create, update: update});
- var player;
- var input;
- var spacebar;
- var D;
- var A;
- var S;
- var W;
- var walls;
- var level = [
- "xxxxx",
- "x x",
- "x x",
- "xxxxx"
- ];
- function preload(){
- setGameProperties();
- preloadImageAssets();
- }
- function create(){
- playerInit();
- levelInit();
- }
- function update(){
- getCollisions();
- getPlayerInput();
- }
- function setGameProperties(){
- game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
- game.scale.pageAlignHorizontally = true;
- game.scale.pageAlignVertically = true;
- game.stage.backgroundColor = "#08ff00";
- game.physics.startSystem(Phaser.Physics.ARCADE);
- game.world.enableBody = true;
- input = game.input.keyboard.createCursorKeys();
- spaceBar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- D = game.input.keyboard.addKey(Phaser.Keyboard.D);
- A = game.input.keyboard.addKey(Phaser.Keyboard.A);
- S = game.input.keyboard.addKey(Phaser.Keyboard.S);
- W = game.input.keyboard.addKey(Phaser.Keyboard.W);
- }
- function preloadImageAssets(){
- game.load.spritesheet("player", "assets/player.png", 64, 64);
- game.load.image("wall", "assets/wall.png");
- }
- function playerInit(){
- player = game.add.sprite(0,100,"player");
- player.body.collideWorldBounds = true;
- }
- function levelInit(){
- walls = game.add.group();
- for(var i = level.length-1; i >= 0; i--)
- {
- for(var j = 0; j < level[i].length; j++){
- if (level[i][j] == "x"){
- var wall = game.add.sprite(25*j, 295-(25*(level.length-1-i)), "wall");
- wall.body.immovable = true;
- walls.add(wall);
- }
- }
- }
- }
- function getCollisions(){
- game.physics.arcade.collide(player, walls);
- }
- function getPlayerInput(){
- player.animations.add("walk_Down", [0,1,2,3],5,true);
- player.animations.add("walk_Left", [4,5],5,true);
- player.animations.add("walk_Up", [6,7,8,9],5,true);
- player.animations.add("walk_Right", [10,11],5,true);
- if(A.isDown){
- player.body.velocity.x = -200;
- player.animations.play("walk_Left");
- }
- else if(D.isDown){
- player.body.velocity.x = 200;
- player.animations.play("walk_Right");
- }
- else{
- player.body.velocity.x = 0;
- }
- /*-- Y AXIS --*/
- if(S.isDown){
- player.body.velocity.y = 175;
- player.animations.play("walk_Down");
- }
- else if(W.isDown){
- player.body.velocity.y = -175;
- player.animations.play("walk_Up");
- }
- else{
- player.body.velocity.y = 0;
- }
- if(player.body.velocity.x == 0 && player.body.velocity.y == 0){
- player.animations.play("walk_Down");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement