State swap example done and working.

This commit is contained in:
Richard Davey 2013-09-13 05:44:04 +01:00
parent fd0a071cb3
commit 7c6e6df91a
13 changed files with 282 additions and 11 deletions

40
examples/state/Game.js Normal file
View file

@ -0,0 +1,40 @@
TestGame.Game = function (game) {
this.game = game;
};
TestGame.Game.prototype = {
preload: function () {
this.game.load.spritesheet('balls', '../assets/sprites/balls.png', 17, 17);
},
create: function () {
this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'cougar').anchor.setTo(0.5, 0.5);
p = this.game.add.emitter(100, 100, 250);
p.makeParticles('balls', [0,1,2,3,4,5]);
p.minParticleSpeed.setTo(-100, -100);
p.maxParticleSpeed.setTo(100, -200);
p.gravity = 10;
p.start(false, 3000, 10);
this.game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Quadratic.InOut, true, 0, 1000, true);
this.game.input.onDown.add(this.quitToMenu, this);
},
quitToMenu: function () {
console.log('lets quit! back to the main menu');
this.game.state.start('mainmenu');
}
}

View file

@ -0,0 +1,30 @@
TestGame.MainMenu = function (game) {
// Our main menu
this.game = game;
};
TestGame.MainMenu.prototype = {
create: function () {
var bg = this.game.add.sprite(0, 100, 'nocooper');
bg.scale.setTo(2.5, 2.5);
var t = this.game.add.sprite(100, 600, 'touhou');
t.anchor.setTo(0, 1);
button = this.game.add.button(this.game.world.centerX, 400, 'button', this.startGame, this, 2, 1, 0);
button.anchor.setTo(0.5, 0.5);
},
startGame: function () {
console.log('lets play');
this.game.state.start('game');
}
}

View file

@ -0,0 +1,28 @@
var TestGame = {};
TestGame.Preloader = function (game) {
this.game = game;
};
TestGame.Preloader.prototype = {
preload: function () {
this.game.load.image('nocooper', '../assets/pics/1984-nocooper-space.png');
this.game.load.image('touhou', '../assets/pics/aya_touhou_teng_soldier.png');
this.game.load.image('cougar', '../assets/pics/cougar_ihsf.png');
this.game.load.spritesheet('button', '../assets/buttons/button_sprite_sheet.png', 193, 71);
},
create: function () {
console.log('Preloade finished, lets go to the main menu automatically');
this.game.state.start('mainmenu');
}
}

30
examples/state/index.php Normal file
View file

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
<script src="Preloader.js"></script>
<script src="MainMenu.js"></script>
<script src="Game.js"></script>
</head>
<body>
<script type="text/javascript">
(function () {
// No parameters given, which means no default state is created or started
var game = new Phaser.Game(800, 600, Phaser.AUTO);
game.state.add('preloader', TestGame.Preloader, true);
game.state.add('mainmenu', TestGame.MainMenu);
game.state.add('game', TestGame.Game);
})();
</script>
</body>
</html>

109
examples/state/js.php Normal file
View file

@ -0,0 +1,109 @@
<?php
// All JS files in build order. Much easier for debugging
// <xscript src="../../src/pixi/extras/Spine.js"></script> <xscript src="../../src/pixi/display/MovieClip.js"></script>
?>
<script src="../../src/Intro.js"></script>
<script src="../../src/pixi/Pixi.js"></script>
<script src="../../src/Phaser.js"></script>
<script src="../../src/utils/Utils.js"></script>
<script src="../../src/pixi/core/Matrix.js"></script>
<script src="../../src/pixi/core/Point.js"></script>
<script src="../../src/pixi/core/Rectangle.js"></script>
<script src="../../src/pixi/display/DisplayObject.js"></script>
<script src="../../src/pixi/display/DisplayObjectContainer.js"></script>
<script src="../../src/pixi/display/Sprite.js"></script>
<script src="../../src/pixi/display/Stage.js"></script>
<script src="../../src/pixi/extras/CustomRenderable.js"></script>
<script src="../../src/pixi/extras/Strip.js"></script>
<script src="../../src/pixi/extras/Rope.js"></script>
<script src="../../src/pixi/extras/TilingSprite.js"></script>
<script src="../../src/pixi/filters/FilterBlock.js"></script>
<script src="../../src/pixi/filters/MaskFilter.js"></script>
<script src="../../src/pixi/primitives/Graphics.js"></script>
<script src="../../src/pixi/renderers/canvas/CanvasGraphics.js"></script>
<script src="../../src/pixi/renderers/canvas/CanvasRenderer.js"></script>
<script src="../../src/pixi/renderers/webgl/WebGLBatch.js"></script>
<script src="../../src/pixi/renderers/webgl/WebGLGraphics.js"></script>
<script src="../../src/pixi/renderers/webgl/WebGLRenderer.js"></script>
<script src="../../src/pixi/renderers/webgl/WebGLRenderGroup.js"></script>
<script src="../../src/pixi/renderers/webgl/WebGLShaders.js"></script>
<script src="../../src/pixi/text/BitmapText.js"></script>
<script src="../../src/pixi/text/Text.js"></script>
<script src="../../src/pixi/textures/BaseTexture.js"></script>
<script src="../../src/pixi/textures/Texture.js"></script>
<script src="../../src/pixi/textures/RenderTexture.js"></script>
<script src="../../src/pixi/utils/EventTarget.js"></script>
<script src="../../src/pixi/utils/Polyk.js"></script>
<script src="../../src/core/Camera.js"></script>
<script src="../../src/core/State.js"></script>
<script src="../../src/core/StateManager.js"></script>
<script src="../../src/core/LinkedList.js"></script>
<script src="../../src/core/Signal.js"></script>
<script src="../../src/core/SignalBinding.js"></script>
<script src="../../src/core/Plugin.js"></script>
<script src="../../src/core/PluginManager.js"></script>
<script src="../../src/core/Stage.js"></script>
<script src="../../src/core/Group.js"></script>
<script src="../../src/core/World.js"></script>
<script src="../../src/core/Game.js"></script>
<script src="../../src/input/Input.js"></script>
<script src="../../src/input/Keyboard.js"></script>
<script src="../../src/input/Mouse.js"></script>
<script src="../../src/input/MSPointer.js"></script>
<script src="../../src/input/Pointer.js"></script>
<script src="../../src/input/Touch.js"></script>
<script src="../../src/input/InputHandler.js"></script>
<script src="../../src/system/Canvas.js"></script>
<script src="../../src/gameobjects/Events.js"></script>
<script src="../../src/gameobjects/GameObjectFactory.js"></script>
<script src="../../src/gameobjects/Sprite.js"></script>
<script src="../../src/gameobjects/TileSprite.js"></script>
<script src="../../src/gameobjects/Text.js"></script>
<script src="../../src/gameobjects/Button.js"></script>
<script src="../../src/gameobjects/Graphics.js"></script>
<script src="../../src/gameobjects/RenderTexture.js"></script>
<script src="../../src/gameobjects/BitmapText.js"></script>
<script src="../../src/system/Canvas.js"></script>
<script src="../../src/system/StageScaleMode.js"></script>
<script src="../../src/system/Device.js"></script>
<script src="../../src/system/RequestAnimationFrame.js"></script>
<script src="../../src/math/RandomDataGenerator.js"></script>
<script src="../../src/math/Math.js"></script>
<script src="../../src/math/QuadTree.js"></script>
<script src="../../src/geom/Circle.js"></script>
<script src="../../src/geom/Point.js"></script>
<script src="../../src/geom/Rectangle.js"></script>
<script src="../../src/net/Net.js"></script>
<script src="../../src/tween/TweenManager.js"></script>
<script src="../../src/tween/Tween.js"></script>
<script src="../../src/tween/Easing.js"></script>
<script src="../../src/time/Time.js"></script>
<script src="../../src/animation/AnimationManager.js"></script>
<script src="../../src/animation/Animation.js"></script>
<script src="../../src/animation/Frame.js"></script>
<script src="../../src/animation/FrameData.js"></script>
<script src="../../src/animation/Parser.js"></script>
<script src="../../src/loader/Cache.js"></script>
<script src="../../src/loader/Loader.js"></script>
<script src="../../src/loader/Parser.js"></script>
<script src="../../src/sound/Sound.js"></script>
<script src="../../src/sound/SoundManager.js"></script>
<script src="../../src/utils/Debug.js"></script>
<script src="../../src/utils/Color.js"></script>
<script src="../../src/physics/arcade/ArcadePhysics.js"></script>
<script src="../../src/physics/arcade/Body.js"></script>
<script src="../../src/particles/Particles.js"></script>
<script src="../../src/particles/arcade/ArcadeParticles.js"></script>
<script src="../../src/particles/arcade/Emitter.js"></script>
<script src="../../src/tilemap/Tilemap.js"></script>
<script src="../../src/tilemap/TilemapLayer.js"></script>
<script src="../../src/tilemap/Tile.js"></script>
<script src="../../src/tilemap/TilemapRenderer.js"></script>

View file

@ -12,7 +12,7 @@
(function () {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { create: create });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { create: create });
function create() {

View file

@ -686,9 +686,17 @@ Phaser.Group.prototype = {
removeAll: function () {
if (this._container.children.length == 0)
{
return;
}
do
{
this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this);
if (this._container.children[0].events)
{
this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this);
}
this._container.removeChild(this._container.children[0]);
}
while (this._container.children.length > 0);

View file

@ -73,11 +73,16 @@ Phaser.LinkedList.prototype = {
callAll: function (callback) {
if (!this.first || !this.last)
{
return;
}
var entity = this.first;
do
{
if (entity[callback])
if (entity && entity[callback])
{
entity[callback].call(entity);
}

View file

@ -1,7 +1,7 @@
/**
* State
*
* This is a base State class which can be extended if you are creating your game with TypeScript.
* This is a base State class which can be extended if you are creating your own game.
* It provides quick access to common functions such as the camera, cache, input, match, sound and more.
*
* @package Phaser.State
@ -18,7 +18,7 @@ Phaser.State = function () {
this.cache = null;
this.input = null;
this.load = null;
// this.math = null;
this.math = null;
this.sound = null;
this.stage = null;
this.time = null;
@ -39,7 +39,7 @@ Phaser.State.prototype = {
this.cache = game.cache;
this.input = game.input;
this.load = game.load;
// this.math = game.math;
this.math = game.math;
this.sound = game.sound;
this.stage = game.stage;
this.time = game.time;

View file

@ -151,6 +151,7 @@ Phaser.StateManager.prototype = {
{
// console.log('Phaser.StateManager.addState: Object given');
newState = state;
newState.game = this.game;
}
else if (typeof state === 'function')
{
@ -237,9 +238,10 @@ Phaser.StateManager.prototype = {
if (clearWorld) {
//this.game.world.destroy();
this.game.world.destroy();
if (clearCache == true) {
if (clearCache == true)
{
this.game.cache.destroy();
}
}

View file

@ -107,7 +107,21 @@ Phaser.World.prototype = {
this.bounds.height = height;
}
}
},
/**
* Destroyer of worlds.
*/
destroy: function () {
this.camera.x = 0;
this.camera.y = 0;
this.game.input.reset(true);
this.group.removeAll();
}
};

View file

@ -5,6 +5,9 @@ Phaser.Text = function (game, x, y, text, style) {
text = text || '';
style = style || '';
PIXI.Text.call(this, text, style);
/*
this.canvas = document.createElement("canvas");
this.context = this.canvas.getContext("2d");
@ -21,10 +24,12 @@ Phaser.Text = function (game, x, y, text, style) {
this.updateText();
this.dirty = false;
*/
};
Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype);
// Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype);
Phaser.Text.prototype = Phaser.Utils.extend(true, PIXI.Text.prototype);
Phaser.Text.prototype.constructor = Phaser.Text;
// Add our own custom methods

View file

@ -378,7 +378,7 @@ Phaser.Input.prototype = {
**/
reset: function (hard) {
hard = hard || false;
if (typeof hard == 'undefined') { hard = false; }
this.keyboard.reset();
this.mousePointer.reset();