Sprite.loadTexture added.

This commit is contained in:
photonstorm
2013-10-10 09:03:38 +01:00
parent f10f9324ad
commit a7230aa769
16 changed files with 2079 additions and 2307 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 335 KiB

+1 -1
View File
@@ -112,7 +112,7 @@ Version 1.0.7 (in progress in the dev branch)
* Fixed the RandomDataGenerator.sow method so if you give in the same seed you'll now get the same results (thanks Hsaka)
* World.randomX/Y now works with negative World.bounds values.
* Added killOnComplete parameter to Animation.play. Really useful in situations where you want a Sprite to animate once then kill itself on complete, like an explosion effect.
* Added Sprite.loadTexture(key, frame) which allows you to load a new texture set into an existing sprite rather than having to create a new sprite.
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
+16 -12
View File
@@ -26,21 +26,25 @@
// echo $filename . "\n";
// Read the file in
$source = file_get_contents($line);
if ($filename == 'Intro.js')
if (file_exists($line))
{
// Built at: {buildDate}
$source = str_replace('{buildDate}', date('r'), $source);
$source = file_get_contents($line);
// {version}
$source = str_replace('{version}', $version, $source);
if ($filename == 'Intro.js')
{
// Built at: {buildDate}
$source = str_replace('{buildDate}', date('r'), $source);
// Set the header
$header = $source;
} else {
$output .= $source . "\n";
// {version}
$source = str_replace('{version}', $version, $source);
// Set the header
$header = $source;
}
else
{
$output .= $source . "\n";
}
}
}
}
+1675 -2271
View File
File diff suppressed because it is too large Load Diff
+51
View File
@@ -0,0 +1,51 @@
<?php
$title = "Changing a Sprite Texture";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, render: render });
function preload() {
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);
}
var bot;
function create() {
bot = game.add.sprite(200, 200, 'bot');
bot.animations.add('run');
bot.animations.play('run', 15, true);
game.input.onDown.addOnce(changeMummy, this);
}
function changeMummy() {
bot.loadTexture('mummy', 0);
bot.animations.add('walk');
bot.animations.play('walk', 30, true);
}
function render() {
game.debug.renderSpriteBounds(bot);
}
</script>
<?php
require('../foot.php');
?>
-4
View File
@@ -5,8 +5,6 @@
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
function preload() {
@@ -34,8 +32,6 @@
}
</script>
<?php
Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.
+123
View File
@@ -0,0 +1,123 @@
<?php
$title = "Transform Tests";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var testGroup;
var sprite1;
var sprite2;
function create() {
game.stage.backgroundColor = '#2d2d2d';
game.world.setBounds(-1000, -1000, 2000, 2000);
testGroup = game.add.group();
test2();
}
function test1 () {
// Test 1 - 2 sprites in world space (seems to work fine with local transform?)
sprite1 = game.add.sprite(-600, 200, 'atari');
sprite1.name = 'atari';
// sprite1.body.setSize(100, 100, 0, 0);
sprite2 = game.add.sprite(-100, 220, 'mushroom');
sprite2.name = 'mushroom';
game.camera.focusOn(sprite1);
game.camera.x += 300;
game.input.onDown.add(go1, this);
}
function test2 () {
// 1 sprite in world space (seems to work fine with local transform?) and 1 in a group
sprite1 = testGroup.create(0, -150, 'atari');
sprite1.name = 'atari';
sprite1.body.immovable = true;
// sprite1.body.setSize(100, 100, 0, 0);
sprite2 = game.add.sprite(-100, 150, 'mushroom');
sprite2.name = 'mushroom';
testGroup.x = -600;
testGroup.y = 200;
game.camera.focusOn(sprite2);
game.camera.x -= 300;
game.input.onDown.add(go2, this);
}
function go1 () {
sprite1.body.velocity.x = 100;
sprite2.body.velocity.x = -100;
}
function go2 () {
sprite2.body.velocity.x = -100;
}
function update () {
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
}
function collisionHandler (obj1, obj2) {
game.stage.backgroundColor = '#992d2d';
console.log(obj1.name + ' collided with ' + obj2.name);
}
function render() {
// game.debug.renderSpriteInfo(sprite1, 32, 32);
// game.debug.renderSpriteCollision(sprite1, 32, 400);
game.debug.renderSpriteCoords(sprite1, 32, 32);
game.debug.renderSpriteCoords(sprite2, 300, 32);
game.debug.renderCameraInfo(game.camera, 32, 500);
game.debug.renderSpriteBody(sprite1);
game.debug.renderSpriteBody(sprite2);
game.debug.renderGroupInfo(testGroup, 500, 500);
game.debug.renderPixel(testGroup.x, testGroup.y, 'rgb(255,255,0)');
}
</script>
<?php
require('../foot.php');
?>
+37 -6
View File
@@ -1,6 +1,14 @@
<?php
$title = "Tanks";
require('../head.php');
$title = "Tanks " . $_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_NAME'] == 'gametest.mobi')
{
require('head_live.php');
}
else
{
require('../head.php');
}
?>
<script type="text/javascript">
@@ -82,12 +90,14 @@
};
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
// var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload () {
game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
game.load.atlas('enemy', 'assets/games/tanks/enemy-tanks.png', 'assets/games/tanks/tanks.json');
game.load.image('logo', 'assets/games/tanks/logo.png');
game.load.image('bullet', 'assets/games/tanks/bullet.png');
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
game.load.spritesheet('kaboom', 'assets/games/tanks/explosion.png', 64, 64, 23);
@@ -104,6 +114,8 @@
var enemyBullets;
var explosions;
var logo;
var currentSpeed = 0;
var cursors;
@@ -145,7 +157,7 @@
// Create some baddies to waste :)
enemies = [];
for (var i = 0; i < 10; i++)
for (var i = 0; i < 20; i++)
{
enemies.push(new EnemyTank(i, game, tank, enemyBullets));
}
@@ -174,14 +186,26 @@
tank.bringToTop();
turret.bringToTop();
logo = game.add.sprite(0, 200, 'logo');
logo.fixedToCamera = true;
game.input.onDown.add(removeLogo, this);
game.camera.follow(tank);
game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300);
game.camera.focusOnXY(0, 0);
cursors = game.input.keyboard.createCursorKeys();
}
function removeLogo () {
game.input.onDown.remove(removeLogo, this);
logo.kill();
}
function update () {
game.physics.collide(enemyBullets, tank, bulletHitPlayer, null, this);
@@ -290,5 +314,12 @@
</script>
<?php
require('../foot.php');
if ($_SERVER['SERVER_NAME'] == 'gametest.mobi')
{
require('foot.php');
}
else
{
require('../foot.php');
}
?>
+8
View File
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>phaser - <?php echo $title?></title>
<script src="phaser.js"></script>
</head>
<body>
+12 -1
View File
@@ -154,10 +154,21 @@ Phaser.Camera.prototype = {
break;
}
},
/**
* Move the camera focus on a display object instantly.
* @method Phaser.Camera#focusOn
* @param {any} displayObject - The display object to focus the camera on. Must have visible x/y properties.
*/
focusOn: function (displayObject) {
this.setPosition(Math.round(displayObject.x - this.view.halfWidth), Math.round(displayObject.y - this.view.halfHeight));
},
/**
* Move the camera focus to a location instantly.
* Move the camera focus on a location instantly.
* @method Phaser.Camera#focusOnXY
* @param {number} x - X position.
* @param {number} y - Y position.
+97 -4
View File
@@ -263,6 +263,11 @@ Phaser.Sprite = function (game, x, y, key, frame) {
*/
this.body = new Phaser.Physics.Arcade.Body(this);
/**
* @property {number} health - Health value. Used in combination with damage() to allow for quick killing of Sprites.
*/
this.health = 1;
/**
* @property {Description} velocity - Description.
*/
@@ -349,6 +354,7 @@ Phaser.Sprite.prototype.preUpdate = function() {
this.prevY = this.y;
this.updateCache();
this.updateAnimation();
// Re-run the camera visibility check
if (this._cache.dirty)
@@ -407,7 +413,10 @@ Phaser.Sprite.prototype.updateCache = function() {
this._cache.dirty = true;
}
// Frame updated?
}
Phaser.Sprite.prototype.updateAnimation = function() {
if (this.currentFrame && this.currentFrame.uuid != this._cache.frameID)
{
this._cache.frameWidth = this.texture.frame.width;
@@ -461,6 +470,47 @@ Phaser.Sprite.prototype.postUpdate = function() {
}
Phaser.Sprite.prototype.loadTexture = function (key, frame) {
this.key = key;
if (key instanceof Phaser.RenderTexture)
{
this.currentFrame = this.game.cache.getTextureFrame(key.name);
}
else
{
if (key == null || this.game.cache.checkImageKey(key) == false)
{
key = '__default';
}
if (this.game.cache.isSpriteSheet(key))
{
this.animations.loadFrameData(this.game.cache.getFrameData(key));
if (frame !== null)
{
if (typeof frame === 'string')
{
this.frameName = frame;
}
else
{
this.frame = frame;
}
}
}
else
{
this.currentFrame = this.game.cache.getFrame(key);
}
}
this.updateAnimation();
}
Phaser.Sprite.prototype.deltaAbsX = function () {
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
}
@@ -497,11 +547,15 @@ Phaser.Sprite.prototype.centerOn = function(x, y) {
*
* @method Phaser.Sprite.prototype.revive
*/
Phaser.Sprite.prototype.revive = function() {
Phaser.Sprite.prototype.revive = function(health) {
if (typeof health === 'undefined') { health = 1; }
this.alive = true;
this.exists = true;
this.visible = true;
this.health = health;
this.events.onRevived.dispatch(this);
}
@@ -520,12 +574,33 @@ Phaser.Sprite.prototype.kill = function() {
}
/**
* Description.
*
* @method Phaser.Sprite.prototype.kill
*/
Phaser.Sprite.prototype.damage = function(amount) {
if (this.alive)
{
this.health -= amount;
if (this.health < 0)
{
this.kill();
}
}
}
/**
* Description.
*
* @method Phaser.Sprite.prototype.reset
*/
Phaser.Sprite.prototype.reset = function(x, y) {
Phaser.Sprite.prototype.reset = function(x, y, health) {
if (typeof health === 'undefined') { health = 1; }
this.x = x;
this.y = y;
@@ -536,7 +611,13 @@ Phaser.Sprite.prototype.reset = function(x, y) {
this.visible = true;
this.renderable = true;
this._outOfBoundsFired = false;
this.body.reset();
this.health = health;
if (this.body)
{
this.body.reset();
}
}
@@ -741,6 +822,18 @@ Object.defineProperty(Phaser.Sprite.prototype, "inCamera", {
});
/**
*
* @returns {boolean}
*/
Object.defineProperty(Phaser.Sprite.prototype, "worldX", {
get: function () {
return 1;
}
});
/**
* Get the input enabled state of this Sprite.
* @returns {Description}
+2 -2
View File
@@ -883,7 +883,7 @@ Phaser.Physics.Arcade.prototype = {
{
if (separate)
{
console.log('x over', this._overlap);
// console.log('x over', this._overlap);
object.x = object.x - this._overlap;
if (object.bounce.x == 0)
@@ -960,7 +960,7 @@ Phaser.Physics.Arcade.prototype = {
if (this._overlap != 0)
{
console.log('y over', this._overlap);
// console.log('y over', this._overlap);
if (separate)
{
+12 -6
View File
@@ -105,8 +105,10 @@ Phaser.Physics.Arcade.Body.prototype = {
this.embedded = false;
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;
@@ -221,12 +223,16 @@ Phaser.Physics.Arcade.Body.prototype = {
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.y = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.x = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.y = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.rotation = this.sprite.angle;
},
+45
View File
@@ -460,6 +460,7 @@ Phaser.Utils.Debug.prototype = {
this.line('angle: ' + sprite.angle.toFixed(1) + ' rotation: ' + sprite.rotation.toFixed(1));
this.line('visible: ' + sprite.visible + ' in camera: ' + sprite.inCamera);
this.line('body x: ' + sprite.body.x.toFixed(1) + ' y: ' + sprite.body.y.toFixed(1));
this.stop();
// 0 = scaleX
// 1 = skewY
@@ -510,6 +511,7 @@ Phaser.Utils.Debug.prototype = {
this.line('scaleY: ' + sprite.worldTransform[4]);
this.line('transX: ' + sprite.worldTransform[2]);
this.line('transY: ' + sprite.worldTransform[5]);
this.stop();
},
@@ -539,6 +541,49 @@ Phaser.Utils.Debug.prototype = {
this.line('scaleY: ' + sprite.localTransform[4]);
this.line('transX: ' + sprite.localTransform[2]);
this.line('transY: ' + sprite.localTransform[5]);
this.stop();
},
renderSpriteCoords: function (sprite, x, y, color) {
if (this.context == null)
{
return;
}
color = color || 'rgb(255, 255, 255)';
this.start(x, y, color);
this.line(sprite.name);
this.line('x: ' + sprite.x);
this.line('y: ' + sprite.y);
this.line('local x: ' + sprite.localTransform[2]);
this.line('local y: ' + sprite.localTransform[5]);
this.line('world x: ' + sprite.worldTransform[2]);
this.line('world y: ' + sprite.worldTransform[5]);
this.stop();
},
renderGroupInfo: function (group, x, y, color) {
if (this.context == null)
{
return;
}
color = color || 'rgb(255, 255, 255)';
this.start(x, y, color);
this.line('Group (size: ' + group.length + ')');
this.line('x: ' + group.x);
this.line('y: ' + group.y);
this.stop();
},