diff --git a/Phaser/Motion.ts b/Phaser/Motion.ts index a9f15826..0dfb4630 100644 --- a/Phaser/Motion.ts +++ b/Phaser/Motion.ts @@ -79,6 +79,11 @@ module Phaser { */ public velocityFromAngle(angle: number, speed: number): Point { + if (isNaN(speed)) + { + speed = 0; + } + var a: number = this._game.math.degreesToRadians(angle); return new Point((Math.cos(a) * speed), (Math.sin(a) * speed)); diff --git a/Phaser/gameobjects/GameObject.ts b/Phaser/gameobjects/GameObject.ts index b7c57e55..271a1f9e 100644 --- a/Phaser/gameobjects/GameObject.ts +++ b/Phaser/gameobjects/GameObject.ts @@ -76,6 +76,11 @@ module Phaser { public origin: MicroPoint; public z: number = 0; + // This value is added to the angle of the GameObject. + // For example if you had a sprite drawn facing straight up then you could set + // rotationOffset to 90 and it would correspond correctly with Phasers rotation system + public rotationOffset: number = 0; + // Physics properties public immovable: bool; diff --git a/Phaser/gameobjects/Sprite.ts b/Phaser/gameobjects/Sprite.ts index c9c5d845..15fc1a57 100644 --- a/Phaser/gameobjects/Sprite.ts +++ b/Phaser/gameobjects/Sprite.ts @@ -224,14 +224,14 @@ module Phaser { } // Rotation - needs to work from origin point really, but for now from center - if (this.angle !== 0 || this.flipped == true) + if (this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true) { this._game.stage.context.save(); this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2)); - if (this.angle !== 0) + if (this.angle !== 0 || this.rotationOffset !== 0) { - this._game.stage.context.rotate(this.angle * (Math.PI / 180)); + this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180)); } this._dx = -(this._dw / 2); diff --git a/README.md b/README.md index b4d2709b..ef934c0b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Phaser Version 0.9.3 -21st April 2013 +23rd April 2013 By Richard Davey, [Photon Storm](http://www.photonstorm.com) @@ -25,9 +25,9 @@ V0.9.3 * Re-built Tilemap handling from scratch to allow for proper layered maps (as exported from Tiled / Mappy) * Tilemap no longer requires a buffer per Camera (in prep for WebGL support) * Added shiftSinTable and shiftCosTable to the GameMath class to allow for quick iteration through the data tables. -* Added the new ScrollZone game object. Endlessly useful but especially for scrolling backdrops. Created several example tests. +* Added the new ScrollZone game object. Endlessly useful but especially for scrolling backdrops. Created 6 example tests. * Removed the need for DynamicTextures to require a key property and updated test cases. - +* Add the rotationOffset value to GameObject (and thus Sprite). Useful if your graphics need to rotate but don't weren't drawn facing zero degrees (to the right). V0.9.2 diff --git a/Tests/phaser.js b/Tests/phaser.js index e31ef067..3e72f0d0 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -467,6 +467,10 @@ var Phaser; _super.call(this, game); this._angle = 0; this.z = 0; + // This value is added to the angle of the GameObject. + // For example if you had a sprite drawn facing straight up then you could set + // rotationOffset to 90 and it would correspond correctly with Phasers rotation system + this.rotationOffset = 0; this.moves = true; // Input this.inputEnabled = false; @@ -1541,11 +1545,11 @@ var Phaser; this._dy -= (camera.worldView.y * this.scrollFactor.y); } // Rotation - needs to work from origin point really, but for now from center - if(this.angle !== 0 || this.flipped == true) { + if(this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true) { this._game.stage.context.save(); this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2)); - if(this.angle !== 0) { - this._game.stage.context.rotate(this.angle * (Math.PI / 180)); + if(this.angle !== 0 || this.rotationOffset !== 0) { + this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180)); } this._dx = -(this._dw / 2); this._dy = -(this._dh / 2); @@ -6983,6 +6987,9 @@ var Phaser; * @return A Point where Point.x contains the velocity x value and Point.y contains the velocity y value */ function (angle, speed) { + if(isNaN(speed)) { + speed = 0; + } var a = this._game.math.degreesToRadians(angle); return new Phaser.Point((Math.cos(a) * speed), (Math.sin(a) * speed)); }; diff --git a/Tests/scrollzones/parallax.js b/Tests/scrollzones/parallax.js index 454e7910..fde10928 100644 --- a/Tests/scrollzones/parallax.js +++ b/Tests/scrollzones/parallax.js @@ -8,6 +8,7 @@ } function create() { var zone = myGame.createScrollZone('starray'); + // Hide the default region (the full image) zone.currentRegion.visible = false; var y = 0; var speed = 16; diff --git a/Tests/scrollzones/parallax.ts b/Tests/scrollzones/parallax.ts index 3e6b0edc..48d04645 100644 --- a/Tests/scrollzones/parallax.ts +++ b/Tests/scrollzones/parallax.ts @@ -17,6 +17,7 @@ var zone: Phaser.ScrollZone = myGame.createScrollZone('starray'); + // Hide the default region (the full image) zone.currentRegion.visible = false; var y:number = 0; diff --git a/Tests/scrollzones/texture repeat.js b/Tests/scrollzones/texture repeat.js index dbd9a790..3aed931c 100644 --- a/Tests/scrollzones/texture repeat.js +++ b/Tests/scrollzones/texture repeat.js @@ -3,19 +3,49 @@ (function () { var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update); function init() { + myGame.loader.addImageFile('nashwan', 'assets/sprites/xenon2_ship.png'); myGame.loader.addImageFile('starfield', 'assets/misc/starfield.jpg'); + myGame.loader.addImageFile('jet', 'assets/sprites/jets.png'); myGame.loader.load(); } var scroller; + var emitter; var ship; + var speed = 0; function create() { - // 512 x 512 - scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 512); - // Some sin/cos data for the movement - myGame.math.sinCosGenerator(256, 4, 4, 2); + scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 1024); + emitter = myGame.createEmitter(myGame.stage.centerX + 16, myGame.stage.centerY + 12); + emitter.makeParticles('jet', 250, 0, false, 0); + //emitter.lifespan + ship = myGame.createSprite(myGame.stage.centerX, myGame.stage.centerY, 'nashwan'); + // We do this because the ship was drawn facing up, but 0 degrees is pointing to the right + ship.rotationOffset = 90; } function update() { - scroller.currentRegion.scrollSpeed.x = myGame.math.shiftSinTable(); - scroller.currentRegion.scrollSpeed.y = myGame.math.shiftCosTable(); + ship.angularVelocity = 0; + if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { + ship.angularVelocity = -200; + } else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { + ship.angularVelocity = 200; + } + if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) { + speed += 0.1; + if(speed > 10) { + speed = 10; + } + } else { + speed -= 0.1; + if(speed < 0) { + speed = 0; + } + } + var motion = myGame.motion.velocityFromAngle(ship.angle, speed); + scroller.setSpeed(motion.x, motion.y); + // emit particles + if(speed > 2) { + emitter.setXSpeed(-(motion.x * 20), -(motion.x * 30)); + emitter.setYSpeed(-(motion.y * 20), -(motion.y * 30)); + emitter.emitParticle(); + } } })(); diff --git a/Tests/scrollzones/texture repeat.ts b/Tests/scrollzones/texture repeat.ts index 94572196..2ada7f16 100644 --- a/Tests/scrollzones/texture repeat.ts +++ b/Tests/scrollzones/texture repeat.ts @@ -7,29 +7,78 @@ function init() { + myGame.loader.addImageFile('nashwan', 'assets/sprites/xenon2_ship.png'); myGame.loader.addImageFile('starfield', 'assets/misc/starfield.jpg'); + myGame.loader.addImageFile('jet', 'assets/sprites/jets.png'); myGame.loader.load(); } var scroller: Phaser.ScrollZone; + var emitter: Phaser.Emitter; var ship: Phaser.Sprite; + var speed: number = 0; + function create() { - // 512 x 512 - scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 512); + scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 1024); - // Some sin/cos data for the movement - myGame.math.sinCosGenerator(256, 4, 4, 2); + emitter = myGame.createEmitter(myGame.stage.centerX + 16, myGame.stage.centerY + 12); + emitter.makeParticles('jet', 250, 0, false, 0); + //emitter.lifespan + + + ship = myGame.createSprite(myGame.stage.centerX, myGame.stage.centerY, 'nashwan'); + + // We do this because the ship was drawn facing up, but 0 degrees is pointing to the right + ship.rotationOffset = 90; } function update() { - scroller.currentRegion.scrollSpeed.x = myGame.math.shiftSinTable(); - scroller.currentRegion.scrollSpeed.y = myGame.math.shiftCosTable(); + ship.angularVelocity = 0; + + if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) + { + ship.angularVelocity = -200; + } + else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) + { + ship.angularVelocity = 200; + } + + if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) + { + speed += 0.1; + + if (speed > 10) + { + speed = 10; + } + } + else + { + speed -= 0.1; + + if (speed < 0) { + speed = 0; + } + } + + var motion:Phaser.Point = myGame.motion.velocityFromAngle(ship.angle, speed); + + scroller.setSpeed(motion.x, motion.y); + + // emit particles + if (speed > 2) + { + emitter.setXSpeed(-(motion.x * 20), -(motion.x * 30)); + emitter.setYSpeed(-(motion.y * 20), -(motion.y * 30)); + emitter.emitParticle(); + } } diff --git a/build/phaser.js b/build/phaser.js index e31ef067..3e72f0d0 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -467,6 +467,10 @@ var Phaser; _super.call(this, game); this._angle = 0; this.z = 0; + // This value is added to the angle of the GameObject. + // For example if you had a sprite drawn facing straight up then you could set + // rotationOffset to 90 and it would correspond correctly with Phasers rotation system + this.rotationOffset = 0; this.moves = true; // Input this.inputEnabled = false; @@ -1541,11 +1545,11 @@ var Phaser; this._dy -= (camera.worldView.y * this.scrollFactor.y); } // Rotation - needs to work from origin point really, but for now from center - if(this.angle !== 0 || this.flipped == true) { + if(this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true) { this._game.stage.context.save(); this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2)); - if(this.angle !== 0) { - this._game.stage.context.rotate(this.angle * (Math.PI / 180)); + if(this.angle !== 0 || this.rotationOffset !== 0) { + this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180)); } this._dx = -(this._dw / 2); this._dy = -(this._dh / 2); @@ -6983,6 +6987,9 @@ var Phaser; * @return A Point where Point.x contains the velocity x value and Point.y contains the velocity y value */ function (angle, speed) { + if(isNaN(speed)) { + speed = 0; + } var a = this._game.math.degreesToRadians(angle); return new Phaser.Point((Math.cos(a) * speed), (Math.sin(a) * speed)); };