diff --git a/templateGame/lib/kiwipreloader.js b/templateGame/lib/kiwipreloader.js index d348457c..6b25d4f1 100644 --- a/templateGame/lib/kiwipreloader.js +++ b/templateGame/lib/kiwipreloader.js @@ -1,137 +1,170 @@ /** -* The Kiwi.JS Preloader is a custom State, that is used wanting to load in any assets for a game. -* Using this Class is very easy. -* All you have to do to use it is: -* 1 - Instantiate this Class instead of a State and pass a few extra parameters. -* 2 - Create a preload method on the new Object (that extends the class) BUT make sure you call the preload method on this class!!! -* 3 - Switch to it (like you would anyway) +* The Kiwi.JS Preloader is a custom State used to load assets into a game +* using a loading bar and animated loading screen. +* +* Using this Class is very easy: +* 1 - Instantiate this Class instead of a State and pass required parameters. +* 2 - Create a preload method on the new Object, ensuring you call the preload +* method on this Class. +* 3 - Switch to it as normal. * 4 - And DONE! -* +* +* ``` +* var loadingState = new KiwiLoadingScreen( +* "loadingState", +* "nextState", +* "assets/loadingAssets/", +* { width: 1024, height: 768 } ); +* +* loadingState.preload = function() { +* KiwiLoadingScreen.prototype.preload.call( this ); +* +* // Your code here +* }; +* ``` * * @class KiwiLoadingScreen * @constructor * @extends Kiwi.State -* @param name {String} Name of this state. -* @param stateToSwitch {String} Name of the state to switch to AFTER all the assets have loaded. Note: The state you want to switch to should already have been added to the game. -* @param subfolder {String} The folder that the loading graphics are located at. -* @param dimensions {Object} A Object containing the width/height that the game is to be. For example {width: 1024, height: 768}. Defaults to the stages width/height if not passed. +* @param name {string} Name of this state. +* @param stateToSwitch {string} Name of the state to switch to AFTER all the +* assets have loaded. Note: The state you want to switch to should already +* have been added to the game. +* @param subfolder {string} Folder wherein the loading graphics are located. +* @param [dimensions] {object} Object containing the width/height of the game. +* For example, `{width: 1024, height: 768}`. Defaults to the stage's +* width/height if not passed. * @return {KiwiLoadingScreen} */ -var KiwiLoadingScreen = function(name, stateToSwitch,subfolder, dimensions) { +var KiwiLoadingScreen = + function( name, stateToSwitch, subfolder, dimensions ) { - //Call the Super + // Call the Super Kiwi.State.call(this, name); - //Check the width/height - if(dimensions !== undefined && dimensions.width !== undefined && dimensions.height != undefined) { + // Check the width/height + if ( dimensions !== undefined && + dimensions.width !== undefined && + dimensions.height !== undefined ) { this.newDimensions = dimensions; } else { this.newDimensions = null; } - //Save the state to load afterwards + // Save the state to load afterwards this.afterState = stateToSwitch; this.kiwiAlpha = 0; - //The Loading children we are going to make + // The Loading children we are going to make this.html5Logo = null; - //Splash + // Splash this.kiwijsLogo = null; this.kiwijsText = null; this.radial = null; this.madeWith = null; this.loadingBar = null; - //The subfolder where everything will be saved + // The subfolder where everything will be saved this.subfolder = subfolder; - -} - -//Extend the State +}; +// Extend the State Kiwi.extend(KiwiLoadingScreen, Kiwi.State); + KiwiLoadingScreen.prototype.init = function() { - - Kiwi.State.prototype.init.call(this); + Kiwi.State.prototype.init.call(this); - if(this.newDimensions !== null) { - this.game.stage.resize( this.newDimensions.width, this.newDimensions.height ); - } + if ( this.newDimensions !== null ) { + this.game.stage.resize( + this.newDimensions.width, this.newDimensions.height ); + } +}; -} /** -* Preload Method. This is the method you override when you are wanting to load in your assets. -* Note: Make sure you call this method! Otherwise the loading graphics will never load and this state won't work!!! Also best to call it at the start of the PRELOAD. +* Preload Method. This is the method you override to load in your assets. +* Note: Make sure you call this method! Otherwise the loading graphics will +* never load and this state won't work. +* Also best to call it at the start of the `preload`. * @method preload * @public */ KiwiLoadingScreen.prototype.preload = function() { - //BackgroundColour - this.game.stage.color = '061029'; + // Background colour + this.game.stage.color = "061029"; - //New Dimensions - var shortest = (this.game.stage.width > this.game.stage.height) ? this.game.stage.height : this.game.stage.width; + // New Dimensions + var shortest = (this.game.stage.width > this.game.stage.height) ? + this.game.stage.height : this.game.stage.width; - if(shortest < 600) { + if ( shortest < 600 ) { this.scaled = shortest / 600; } else { this.scaled = 1; } - - //Should combine into a texture atlas... this.currentSplashState = 0; // 0 = HTML 5 LOGO FADING IN // 1 = KIWIJS READY TO APPEAR // 2 = KIWIJS FADING IN // 3 = LOADING WITH KIWIJS THERE // 4 = DONE. SWITCHING TO NEXT STATE - this.addTextureAtlas('loadingGraphic', this.subfolder + 'loading-texture-atlas.png', 'loadingJSON', this.subfolder + 'loading-texture-atlas.json', false); + this.addTextureAtlas( + "loadingGraphic", + this.subfolder + "loading-texture-atlas.png", + "loadingJSON", + this.subfolder + "loading-texture-atlas.json", + false); - //Information about the files we need to load - this.loadingData = {toLoad: 0, loaded: 0}; - this.filesToLoad = ['loadingGraphic', 'loadingJSON']; + // Information about the files we need to load + this.loadingData = { toLoad: 0, loaded: 0 }; + this.filesToLoad = [ "loadingGraphic", "loadingJSON" ]; this.percentLoaded = 0; -} +}; /** -* Start Loading everything in. +* Start loading everything in. * @method loadProgress * @public */ -KiwiLoadingScreen.prototype.loadProgress = function (percent, bytesLoaded, file) { +KiwiLoadingScreen.prototype.loadProgress = + function ( percent, bytesLoaded, file ) { this.percentLoaded = percent; - if(this.currentSplashState == 3) { - this.loadingBar.scaleX = this.percentLoaded / 100 * this.scaled; + if ( this.currentSplashState === 3 ) { + this.loadingBar.scaleX = this.percentLoaded / 100 * this.scaled; this.finishLoading(); } - if(file == null || file == undefined) return; + if ( file == null ) { + return; + } - if( this.filesToLoad.length > 0 ) { - + if ( this.filesToLoad.length > 0 ) { var index = this.filesToLoad.indexOf( file.key ); - if( index !== -1 ) { + if ( index !== -1 ) { this.filesToLoad.splice( index, 1 ); } - if( this.filesToLoad.length == 0) { - //Add to the Library - this.game.states.rebuildLibraries(); + if ( this.filesToLoad.length === 0 ) { + // Add to the Library + this.game.states.rebuildLibraries(); - //Create the StaticImage - this.html5Logo = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], this.game.stage.width / 2, this.game.stage.height / 2); + // Create the StaticImage + this.html5Logo = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + this.game.stage.width / 2, + this.game.stage.height / 2 ); this.html5Logo.cellIndex = 0; this.html5Logo.scaleX = this.scaled; this.html5Logo.scaleY = this.scaled; @@ -140,101 +173,132 @@ KiwiLoadingScreen.prototype.loadProgress = function (percent, bytesLoaded, file) this.html5Logo.alpha = 0; this.html5Logo.rotPointX = 0; this.html5Logo.rotPointY = 0; - this.addChild(this.html5Logo); - - //Tween - this.loadingTween = this.game.tweens.create(this.html5Logo); - this.loadingTween.onComplete(this.fadeInHTML5, this); - this.loadingTween.to({ alpha: 1 }, 500, Kiwi.Animations.Tweens.Easing.Linear.None); - this.loadingTween._onCompleteCalled = false; - this.loadingTween.start(); + this.addChild( this.html5Logo ); + + // Tween + this.loadingTween = this.game.tweens.create( this.html5Logo ); + this.loadingTween.onComplete( this.fadeInHTML5, this ); + this.loadingTween.to( + { alpha: 1 }, + 500, + Kiwi.Animations.Tweens.Easing.Linear.None ); + this.loadingTween._onCompleteCalled = false; + this.loadingTween.start(); - //Don't need to do anything else - return; + return; } - } - if(this.currentSplashState <= 1 && this.loadingData.loaded == this.loadingData.toLoad) { - - //So the HTML 5 logo was waiting for the KIWI splash assets to load. - if( this.currentSplashState == 1 ) { - //Play the tween. No delay because there most likely already was one - this.loadingTween._onCompleteCalled = false; + if ( this.currentSplashState <= 1 && + this.loadingData.loaded === this.loadingData.toLoad ) { + + // So the HTML 5 logo was waiting for the KiwiJS splash assets to load. + if ( this.currentSplashState === 1 ) { + + // Play the tween. No delay as there probably already was one + this.loadingTween._onCompleteCalled = false; this.loadingTween.start(); - this.currentSplashState = 2; + this.currentSplashState = 2; - //Still waiting for the logo to fade in? Tell it to fade out when its done. + // Still waiting for the logo to fade in? + // Tell it to fade out when it's done. } else { this.currentSplashState = 1; } - } - -} +}; /** * Create: rebuild html5Logo -**/ -KiwiLoadingScreen.prototype.create = function(){ +* @method create +* @public +*/ +KiwiLoadingScreen.prototype.create = function() { Kiwi.State.prototype.create.call( this ); - - // Reassign texture atlas to html5Logo, as it has just been rebuilt and WebGL may have lost track of it + + // Reassign texture atlas to html5Logo, + // as it has just been rebuilt and WebGL may have lost track of it this.html5Logo.atlas = this.textures.loadingGraphic; -} +}; /** -* Called when the fading in of the HTML5 logo is completed. Makes the HTML5 logo fade out oafter a period of time. +* Called when the fading in of the HTML5 logo is completed. +* Makes the HTML5 logo fade out oafter a period of time. * @method fadeInHTML5 -* +* @public */ KiwiLoadingScreen.prototype.fadeInHTML5 = function() { - this.loadingTween.to({ alpha: 0 }, 500, Kiwi.Animations.Tweens.Easing.Linear.None); - this.loadingTween.onComplete(this.fadeOutHTML5, this); - - //Have all the assets for the next splash screen loaded in the time it has taken us to fade the logo in? - if( this.currentSplashState >= 1) { - //Start the fadeout tween after a delay - this.loadingTween.delay(500); - this.loadingTween._onCompleteCalled = false; + this.loadingTween.to( + { alpha: 0 }, + 500, + Kiwi.Animations.Tweens.Easing.Linear.None ); + this.loadingTween.onComplete( this.fadeOutHTML5, this ); + + // Have all the assets for the next splash screen loaded + // in the time it has taken us to fade the logo in? + if ( this.currentSplashState >= 1) { + + // Start the fadeout tween after a delay + this.loadingTween.delay( 500 ); + this.loadingTween._onCompleteCalled = false; this.loadingTween.start(); - this.currentSplashState = 2; + this.currentSplashState = 2; //Otherwise say we are ready. } else { - this.currentSplashState = 1; + this.currentSplashState = 1; } -} +}; /** * Called when the HTML5 logo has fulled faded in. Creates/fades in the KIWI. * @method fadeOutHTML5 -* -* +* @public */ KiwiLoadingScreen.prototype.fadeOutHTML5 = function() { this.currentSplashState = 3; - //Remove the logo + // Remove the logo this.html5Logo.exists = false; - //Create all the HTML 5 assets - this.kiwijsLogo = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], this.game.stage.width / 2, this.game.stage.height / 2); - this.kiwijsText = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], this.game.stage.width / 2, this.game.stage.height / 2); - this.radial = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], this.game.stage.width / 2, this.game.stage.height / 2); - this.madeWith = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], this.game.stage.width / 2, this.game.stage.height); - this.loadingBar = new Kiwi.GameObjects.StaticImage(this, this.textures['loadingGraphic'], 0, 0); + // Create all the HTML5 assets + this.kiwijsLogo = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + this.game.stage.width / 2, + this.game.stage.height / 2 ); + this.kiwijsText = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + this.game.stage.width / 2, + this.game.stage.height / 2 ); + this.radial = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + this.game.stage.width / 2, + this.game.stage.height / 2 ); + this.madeWith = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + this.game.stage.width / 2, + this.game.stage.height ); + this.loadingBar = new Kiwi.GameObjects.StaticImage( + this, + this.textures[ "loadingGraphic" ], + 0, + 0 ); this.kiwijsLogo.cellIndex = 4; this.kiwijsText.cellIndex = 2; this.radial.cellIndex = 1; this.madeWith.cellIndex = 5; this.loadingBar.cellIndex = 3; - //Adjust Coordinates + + // Adjust Coordinates this.radial.scaleX = this.scaled; this.radial.scaleY = this.scaled; this.radial.rotPointX = 0; @@ -252,143 +316,163 @@ KiwiLoadingScreen.prototype.fadeOutHTML5 = function() { this.kiwijsLogo.scaleX = this.scaled; this.kiwijsLogo.scaleY = this.scaled; - this.kiwijsLogo.rotPointX = this.kiwijsLogo.width * 0.2727; this.kiwijsLogo.rotPointY = this.kiwijsLogo.height; - //Swinging Kiwi Logo - this.kiwijsLogo.x = this.radial.x + ( this.radial.box.bounds.width * 0.5 - this.kiwijsLogo.box.bounds.width * 0.5); - this.kiwijsLogo.y = this.radial.y + ( this.radial.box.bounds.height * 0.5 - this.kiwijsLogo.box.bounds.height * 0.5); - - if(this.scaled < 1) { - this.kiwijsLogo.y -= this.kiwijsLogo.rotPointY * (1 - this.scaled); - this.kiwijsLogo.x -= this.kiwijsLogo.box.bounds.width * (1 - this.scaled); + // Swinging Kiwi Logo + this.kiwijsLogo.x = this.radial.x + ( this.radial.box.bounds.width * 0.5 - + this.kiwijsLogo.box.bounds.width * 0.5); + this.kiwijsLogo.y = this.radial.y + ( this.radial.box.bounds.height * 0.5 - + this.kiwijsLogo.box.bounds.height * 0.5); + + if ( this.scaled < 1 ) { + this.kiwijsLogo.y -= + this.kiwijsLogo.rotPointY * ( 1 - this.scaled ); + this.kiwijsLogo.x -= + this.kiwijsLogo.box.bounds.width * (1 - this.scaled); } - - this.swing = this.game.tweens.create(this.kiwijsLogo); + this.swing = this.game.tweens.create( this.kiwijsLogo ); this.swingForward(); - this.kiwijsText.scaleX = this.scaled; this.kiwijsText.scaleY = this.scaled; this.kiwijsText.rotPointX = 0; this.kiwijsText.rotPointY = 0; - this.kiwijsText.x = this.kiwijsLogo.box.bounds.right - this.kiwijsText.box.bounds.width / 1.75; - this.kiwijsText.y = this.kiwijsLogo.box.bounds.bottom - this.kiwijsText.box.bounds.height * 1.2; - + this.kiwijsText.x = this.kiwijsLogo.box.bounds.right - + this.kiwijsText.box.bounds.width / 1.75; + this.kiwijsText.y = this.kiwijsLogo.box.bounds.bottom - + this.kiwijsText.box.bounds.height * 1.2; this.loadingBar.rotPointX = 0; this.loadingBar.rotPointY = 0; this.loadingBar.scaleX = this.scaled; this.loadingBar.scaleY = this.scaled; this.loadingBar.x = this.kiwijsText.x; - this.loadingBar.y = this.kiwijsText.y + this.kiwijsText.box.bounds.height * 0.85; - + this.loadingBar.y = + this.kiwijsText.y + this.kiwijsText.box.bounds.height * 0.85; - //Alpha + // Alpha this.kiwijsLogo.alpha = 0; this.kiwijsText.alpha = 0; this.radial.alpha = 0; this.madeWith.alpha = 0; this.loadingBar.alpha = 0; - - //Add then in the right order + // Add then in the right order this.addChild(this.radial); this.addChild(this.madeWith); this.addChild(this.kiwijsLogo); this.addChild(this.kiwijsText); this.addChild(this.loadingBar); + // Scale the LoadingBar + this.loadingBar.scaleX = (this.percentLoaded / 100) * this.scaled; - //Scale the LoadingBar - this.loadingBar.scaleX = (this.percentLoaded / 100) * this.scaled; - - - //Start the Tween + // Start the Tween this.loadingTween._object = this; - this.loadingTween.to({ kiwiAlpha : 1 }, 750, Kiwi.Animations.Tweens.Easing.Linear.None); - this.loadingTween.onComplete(null, null); - this.loadingTween.onUpdate(this.updatedAlpha, this); - this.loadingTween.onComplete(this.finishLoading, this); - this.loadingTween._onCompleteCalled = false; + this.loadingTween.to( + { kiwiAlpha : 1 }, + 750, + Kiwi.Animations.Tweens.Easing.Linear.None ); + this.loadingTween.onComplete( null, null ); + this.loadingTween.onUpdate( this.updatedAlpha, this ); + this.loadingTween.onComplete( this.finishLoading, this ); + this.loadingTween._onCompleteCalled = false; this.loadingTween.start(); - - -} +}; /** -* Makes the Kiwi Swing Back. Called when he has fully swung forward. +* Makes the Kiwi swing back. Called when it has fully swung forward. * @method swingBack -* +* @public */ KiwiLoadingScreen.prototype.swingBack = function() { - if(this.currentSplashState == 3) { + if ( this.currentSplashState === 3 ) { this.swing.onComplete(this.swingForward, this); - this.swing.to({ rotation: -(Math.PI / 50)}, 400, Kiwi.Animations.Tweens.Easing.Circular.OUT); - this.swing._onCompleteCalled = false; - this.swing.start(); + this.swing.to( + { rotation: -Math.PI / 50 }, + 400, + Kiwi.Animations.Tweens.Easing.Circular.OUT ); + this.swing._onCompleteCalled = false; + this.swing.start(); } -} +}; /** -* Makes the Kiwi Swing Forward. Called when he has fully swung back. -* @method swingBack -* +* Makes the Kiwi swing forward. Called when it has fully swung back. +* @method swingForward +* @public */ KiwiLoadingScreen.prototype.swingForward = function() { - if(this.currentSplashState == 3) { - this.swing.onComplete(this.swingBack, this); - this.swing.to({ rotation: Math.PI / 15}, 400, Kiwi.Animations.Tweens.Easing.Circular.OUT); - this.swing._onCompleteCalled = false; - this.swing.start(); + if ( this.currentSplashState === 3 ) { + this.swing.onComplete( this.swingBack, this ); + this.swing.to( + { rotation: Math.PI / 15 }, + 400, + Kiwi.Animations.Tweens.Easing.Circular.OUT ); + this.swing._onCompleteCalled = false; + this.swing.start(); } -} +}; /** * Checks to see if all the assets have loaded. Fades out the Kiwi. * @method finishLoading -* +* @public */ KiwiLoadingScreen.prototype.finishLoading = function() { - if(this.percentLoaded == 100) { - this.loadingTween.to({ kiwiAlpha : 0 }, 500, Kiwi.Animations.Tweens.Easing.Linear.None); - this.loadingTween.onComplete(this.completed, this); - this.loadingTween._onCompleteCalled = false; - this.loadingTween.start(); + if ( this.percentLoaded === 100 ) { + this.loadingTween.to( + { kiwiAlpha : 0 }, + 500, + Kiwi.Animations.Tweens.Easing.Linear.None ); + this.loadingTween.onComplete( this.completed, this ); + this.loadingTween._onCompleteCalled = false; + this.loadingTween.start(); } -} +}; -// Updates/Fade in and out the alpha -KiwiLoadingScreen.prototype.updatedAlpha = function(obj, val) { +/** +* Updates/Fade in and out the alpha +* @method updatedAlpha +* @param obj {object} Deprecated param +* @param val {number} Deprecated param +* @public +*/ +KiwiLoadingScreen.prototype.updatedAlpha = function( obj, val ) { this.kiwijsLogo.alpha = this.kiwiAlpha; this.kiwijsText.alpha = this.kiwiAlpha; this.radial.alpha = this.kiwiAlpha; this.madeWith.alpha = this.kiwiAlpha; this.loadingBar.alpha = this.kiwiAlpha; +}; -} /** * Called when the game is ready, so we can switch to the next state now. -* +* @method completed +* @public */ KiwiLoadingScreen.prototype.completed = function() { - this.currentSplashState = 4; - //Switch States - this.game.states.switchState(this.afterState); + // Switch States + this.game.states.switchState( this.afterState ); +}; -} +/** +* Shut down the loading state. +* @method shutDown +* @public +*/ KiwiLoadingScreen.prototype.shutDown = function() { this.game.tweens.removeAll(); delete this.loadingTween; delete this.swing; -} \ No newline at end of file +}; diff --git a/templateGame/src/game.js b/templateGame/src/game.js index 14bb3146..f9d1347a 100644 --- a/templateGame/src/game.js +++ b/templateGame/src/game.js @@ -1,25 +1,23 @@ - /** * The core TemplateGame game file. * * This file is only used to initalise (start-up) the main Kiwi Game * and add all of the relevant states to that Game. -* */ -//Initialise the Kiwi Game. +// Initialise the Kiwi Game. var gameOptions = { - renderer: Kiwi.RENDERER_CANVAS, + renderer: Kiwi.RENDERER_CANVAS, width: 800, height: 600 -} +}; -var game = new Kiwi.Game('content', 'TemplateGame', null, gameOptions); +var game = new Kiwi.Game( "content", "TemplateGame", null, gameOptions ); -//Add all the States we are going to use. -game.states.addState(TemplateGame.Loading); -game.states.addState(TemplateGame.Intro); -game.states.addState(TemplateGame.Play); +// Add all the States we are going to use. +game.states.addState( TemplateGame.Loading ); +game.states.addState( TemplateGame.Intro ); +game.states.addState( TemplateGame.Play ); -game.states.switchState("Loading"); \ No newline at end of file +game.states.switchState( "Loading" ); diff --git a/templateGame/src/states/intro.js b/templateGame/src/states/intro.js index dc861bd1..6d415d86 100644 --- a/templateGame/src/states/intro.js +++ b/templateGame/src/states/intro.js @@ -1,16 +1,19 @@ var TemplateGame = TemplateGame || {}; -TemplateGame.Intro = new Kiwi.State('Intro'); +TemplateGame.Intro = new Kiwi.State( "Intro" ); /** -* The IntroState is the state which would manage any main-menu functionality for your game. -* Generally this State would switch to other sub 'states' which would handle the individual features. +* IntroState manages main-menu functionality for your game. +* Generally this State would switch to other sub 'states' +* which would handle the individual features. * * Right now we are just switching straight to the PlayState. -* */ TemplateGame.Intro.create = function () { - game.states.switchState("Play"); -} \ No newline at end of file + + Kiwi.State.prototype.create.call( this ); + + game.states.switchState( "Play" ); +}; diff --git a/templateGame/src/states/loading.js b/templateGame/src/states/loading.js index db65888d..331ab0b0 100644 --- a/templateGame/src/states/loading.js +++ b/templateGame/src/states/loading.js @@ -1,39 +1,48 @@ /** -* The Loading State is going to be used to load in all of the in-game assets that we need in game. +* Loading State loads in all of the in-game assets. * -* Because in this blueprint there is only a single "hidden object" section we are going to load in all of -* the asset's at this point. -* -* If you have multiple states however, I would recommend have loading the other graphics as they are required by their states, -* Otherwise the loading times maybe a bit long and it is not the most optimal solution. +* Because in this blueprint there is only a single "hidden object" section +* we are going to load in all of the assets at this point. * +* If you have multiple states, I would recommend loading the other graphics +* as they are required by their states. +* Otherwise the loading times may be a bit long and it is not optimal. */ /** -* Since we want to use the custom Kiwi.JS loader with the bobing kiwi/html5 logo and everything. We need to extend the KiwiLoadingScreen State. -* The KiwiLoadingScreen State is an extentsion of a normal State but it has some custom code to handle the loading/bobbing/fading of all the items, so if you override a method (like the preload) for example just make sure you call the super method. +* Since we want to use the custom Kiwi.JS loader with the bobbing kiwi/html5 +* logo, we need to extend the KiwiLoadingScreen State. +* The KiwiLoadingScreen State is an extension of a normal State, +* with some custom code to handle the loading/bobbing/fading of all the items, +* so if you override a method (e.g. the preload) make sure you call the +* super method. * -* The parameters we are passing into this method are as ordered. -* 1 - name {String} Name of this state. -* 2 - stateToSwitch {String} Name of the state to switch to AFTER all the assets have loaded. Note: The state you want to switch to should already have been added to the game. -* 3 - dimensions {Object} A Object containing the width/height that the game is to be. For example {width: 1024, height: 768} -* 4 - subfolder {String} The folder that the loading graphics are located at. +* The parameters we are passing into this method are as follows: +* 1 - name {string} Name of this state. +* 2 - stateToSwitch {string} Name of the state to switch to AFTER all the +* assets have loaded. Note: The state you want to switch to should already +* have been added to the game. +* 3 - dimensions {object} A Object containing the width/height that the game +* is to be. For example `{width: 1024, height: 768}` +* 4 - subfolder {string} Folder that the loading graphics are located in */ var TemplateGame = TemplateGame || {}; -TemplateGame.Loading = new KiwiLoadingScreen('Loading', 'Intro', 'assets/img/loading/'); +TemplateGame.Loading = new KiwiLoadingScreen( + "Loading", "Intro", "assets/img/loading/" ); + TemplateGame.Loading.preload = function () { - //Make sure to call the super at the top. - //Otherwise the loading graphics will load last, and that defies the whole point in loading them. + // Make sure to call the super at the top. + // Otherwise the loading graphics will load last, + // and that defies the whole point in loading them. KiwiLoadingScreen.prototype.preload.call(this); /** - * Replace with your own in-assets to load. + * Replace with your own in-game assets to load. **/ - this.addImage('kiwiName', 'assets/img/kiwijs-name.png'); - this.addSpriteSheet('icons', 'assets/img/kiwijs-icons.png', 100, 90); - + this.addImage( "kiwiName", "assets/img/kiwijs-name.png" ); + this.addSpriteSheet( "icons", "assets/img/kiwijs-icons.png", 100, 90 ); }; diff --git a/templateGame/src/states/play.js b/templateGame/src/states/play.js index 0c1f721f..5b63bba2 100644 --- a/templateGame/src/states/play.js +++ b/templateGame/src/states/play.js @@ -1,50 +1,65 @@ var TemplateGame = TemplateGame || {}; -TemplateGame.Play = new Kiwi.State('Play'); +TemplateGame.Play = new Kiwi.State( "Play" ); /** * The PlayState in the core state that is used in the game. * * It is the state where majority of the functionality occurs 'in-game' occurs. -* */ /** -* This create method is executed when a Kiwi state has finished loading any resources that were required to load. +* This create method is executed when a Kiwi state has finished loading +* any resources that were required to load. */ TemplateGame.Play.create = function () { + Kiwi.State.prototype.create.call( this ); + /* * Replace with your own game creation code here... */ - this.name = new Kiwi.GameObjects.StaticImage(this, this.textures.kiwiName, 10, 10); + this.name = new Kiwi.GameObjects.StaticImage( + this, this.textures.kiwiName, 10, 10) ; + + this.heart = new Kiwi.GameObjects.Sprite( + this, this.textures.icons, 10, 10 ); + this.heart.cellIndex = 8; + this.heart.y = this.game.stage.height - this.heart.height - 10; + - this.heart = new Kiwi.GameObjects.Sprite(this, this.textures.icons, 10, 10); - this.heart.cellIndex = 8; - this.heart.y = this.game.stage.height - this.heart.height - 10; + this.shield = new Kiwi.GameObjects.Sprite( + this, this.textures.icons, 200, 200 ); + this.shield.cellIndex = 9; + this.shield.y = this.game.stage.height * 0.5 - this.shield.height * 0.5; + this.shield.x = this.game.stage.width * 0.5 - this.shield.width * 0.5; - this.sheild = new Kiwi.GameObjects.Sprite(this, this.textures.icons, 200, 200); - this.sheild.cellIndex = 9; - this.sheild.y = this.game.stage.height * 0.5 - this.sheild.height * 0.5; - this.sheild.x = this.game.stage.width * 0.5 - this.sheild.width * 0.5; + this.crown = new Kiwi.GameObjects.Sprite( + this, this.textures.icons, 10, 10 ); + this.crown.cellIndex = 10; + this.crown.x = this.game.stage.width - this.crown.width - 10; + this.crown.y = this.game.stage.height - this.crown.height - 10; - this.crown = new Kiwi.GameObjects.Sprite(this, this.textures.icons, 10, 10); - this.crown.cellIndex = 10; - this.crown.x = this.game.stage.width - this.crown.width - 10; - this.crown.y = this.game.stage.height - this.crown.height - 10; + this.bomb = new Kiwi.GameObjects.Sprite( + this, this.textures.icons, 0, 10 ); + this.bomb.x = this.game.stage.width - this.bomb.width -10; + + + // Add the GameObjects to the stage + this.addChild( this.heart ); + this.addChild( this.crown ); + this.addChild( this.shield ); + this.addChild( this.bomb ); + this.addChild( this.name ); +}; - this.bomb = new Kiwi.GameObjects.Sprite(this, this.textures.icons, 0, 10); - this.bomb.x = this.game.stage.width - this.bomb.width -10; +TemplateGame.Play.update = function() { + Kiwi.State.prototype.update.call( this ); - //Add the GameObjects to the stage - this.addChild(this.heart); - this.addChild(this.crown); - this.addChild(this.sheild); - this.addChild(this.bomb); - this.addChild(this.name); + this.shield.rotation += this.game.time.clock.rate * 0.01; };