Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var game;
- var SCREEN_W = 500;
- var SCREEN_H = 340;
- var bootState = {
- preload: function() {
- game.load.image('progressBar', 'assets/progressBar.png');
- },
- create: function() {
- game.stage.backgroundColor = "#000000";
- game.physics.startSystem(Phaser.Physics.ARCADE);
- game.state.start('load');
- if(!game.device.desktop)
- {
- game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
- document.body.style.backgroundColor = "#000000";
- game.scale.minWidth = 250;
- game.scale.minHeight = 170;
- game.scale.maxWidth = 1000;
- game.maxHeight = 680;
- game.scale.pageAlignVertically = true;
- }
- }
- };
- var loadState = {
- preload: function() {
- var loadingText = game.add.text(game.world.centerX, 150, 'Loading...', { font: '30px Roboto', fill: '#FFFFFF' });
- loadingText.anchor.setTo(.5, .5);
- var progressBar = game.add.sprite(game.world.centerX, 200, 'progressBar');
- progressBar.anchor.setTo(.5, .5);
- game.load.setPreloadSprite(progressBar);
- //game.load.image('mario', 'assets/mario.png');
- game.load.image('wallV', 'assets/wallVertical.png');
- game.load.image('wallH', 'assets/wallHorizontal.png');
- game.load.image('coin', 'assets/uCoin.png');
- //game.load.image('enemy', 'assets/enemy.png');
- game.load.spritesheet('mario', 'assets/marioSS.png', 16, 16);
- game.load.spritesheet('enemy', 'assets/greenShell.png', 20, 28);
- game.load.image('background', 'assets/background.png');
- game.load.audio('jump', ['assets/jump.ogg', 'assets/jump.mp3']);
- game.load.audio('coin', ['assets/coin.ogg', 'assets/coin.mp3']);
- game.load.audio('dead', ['assets/dead.ogg', 'assets/dead.mp3']);
- game.load.image('jumpButton', 'assets/jumpButton.png');
- game.load.image('rightButton','asset/rightButton.png');
- game.load.image('leftButton', 'assets/leftButton.png');
- },
- create: function() {
- game.state.start('menu');
- }
- };
- var menuState = {
- create: function() {
- var text;
- if(game.device.desktop)
- text = 'Press the Up Arrow to Start!';
- else
- text = 'Touch the Screen to Start!';
- var startLabel = game.add.text(game.world.centerX, game.world.height - 80, text, {font: '25px Roboto', fill: '#FFFFFF'});
- startLabel.anchor.setTo(.5, .5);
- game.add.image(0, 0, 'background');
- if (!localStorage.getItem('bestScore'))
- {
- localStorage.setItem('bestScore', 0);
- }
- if (game.global.score > localStorage.getItem('bestScore'))
- {
- localStorage.setItem('bestScore', game.global.score);
- }
- var nameLabel = game.add.text(game.world.centerX, 80, 'Super Ucode Game!', { font: '50px Arial', fill: '#FFFFFF' });
- nameLabel.anchor.setTo(.5, .5);
- game.add.tween(nameLabel).to({ y: 80 }, 1000).easing(Phaser.Easing.Bounce.Out).start();
- var scoreText = 'Score: ' + game.global.score + '\nBest Score: ' + localStorage.getItem('bestScore')
- var scoreLabel = game.add.text(game.world.centerX, game.world.centerY, scoreText , {font: '25px Roboto', fill: '#FFFFFF'});
- scoreLabel.anchor.setTo(.5, .5);
- scoreLabel.anchor.setTo(.5, .5);
- var otherLabel = game.add.text(36, 250, text, { font: '30px Arial', fill: '#FFFFFF' });
- otherLabel.anchor.setTo(.01, .5);
- var upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
- upKey.onDown.addOnce(this.startGame, this);
- if(game.device.desktop)
- {
- var upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
- upKey.onDown.addOnce(this.startGame, this);
- }
- else
- {
- game.input.onTap.addOnce(this.startGame, this);
- }
- },
- startGame: function() {
- game.state.start('main');
- }
- };
- var mainState = {
- create: function() {
- //this.helloText = game.add.text(250, 170, 'Hello World!', {font: '20px Roboto', fill: '#FF0000' } );
- this.scoreText = game.add.text(30, 30, 'Score: 0', { font: '18px Roboto', fill: '#FFFFFF' });
- //this.helloText.anchor.setTo(0.5,0.5)
- this.sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'mario');
- this.sprite.anchor.setTo(.5, 0.5);
- //game.physics.startSystem(Phaser.Physics.ARCADE);
- game.physics.arcade.enable(this.sprite);
- this.sprite.body.gravity.y = 500;
- this.cursor = game.input.keyboard.createCursorKeys();
- this.createWorld();
- this.coin = game.add.sprite(60, 140, 'coin');
- this.coin.anchor.setTo(.5, .5);
- game.physics.arcade.enable(this.coin);
- //this.score = 0;
- game.global.score = 0;
- this.enemies = game.add.group();
- this.enemies.enableBody = true;
- this.enemies.createMultiple(10, 'enemy');
- game.time.events.loop(2000, this.spawnEnemy, this);
- this.coinSound = game.add.audio('coin');
- this.deadSound = game.add.audio('dead');
- this.jumpSound = game.add.audio('jump');
- // this.sprite = game.add.sprite(250, 170, 'mario');
- this.sprite.anchor.setTo(.5, .5);
- this.sprite.animations.add('right', [4, 5, 6, 7], 8, true);
- this.sprite.animations.add('left', [3, 2, 1, 0], 8, true);
- if(!game.device.desktop)
- this.addMobileInputs();
- },
- update: function() {
- game.physics.arcade.collide(this.sprite, this.walls);
- //this.sprite.angle++;
- this.movePlayer();
- if (!this.sprite.inWorld) {
- this.restart();
- }
- game.physics.arcade.collide(this.enemies, this.walls);
- game.physics.arcade.overlap(this.sprite, this.coin, this.takeCoin, null, this);
- game.physics.arcade.overlap(this.sprite, this.enemies, this.restart, null, this);
- },
- movePlayer: function() {
- if (this.cursor.left.isDown || this.moveLeft) {
- this.sprite.body.velocity.x = -200;
- this.sprite.animations.play('left');
- }
- else if (this.cursor.right.isDown || this.moveRight) {
- this.sprite.body.velocity.x = 200;
- this.sprite.animations.play('right');
- }
- else {
- this.sprite.body.velocity.x = 0;
- this.sprite.animations.stop();
- this.sprite.frame = 4;
- }
- if (this.cursor.up.isDown && this.sprite.body.touching.down) {
- this.sprite.body.velocity.y = -320;
- this.jumpSound.play();
- this.jumpPlayer()
- }
- },
- createWorld: function() {
- this.walls = game.add.group();
- this.walls.enableBody = true;
- game.add.sprite(0, 0, 'wallV', 0, this.walls);
- game.add.sprite(480, 0, 'wallV', 0, this.walls);
- game.add.sprite(0, 0, 'wallH', 0, this.walls);
- game.add.sprite(300, 0, 'wallH', 0, this.walls);
- game.add.sprite(0, 320, 'wallH', 0, this.walls);
- game.add.sprite(300, 320, 'wallH', 0, this.walls);
- game.add.sprite(-100, 160, 'wallH', 0, this.walls);
- game.add.sprite(400, 160, 'wallH', 0, this.walls);
- //Please check to see if the following set of code belongs in the creatWorld object.
- var middleTop = game.add.sprite(100, 80, 'wallH', 0, this.walls);
- middleTop.scale.setTo(1.5, 1);
- var middleBottom = game.add.sprite(100, 240, 'wallH', 0, this.walls);
- middleBottom.scale.setTo(1.5, 1);
- this.walls.setAll('body.immovable', true);
- },
- restart: function() {
- //game.state.start('main');
- if (!this.sprite.alive)
- return;
- this.sprite.kill();
- this.deadSound.play();
- game.state.start('menu');
- game.time.events.add(1000, game.state.start('menu'), this);
- },
- takeCoin: function() {
- //this.coin.kill();
- this.moveCoin();
- //this.score += 5;
- this.scoreText.text = 'Score: ' + this.score;
- this.coinSound.play();
- game.add.tween(this.sprite.scale).to({ x: 1.5, y: 1.5 }, 50).to({ x: 1, y: 1 }, 150).start();
- game.global.score += 10;
- this.scoreText.text = 'Score: ' + game.global.score;
- },
- moveCoin: function() {
- var coinPosition = [{ x: 140, y: 60 }, { x: 360, y: 60 },
- { x: 60, y: 140 }, { x: 440, y: 140 },
- { x: 130, y: 300 }, { x: 370, y: 300 }
- ];
- for (var i = 0; i < coinPosition.length; i++) {
- if (coinPosition[i].x == this.coin.x) {
- coinPosition.splice(i, 1);
- }
- }
- var newPosition = coinPosition[game.rnd.integerInRange(0, coinPosition.length - 1)];
- this.coin.reset(newPosition.x, newPosition.y);
- },
- spawnEnemy: function() {
- var enemy = this.enemies.getFirstDead();
- if (!enemy)
- return;
- enemy.animations.add('spin', [0, 1, 2], 8, true);
- enemy.anchor.setTo(.5, 1);
- enemy.reset(game.world.centerX, 0);
- enemy.body.gravity.y = 500;
- enemy.body.velocity.x = 100 * Phaser.Math.randomSign();
- enemy.body.bounce.x = 1;
- enemy.checkWorldBounds = true;
- enemy.outOfBoundsKill = true;
- enemy.animations.play('spin');
- },
- addMobileInputs: function()
- {
- this.moveLeft = false;
- this.moveRight = false;
- this.jumpButton = game.add.sprite(350, 247, 'jumpButton');
- this.jumpButton.inputEnabled = true;
- this.jumpButton.alpha = .5;
- this.jumpButton.events.onInputDown.add(this.jumpPlayer, this);
- this.leftButton = game.add.sprite(50, 247, 'leftButton');
- this.leftButton.inputEnabled = true;
- this.leftButton.alpha = .5;
- this.leftButton.events.onInputUp.add(function(){this.moveLeft = false; this.leftButton.alpha = .5;}, this);
- this.leftButton.events.onInputDown.add(function(){this.moveLeft = true; this.leftButton.alpha = 1;}, this);
- this.leftButton.events.onInputOut.add(function(){this.moveLeft = false; this.leftButton.alpha = .5;}, this);
- this.leftButton.events.onInputOver.add(function(){this.moveLeft = true; this.leftButton.alpha = 1;}, this);
- this.rightButton = game.add.sprite(130, 247, 'rightButton');
- this.rightButton.inputEnabled = true;
- this.rightButton.alpha = .5;
- this.rightButton.events.onInputOver.add(function(){this.moveRight = true; this.rightButton.alpha = 1}, this);
- this.rightButton.events.onInputOut.add(function(){this.moveRight = false; this.rightButton.alpha = .5;}, this);
- this.rightButton.events.onInputDown.add(function(){this.moveRight = true; this.rightButton.alpha = 1;}, this);
- this.rightButton.events.onInputUp.add(function(){this.moveRight = false; this.rightButton.alpha = .5;}, this);
- },
- jumpPlayer: function()
- {
- if(this.sprite.body.touching.down)
- {
- this.sprite.body.velocity.y = -320;
- this.jumpSound.play();
- }
- }
- };
- game = new Phaser.Game(SCREEN_W, SCREEN_H, Phaser.AUTO, 'game');
- game.state.add('boot', bootState);
- game.state.add('load', loadState);
- game.state.add('menu', menuState);
- game.state.add('main', mainState);
- game.global = {
- score: 0
- };
- game.state.start('boot');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement