Speakeasys Posted May 13, 2015 Share Posted May 13, 2015 So I am in the process of turning my single javascript file game into a state based game. I'm having trouble using global variables that I create in my create() function. I want to use them in my update() function. I've tried several methods but they don't seem to work. I only get undefined when I console.log their value in update(). Any information is great appreciated.BasicGame.Game = function (game) { // When a State is added to Phaser it automatically has the // following properties set on it, even if they already exist: this.game; // a reference to the currently running game this.add; // used to add sprites, text, groups, etc this.camera; // a reference to the game camera this.cache; // the game cache this.input; // the global input manager (you can access // this.input.keyboard, this.input.mouse, as well // from it) this.load; // for preloading assets this.math; // lots of useful common math operations this.sound; // the sound manager - add a sound, play one, // set-up markers, etc this.stage; // the game stage this.time; // the clock this.tweens; // the tween manager this.world; // the game world this.particles; // the particle manager this.physics; // the physics manager this.rnd; // the repeatable random number generator // You can use any of these from any function within this State. // But do consider them as being 'reserved words', i.e. don't create // a property for your own game called "world" or you'll over-write // the world reference. this.player; this.aliens; this.bullets; bulletTime: 0; this.cursors; this.fireButton; this.explosions; this.starfield; this.score; scoreString: ''; this.scoreText; this.lives; this.enemyBullet; firingTimer: 0; this.stateText; livingEnemies: []; thingy: "hello";};BasicGame.Game.prototype = { create: function () { this.physics.startSystem(Phaser.Physics.ARCADE); this.starfield = this.add.tileSprite(0, 0, 800, 600, 'starfield'); var bullets = this.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(30, 'bullet'); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 1); bullets.setAll('outOfBoundsKill', true); bullets.setAll('checkWorldBounds', true); var enemyBullet = this.add.group(); enemyBullet.createMultiple(30, 'enemyBullet'); enemyBullet.enableBody = true; enemyBullet.physicsBodyType = Phaser.Physics.ARCADE; enemyBullet.setAll('anchor.x', 0.5); enemyBullet.setAll('anchor.y', 1); enemyBullet.setAll('outOfBoundsKill', true); enemyBullet.setAll('checkWorldBounds', true); var player = this.add.sprite(400, 500, 'ship'); player.anchor.setTo(0.5, 0.5); this.physics.enable(player, Phaser.Physics.ARCADE); var aliens = this.add.group(); aliens.enableBody = true; aliens.physicsBodyType = Phaser.Physics.ARCADE; var tween = this.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); createAliens(); tween.onLoop.add(descend, this); var scoreString = ''; var firingTimer = 0; var bulletTime = 0; var livingEnemies = []; var score = 0; scoreString = 'Score : '; var scoreText = this.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' }); var lives = this.add.group(); this.add.text(this.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' }); stateText = this.add.text(this.world.centerX,this.world.centerY,' ', { font: '84px Arial', fill: '#fff' }); stateText.anchor.setTo(0.5, 0.5); stateText.visible = false; for (var i = 0; i < 3; i++) { var ship = lives.create(this.world.width - 100 + (30 * i), 60, 'ship'); ship.anchor.setTo(0.5, 0.5); ship.angle = 90; ship.alpha = 0.4; } explosions = this.add.group(); explosions.createMultiple(30, 'kaboom'); explosions.forEach(setupInvader, this); var cursors = this.input.keyboard.createCursorKeys(); var fireButton = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; function createAliens () { for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { var alien = aliens.create(x * 48, y * 50, 'invader'); alien.anchor.setTo(0.5, 0.5); alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true); alien.play('fly'); alien.body.moves = false; } } aliens.x = 100; aliens.y = 50; } function descend() { aliens.y += 10; } function setupInvader (invader) { invader.anchor.x = 0.5; invader.anchor.y = 0.5; invader.animations.add('kaboom'); } var fullscreen = this.add.button(this.game.width-8, this.game.height-8,'fullscreen',BasicGame.toggleFullscreen,this,'over', 'up', 'down'); fullscreen.pivot.x = fullscreen.width; fullscreen.pivot.y = fullscreen.height; console.log(scoreString); }, update: function () { }, quitGame: function (pointer) { this.state.start('MainMenu'); }}; Link to comment Share on other sites More sharing options...
xaviserrag Posted May 14, 2015 Share Posted May 14, 2015 you only need to convert those properties from private to public on the state that youre working. A way of doing this is making the variable a property of the object, setting the variable on the this like in the example. Then on the update you use the variable as this.someVariableSo the example would be: // Instead of var bulletsthis.bullets = this.add.group(); Link to comment Share on other sites More sharing options...
Speakeasys Posted May 14, 2015 Author Share Posted May 14, 2015 So when I declare a global variable do I put it inside of here? Because when I do my game no longer loads. I get an error about group not found or if I console.log the value is undefined. How do I access them inside of the update function? "BasicGame.bullets"? "this.bullets"? "BasicGame.Game.bullets"?BasicGame.Game = function (game) { this.bullets = this.add.group();};"Uncaught TypeError: Cannot read property 'group' of undefined" Link to comment Share on other sites More sharing options...
drhayes Posted May 14, 2015 Share Posted May 14, 2015 You should read up on JavaScript programming. Probably this one on constructor functions and this one on the keyword "this". Essentially, when you do this:function Catpants(arg1, arg2, arg3, arg4) { this.one = arg1; this.two = arg2; this.three = arg3; var four = arg4;}var cat = new Catpants('a', 'b', 'c', 'd');There are a bunch of ways to access those properties. Now that you've invoked the constructor function "Catpants" you have an object named "cat" that has three properties on it: "cat.one", "cat.two", "cat.three". There's no property named "four"; it's only a variable within the constructor function. If you were to define a method on the Catpants class like this:Catpants.prototype.doStuff = function() { console.log(this.one); console.log(this.two); console.log(this.three); console.log(this.four); // <~~~ this will log undefined}Then when you call "cat.doStuff();" it'll log "a" then "b" then "c" then "undefined". There's no property named "four"; it was only a variable inside your constructor function. Pay *really* close attention to those names. Catpants is a constructor function, cat is the instance, doStuff is a method, and one, two, and three are properties on the cat instance while four is only a variable inside the constructor function. Here's what's going on with your code: you've declared variables inside your constructor function, so the names disappear when the function ends. You *actually* want them to be properties so you can access them in other methods. That's a REALLY dense packed sentence but is central to programming the way you want to, here. Your BasicGame.Game function is kind of a mess. All the stuff starting from "// When a state" until "this.rnd" should be deleted. That'll get stuck onto your object when you tell Phaser it's a state. Hiding in that long list of properties are a couple of syntax errors, things like: "bulletTime: 0;". Then, like xaviserrag said, you should use "this.bullets" instead of "bullets" in your create function. Why? Because "this.bullets" is the property while "bullets" is just the variable. Link to comment Share on other sites More sharing options...
Speakeasys Posted May 16, 2015 Author Share Posted May 16, 2015 Ok. I started to read those two links you sent me as well as reading a little more about javascript prototyping. Thank you! It's starting to make more sense. I did what you said and removed the excess from the template I started to use for state-based and fullscreen. I then converted to using the this pointer to change the scope for global instead of only in the create() function. I got pretty far in terms of getting my image background, the aliens moving. But then when I got to the bullets of the player they come out in these un-timed spraying instead of a consistent spraying. And that's only if I have the fireButton.isDown uncommented out in my update function. I then nest functions inside the update() function. Is that the proper method? I couldn't figure out a way to place functions outside of update() and create(), but would still be within the prototype. I'm getting close to getting this running! Thank you so much for your assistance! Uncaught TypeError: Cannot read property 'getFirstExists' of undefinedenemy Fires @ Game.js:209 Line 209: this.enemyBullet = this.enemyBullets.getFirstExists(false); BasicGame.Game = function (game) { this.enemyBullet this.player; this.aliens; this.bullets; this.enemyBullet; this.starfield; this.firingTimer; this.enemyBullets; this.fireButton; this.bulletTime; this.livingEnemies;};BasicGame.Game.prototype = { create: function () { this.bulletTime = 0; this.physics.startSystem(Phaser.Physics.ARCADE); this.starfield = this.add.tileSprite(0, 0, 800, 600, 'starfield'); this.bullets = this.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.ARCADE; this.bullets.createMultiple(30, 'bullet'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 1); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); this.enemyBullet = this.add.group(); this.enemyBullet.createMultiple(30, 'enemyBullet'); this.enemyBullet.enableBody = true; this.enemyBullet.physicsBodyType = Phaser.Physics.ARCADE; this.enemyBullet.setAll('anchor.x', 0.5); this.enemyBullet.setAll('anchor.y', 1); this.enemyBullet.setAll('outOfBoundsKill', true); this.enemyBullet.setAll('checkWorldBounds', true); this.player = this.add.sprite(400, 500, 'ship'); this.player.anchor.setTo(0.5, 0.5); this.physics.enable(this.player, Phaser.Physics.ARCADE); aliens = this.add.group(); aliens.enableBody = true; aliens.physicsBodyType = Phaser.Physics.ARCADE; var tween = this.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); createAliens(); tween.onLoop.add(descend, this); this.scoreString = ''; this.firingTimer = 0; this.livingEnemies = []; var score = 0; scoreString = 'Score : '; this.scoreText = this.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' }); var lives = this.add.group(); this.add.text(this.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' }); stateText = this.add.text(this.world.centerX,this.world.centerY,' ', { font: '84px Arial', fill: '#fff' }); stateText.anchor.setTo(0.5, 0.5); stateText.visible = false; for (var i = 0; i < 3; i++) { var ship = lives.create(this.world.width - 100 + (30 * i), 60, 'ship'); ship.anchor.setTo(0.5, 0.5); ship.angle = 90; ship.alpha = 0.4; } explosions = this.add.group(); explosions.createMultiple(30, 'kaboom'); explosions.forEach(setupInvader, this); this.cursors = this.input.keyboard.createCursorKeys(); this.fireButton = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; function createAliens () { for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { alien = aliens.create(x * 48, y * 50, 'invader'); alien.anchor.setTo(0.5, 0.5); alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true); alien.play('fly'); alien.body.moves = false; } } aliens.x = 100; aliens.y = 50; } function descend() { aliens.y += 10; } function setupInvader (invader) { invader.anchor.x = 0.5; invader.anchor.y = 0.5; invader.animations.add('kaboom'); } function resetBullet (bullet) { this.bullet.kill(); } function restart () { this.lives.callAll('revive'); this.aliens.removeAll(); createAliens(); this.player.revive(); this.stateText.visible = false; } function gofull() { if (this.scale.isFullScreen) { this.scale.stopFullScreen(); } else { this.scale.startFullScreen(false); } } var fullscreen = this.add.button(this.game.width-8, this.game.height-8,'fullscreen',BasicGame.toggleFullscreen,this,'over', 'up', 'down'); fullscreen.pivot.x = fullscreen.width; fullscreen.pivot.y = fullscreen.height; }, update: function () { time = this.time; player = this.player; cursors = this.cursors; bulletTime = this.bulletTime; fireButton = this.fireButton; bullet = this.bullet; bullets = this.bullets; aliens = this.aliens; firingTimer = this.firingTimer; enemyBullets = this.enemyBullets; enemyBullet = this.enemyBullet; livingEnemies = this.livingEnemies; this.starfield.tilePosition.y += 2; if(player.alive) { player.body.velocity.setTo(0, 0); if (cursors.left.isDown) { player.body.velocity.x = -200; } else if (cursors.right.isDown) { player.body.velocity.x = 200; } if (fireButton.isDown) { fireBullet(); } if (time.now > this.firingTimer) { enemyFires(); } this.physics.arcade.overlap(bullets, aliens, this.collisionHandler, null, this); //this.physics.arcade.overlap(this.enemyBullets, this.player, enemyHitsPlayer, null, this); //this.physics.arcade.overlap(this.player, this.aliens, playerCollision, null, this); } function fireBullet () { if (this.time.now > bulletTime) { bullet = bullets.getFirstExists(false); if (bullet) { console.log(time.now); bullet.reset(player.x, player.y + 8); bullet.body.velocity.y = -400; bulletTime = time.now + 200; } } } function enemyFires () { aliens = this.aliens; this.enemyBullet = this.enemyBullets.getFirstExists(false); this.livingEnemies.length = 0; aliens.forEachAlive(function(alien) { livingEnemies.push(alien); }); if (enemyBullet && livingEnemies.length > 0) { var random = this.rnd.integerInRange(0,livingEnemies.length-1); var shooter = livingEnemies[random]; enemyBullet.reset(shooter.body.x, shooter.body.y); this.physics.arcade.moveToObject(enemyBullet,player,120); firingTimer = game.time.now + 2000; } } function collisionHandler (bullet, alien) { bullet.kill(); alien.kill(); score += 20; scoreText.text = scoreString + score; var explosion = explosions.getFirstExists(false); explosion.reset(alien.body.x, alien.body.y); explosion.play('kaboom', 30, false, true); if (aliens.countLiving() == 0) { score += 1000; scoreText.text = scoreString + score; stateText.text = " You Won, \n Click to restart"; stateText.visible = true; enemyBullets.callAll('kill'); this.input.onTap.addOnce(restart,this); } } function enemyHitsPlayer (player,bullet) { this.bullet.kill(); live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } var explosion = explosions.getFirstExists(false); this.explosion.reset(player.body.x, player.body.y); this.explosion.play('kaboom', 30, false, true); if (this.lives.countLiving() < 1) { this.player.kill(); this.enemyBullets.callAll('kill'); this.stateText.text=" GAME OVER \n Click to restart"; this.stateText.visible = true; this.input.onTap.addOnce(restart,this); } } function playerCollision (player,aliens) { this.player.kill(); live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } aliens.reset(); createAliens(); var explosion = explosions.getFirstExists(false); this.explosion.reset(player.body.x, player.body.y); this.explosion.play('kaboom', 30, false, true); this.player.revive(); if (this.lives.countLiving() < 1) { this.player.kill(); this.enemyBullets.callAll('kill'); stateText.text=" GAME OVER \n Click to restart"; stateText.visible = true; this.input.onTap.addOnce(restart,this); } } }, quitGame: function (pointer) { this.state.start('MainMenu'); }}; Link to comment Share on other sites More sharing options...
Speakeasys Posted May 18, 2015 Author Share Posted May 18, 2015 My professor taught me how to break up my functions in prototype functions. I'm still working on it but I"ll post the result when I'm done debugging my code.BasicGame.Game = function (game) {}; BasicGame.Game.prototype = { create: function () { this.firingTimer = 0; this.bulletTime = 0; this.score = 0; this.physics.startSystem(Phaser.Physics.ARCADE); this.starfield = this.add.tileSprite(0, 0, 800, 600, 'starfield'); this.bullets = this.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.ARCADE; this.bullets.createMultiple(30, 'bullet'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 1); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); this.enemyBullet = this.add.group(); this.enemyBullet.createMultiple(30, 'enemyBullet'); this.enemyBullet.enableBody = true; this.enemyBullet.physicsBodyType = Phaser.Physics.ARCADE; this.enemyBullet.setAll('anchor.x', 0.5); this.enemyBullet.setAll('anchor.y', 1); this.enemyBullet.setAll('outOfBoundsKill', true); this.enemyBullet.setAll('checkWorldBounds', true); this.enemyBullet = this.enemyBullet.getFirstExists(false); this.player = this.add.sprite(400, 500, 'ship'); this.player.anchor.setTo(0.5, 0.5); this.physics.enable(this.player, Phaser.Physics.ARCADE); this.aliens = this.add.group(); this.aliens.enableBody = true; this.aliens.physicsBodyType = Phaser.Physics.ARCADE; this.createAliens(); this.scoreString = 'Score : '; this.livingEnemies = []; this.scoreText = this.add.text(10, 10, this.scoreString + this.score, { font: '34px Arial', fill: '#fff' }); this.lives = this.add.group(); this.add.text(this.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' }); this.stateText = this.add.text(this.world.centerX,this.world.centerY,' ', { font: '84px Arial', fill: '#fff' }); this.stateText.anchor.setTo(0.5, 0.5); this.stateText.visible = false; for (var i = 0; i < 3; i++) { var ship = this.lives.create(this.world.width - 100 + (30 * i), 60, 'ship'); ship.anchor.setTo(0.5, 0.5); ship.angle = 90; ship.alpha = 0.4; } this.explosions = this.add.group(); this.explosions.createMultiple(30, 'kaboom'); this.explosions.forEach(this.setupInvader, this); this.cursors = this.input.keyboard.createCursorKeys(); this.fireButton = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; var fullscreen = this.add.button(this.game.width-8, this.game.height-8,'fullscreen',BasicGame.toggleFullscreen,this,'over', 'up', 'down'); fullscreen.pivot.x = fullscreen.width; fullscreen.pivot.y = fullscreen.height; }, update: function () { this.starfield.tilePosition.y += 2; if(this.player.alive) { this.player.body.velocity.setTo(0, 0); if (this.cursors.left.isDown) { this.player.body.velocity.x = -200; } else if (this.cursors.right.isDown) { this.player.body.velocity.x = 200; } if (this.fireButton.isDown) { this.fireBullet(); } if (this.time.now > this.firingTimer) { this.enemyFires(); } this.physics.arcade.overlap(this.bullets, this.aliens, this.collisionHandler, null, this); this.physics.arcade.overlap(this.enemyBullet, this.player, this.enemyHitsPlayer, null, this); this.physics.arcade.overlap(this.player, this.aliens, this.playerCollision, null, this); } }, restart: function () { this.lives.callAll('revive'); this.aliens.removeAll(); this.createAliens(); this.player.revive(); this.stateText.visible = false; }, playerCollision: function(player,aliens) { this.player.kill(); this.live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } this.aliens.reset(); this.createAliens(); var explosion = this.explosions.getFirstExists(false); explosion.reset(this.player.body.x, this.player.body.y); explosion.play('kaboom', 30, false, true); this.player.revive(); if (this.lives.countLiving() < 1) { this.player.kill(); this.enemyBullet.callAll('kill'); // this.stateText.text=" GAME OVER \n Click to restart"; this.stateText.visible = true; this.input.onTap.addOnce(this.restart,this); } }, enemyFires: function() { this.livingEnemies.length = 0; this.aliens.forEachAlive(function(alien){ this.livingEnemies.push(alien); },this); if (this.enemyBullet && this.livingEnemies.length > 0) { var random = this.rnd.integerInRange(0,this.livingEnemies.length-1); var shooter = this.livingEnemies[random]; this.enemyBullet.reset(shooter.body.x, shooter.body.y); this.physics.arcade.moveToObject(this.enemyBullet,this.player,120); this.firingTimer = this.time.now + 2000; } }, collisionHandler: function(bullet, alien) { this.bullet.kill(); this.alien.kill(); this.score += 20; this.scoreText.text = this.scoreString + this.score; this.explosion = this.explosions.getFirstExists(false); this.explosion.reset(this.alien.body.x, this.alien.body.y); this.explosion.play('kaboom', 30, false, true); if (this.aliens.countLiving() == 0) { this.score += 1000; this.scoreText.text = this.scoreString + this.score; this.stateText.text = " You Won, \n Click to restart"; this.stateText.visible = true; this.enemyBullet.callAll('kill'); this.input.onTap.addOnce(this.restart,this); } }, fireBullet: function() { if (this.time.now > this.bulletTime) { this.bullet = this.bullets.getFirstExists(false); if (this.bullet) { this.bullet.reset(this.player.x, this.player.y + 8); this.bullet.body.velocity.y = -400; this.bulletTime = this.time.now + 200; } } }, restart: function() { this.lives.callAll('revive'); this.aliens.removeAll(); this.createAliens(); this.player.revive(); this.stateText.visible = false; }, gofull: function() { if (this.scale.isFullScreen) { this.scale.stopFullScreen(); } else { this.scale.startFullScreen(false); } }, resetBullet: function(bullet) { this.bullet.kill(); }, setupInvader: function (invader) { invader.anchor.x = 0.5; invader.anchor.y = 0.5; invader.animations.add('kaboom'); }, descend: function() { this.aliens.y += 10; }, createAliens: function () { for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { var alien = this.aliens.create(x * 48, y * 50, 'invader'); alien.anchor.setTo(0.5, 0.5); alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true); alien.play('fly'); alien.body.moves = false; } } this.aliens.x = 100; this.aliens.y = 50; this.tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); this.tween.onLoop.add(descend, this); }, enemyHitsPlayer: function(player,bullet) { this.bullet.kill(); this.live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } var explosion = this.explosions.getFirstExists(false); this.explosion.reset(this.player.body.x, this.player.body.y); this.explosion.play('kaboom', 30, false, true); if (this.lives.countLiving() < 1) { this.player.kill(); this.enemyBullet.callAll('kill'); this.stateText.text=" GAME OVER \n Click to restart"; this.stateText.visible = true; this.input.onTap.addOnce(this.restart,this); } }, quitGame: function (pointer) { this.state.start('MainMenu'); },}; Link to comment Share on other sites More sharing options...
Speakeasys Posted May 21, 2015 Author Share Posted May 21, 2015 Here is my final code. For anybody else looking for formatting within prototyping in scaling up their game from a single javascript file to multiple, state-based code. Thank you for your assistance all!BasicGame.Game = function (game) {};BasicGame.Game.prototype = { create: function () { this.firingTimer = 0; this.bulletTime = 0; this.score = 0; this.livingEnemies = []; this.physics.startSystem(Phaser.Physics.ARCADE); this.starfield = this.add.tileSprite(0, 0, 800, 600, 'starfield'); this.bullets = this.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.ARCADE; this.bullets.createMultiple(30, 'bullet'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 1); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); this.enemyBullets = this.add.group(); this.enemyBullets.createMultiple(30, 'enemyBullet'); this.enemyBullets.enableBody = true; this.enemyBullets.physicsBodyType = Phaser.Physics.ARCADE; this.enemyBullets.setAll('anchor.x', 0.5); this.enemyBullets.setAll('anchor.y', 1); this.enemyBullets.setAll('outOfBoundsKill', true); this.enemyBullets.setAll('checkWorldBounds', true); this.player = this.add.sprite(400, 500, 'ship'); this.player.anchor.setTo(0.5, 0.5); this.physics.enable(this.player, Phaser.Physics.ARCADE); this.aliens = this.add.group(); this.aliens.enableBody = true; this.aliens.physicsBodyType = Phaser.Physics.ARCADE; this.createAliens(); this.scoreString = 'Score : '; this.scoreText = this.add.text(10, 10, this.scoreString + this.score, { font: '34px Arial', fill: '#fff' }); this.lives = this.add.group(); this.add.text(this.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' }); this.stateText = this.add.text(this.world.centerX,this.world.centerY,' ', { font: '84px Arial', fill: '#fff' }); this.stateText.anchor.setTo(0.5, 0.5); this.stateText.visible = false; for (var i = 0; i < 3; i++) { var ship = this.lives.create(this.world.width - 100 + (30 * i), 60, 'ship'); ship.anchor.setTo(0.5, 0.5); ship.angle = 90; ship.alpha = 0.4; } this.explosions = this.add.group(); this.explosions.createMultiple(30, 'kaboom'); this.explosions.forEach(this.setupInvader, this); this.cursors = this.input.keyboard.createCursorKeys(); this.fireButton = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; var fullscreen = this.add.button(this.game.width-8, this.game.height-8,'fullscreen',BasicGame.toggleFullscreen,this,'over', 'up', 'down'); fullscreen.pivot.x = fullscreen.width; fullscreen.pivot.y = fullscreen.height; }, update: function () { this.starfield.tilePosition.y += 2; if(this.player.alive) { this.player.body.velocity.setTo(0, 0); if (this.cursors.left.isDown) { this.player.body.velocity.x = -200; } else if (this.cursors.right.isDown) { this.player.body.velocity.x = 200; } if (this.fireButton.isDown) { this.fireBullet(); } if (this.time.now > this.firingTimer) { this.enemyFires(); } this.physics.arcade.overlap(this.bullets, this.aliens, this.collisionHandler, null, this); this.physics.arcade.overlap(this.enemyBullets, this.player, this.enemyHitsPlayer, null, this); this.physics.arcade.overlap(this.player, this.aliens, this.playerCollision, null, this); } }, restart: function () { this.lives.callAll('revive'); this.aliens.removeAll(); this.createAliens(); this.player.revive(); this.stateText.visible = false; }, playerCollision: function(player,aliens) { this.player.kill(); this.live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } aliens.reset(); this.createAliens(); var explosion = this.explosions.getFirstExists(false); explosion.reset(player.body.x, player.body.y); explosion.play('kaboom', 30, false, true); player.revive(); if (this.lives.countLiving() < 1) { player.kill(); this.enemyBullets.callAll('kill'); // this.stateText.text=" GAME OVER \n Click to restart"; this.stateText.visible = true; this.input.onTap.addOnce(this.restart,this); } }, enemyFires: function() { this.enemyBullet = this.enemyBullets.getFirstExists(false); this.livingEnemies.length = 0; this.aliens.forEachAlive(function(alien){ this.livingEnemies.push(alien); },this); this.physics.enable(this.enemyBullet, Phaser.Physics.ARCADE); if (this.enemyBullet && this.livingEnemies.length > 0) { var random = this.rnd.integerInRange(0,this.livingEnemies.length-1); var shooter = this.livingEnemies[random]; this.enemyBullet.reset(shooter.body.x, shooter.body.y); this.physics.arcade.moveToObject(this.enemyBullet,this.player,120); this.firingTimer = this.time.now + 2000; } }, collisionHandler: function(bullet, alien) { bullet.kill(); alien.kill(); this.score += 20; this.scoreText.text = this.scoreString + this.score; this.explosion = this.explosions.getFirstExists(false); this.explosion.reset(alien.body.x, alien.body.y); this.explosion.play('kaboom', 30, false, true); if (this.aliens.countLiving() == 0) { this.score += 1000; this.scoreText.text = this.scoreString + this.score; this.stateText.text = " You Won, \n Click to restart"; this.stateText.visible = true; this.enemyBullets.callAll('kill'); this.input.onTap.addOnce(this.restart,this); } }, fireBullet: function() { if (this.time.now > this.bulletTime) { this.bullet = this.bullets.getFirstExists(false); if (this.bullet) { this.bullet.reset(this.player.x, this.player.y + 8); this.bullet.body.velocity.y = -400; this.bulletTime = this.time.now + 200; } } }, restart: function() { this.lives.callAll('revive'); this.aliens.removeAll(); this.createAliens(); this.player.revive(); this.stateText.visible = false; }, gofull: function() { if (this.scale.isFullScreen) { this.scale.stopFullScreen(); } else { this.scale.startFullScreen(false); } }, resetBullet: function(bullet) { bullet.kill(); }, setupInvader: function (invader) { invader.anchor.x = 0.5; invader.anchor.y = 0.5; invader.animations.add('kaboom'); }, descend: function() { this.aliens.y += 10; }, createAliens: function () { for (var y = 0; y < 4; y++) { for (var x = 0; x < 10; x++) { var alien = this.aliens.create(x * 48, y * 50, 'invader'); alien.anchor.setTo(0.5, 0.5); alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true); alien.play('fly'); alien.body.moves = false; } } this.aliens.x = 100; this.aliens.y = 50; this.tween = this.add.tween(this.aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); this.tween.onLoop.add(this.descend, this); }, enemyHitsPlayer: function(player,bullet) { bullet.kill(); this.live = this.lives.getFirstAlive(); if (this.live) { this.live.kill(); } var explosion = this.explosions.getFirstExists(false); explosion.reset(player.body.x, player.body.y); explosion.play('kaboom', 30, false, true); if (this.lives.countLiving() < 1) { player.kill(); this.enemyBullets.callAll('kill'); this.stateText.text=" GAME OVER \n Click to restart"; this.stateText.visible = true; this.input.onTap.addOnce(this.restart,this); } }, quitGame: function (pointer) { this.state.start('MainMenu'); },}; phuurup 1 Link to comment Share on other sites More sharing options...
phuurup Posted May 21, 2015 Share Posted May 21, 2015 great reference! thanks! Speakeasys 1 Link to comment Share on other sites More sharing options...
Recommended Posts