Skip to content

Commit 48ed27d

Browse files
committed
Finished off RenderTexture. Sprites can now accept a RenderTexture or a key when you create them. RenderTextures are also now stored in the cache under their own key, making re-using them across other Sprites much easier. Also ported over all of the Tilemap classes, but need to get them rendering.
1 parent f885f7b commit 48ed27d

11 files changed

Lines changed: 1291 additions & 72 deletions

File tree

examples/js.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,7 @@
102102
<script src="../src/particles/arcade/ArcadeParticles.js"></script>
103103
<script src="../src/particles/arcade/Emitter.js"></script>
104104

105+
<script src="../src/tilemap/Tilemap.js"></script>
106+
<script src="../src/tilemap/TilemapLayer.js"></script>
107+
<script src="../src/tilemap/Tile.js"></script>
105108

examples/rendertexture.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ function preload() {
2222

2323
function create() {
2424

25-
// var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, game.rnd.pick(images));
26-
var renderTexture = new Phaser.RenderTexture(800, 600);
25+
var tempSprite = game.add.sprite(0, 0, 'atari1');
26+
tempSprite.visible = false;
27+
28+
var renderTexture = game.add.renderTexture('fuji', 320, 200);
29+
renderTexture.render(tempSprite);
30+
31+
var texturedSprite = game.add.sprite(400, 300, renderTexture);
2732

2833
}
2934

examples/sprite_extend.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
(function () {
3333

34-
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
34+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
3535

3636
function preload() {
3737

@@ -49,12 +49,6 @@ function create() {
4949

5050
}
5151

52-
function update() {
53-
}
54-
55-
function render() {
56-
}
57-
5852
})();
5953
</script>
6054

examples/tilemap.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
19+
// CSV Tilemap Test
20+
21+
// First we load our map data (a csv file)
22+
game.load.text('csvtest', 'assets/maps/catastrophi_level2.csv');
23+
24+
// Then we load the actual tile sheet image
25+
game.load.image('csvtiles', 'assets/tiles/catastrophi_tiles_16.png');
26+
27+
}
28+
29+
function create() {
30+
31+
// game, key, mapData, format, resizeWorld, tileWidth, tileHeight
32+
33+
// This creates the tilemap using the csv and tile sheet we loaded.
34+
// We tell it use to CSV format parser. The 16x16 are the tile sizes.
35+
// The 4th parameter (true) tells the game world to resize itself based on the map dimensions or not.
36+
var t = new Phaser.Tilemap(game, 'csvtiles', 'csvtest', Phaser.Tilemap.FORMAT_CSV, true, 16, 16);
37+
38+
}
39+
40+
function update() {
41+
42+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
43+
{
44+
game.camera.x -= 4;
45+
}
46+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
47+
{
48+
game.camera.x += 4;
49+
}
50+
51+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
52+
{
53+
game.camera.y -= 4;
54+
}
55+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
56+
{
57+
game.camera.y += 4;
58+
}
59+
60+
}
61+
62+
function render() {
63+
}
64+
65+
})();
66+
</script>
67+
68+
</body>
69+
</html>

src/gameobjects/GameObjectFactory.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ Phaser.GameObjectFactory.prototype = {
2121
*
2222
* @param x {number} X position of the new sprite.
2323
* @param y {number} Y position of the new sprite.
24-
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
24+
* @param [texture] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture
2525
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
2626
* @returns {Sprite} The newly created sprite object.
2727
*/
28-
sprite: function (x, y, key, frame) {
28+
sprite: function (x, y, texture, frame) {
2929

30-
return this.world.add(new Phaser.Sprite(this.game, x, y, key, frame));
30+
return this.world.add(new Phaser.Sprite(this.game, x, y, texture, frame));
3131

3232
},
3333

@@ -36,13 +36,13 @@ Phaser.GameObjectFactory.prototype = {
3636
*
3737
* @param x {number} X position of the new sprite.
3838
* @param y {number} Y position of the new sprite.
39-
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
39+
* @param [texture] {string|RenderTexture} The image key as defined in the Game.Cache to use as the texture for this sprite OR a RenderTexture
4040
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
4141
* @returns {Sprite} The newly created sprite object.
4242
*/
43-
child: function (parent, x, y, key, frame) {
43+
child: function (parent, x, y, texture, frame) {
4444

45-
var child = this.world.add(new Phaser.Sprite(this.game, x, y, key, frame));
45+
var child = this.world.add(new Phaser.Sprite(this.game, x, y, texture, frame));
4646
parent.addChild(child);
4747
return child;
4848

@@ -52,12 +52,11 @@ Phaser.GameObjectFactory.prototype = {
5252
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
5353
*
5454
* @param obj {object} Object the tween will be run on.
55-
* @param [localReference] {bool} If true the tween will be stored in the object.tween property so long as it exists. If already set it'll be over-written.
5655
* @return {Phaser.Tween} The newly created tween object.
5756
*/
58-
tween: function (obj, localReference) {
57+
tween: function (obj) {
5958

60-
return this.game.tweens.create(obj, localReference);
59+
return this.game.tweens.create(obj);
6160

6261
},
6362

@@ -109,4 +108,14 @@ Phaser.GameObjectFactory.prototype = {
109108

110109
},
111110

111+
renderTexture: function (key, width, height) {
112+
113+
var texture = new Phaser.RenderTexture(this.game, key, width, height);
114+
115+
this.game.cache.addRenderTexture(key, texture);
116+
117+
return texture;
118+
119+
},
120+
112121
};

src/gameobjects/RenderTexture.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
Phaser.RenderTexture = function (game, width, height) {
2-
3-
// If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all
4-
this.exists = true;
5-
6-
// This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering
7-
this.alive = true;
8-
9-
this.group = null;
10-
11-
this.name = '';
1+
Phaser.RenderTexture = function (game, key, width, height) {
122

133
this.game = game;
144

5+
this.name = key;
6+
157
PIXI.EventTarget.call( this );
168

179
this.width = width || 100;

src/gameobjects/Sprite.js

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Phaser.Sprite = function (game, x, y, key, frame) {
1+
Phaser.Sprite = function (game, x, y, texture, frame) {
22

33
x = x || 0;
44
y = y || 0;
5-
key = key || null;
5+
texture = texture || null;
66
frame = frame || null;
77

88
this.game = game;
@@ -23,13 +23,47 @@ Phaser.Sprite = function (game, x, y, key, frame) {
2323
// The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.
2424
this.lifespan = 0;
2525

26-
if (key == null || this.game.cache.checkImageKey(key) == false)
26+
if (texture instanceof Phaser.RenderTexture)
2727
{
28-
key = '__default';
28+
PIXI.Sprite.call(this, texture);
29+
30+
this.currentFrame = this.game.cache.getTextureFrame(texture.name);
2931
}
32+
else
33+
{
34+
if (texture == null || this.game.cache.checkImageKey(texture) == false)
35+
{
36+
texture = '__default';
37+
}
38+
39+
PIXI.Sprite.call(this, PIXI.TextureCache[texture]);
40+
41+
if (this.game.cache.isSpriteSheet(texture))
42+
{
43+
this.animations.loadFrameData(this.game.cache.getFrameData(texture));
3044

31-
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
45+
if (frame !== null)
46+
{
47+
if (typeof frame === 'string')
48+
{
49+
this.frameName = frame;
50+
}
51+
else
52+
{
53+
this.frame = frame;
54+
}
55+
}
56+
}
57+
else
58+
{
59+
this.currentFrame = this.game.cache.getFrame(texture);
60+
}
61+
}
3262

63+
/**
64+
* The Signals you can subscribe to that are dispatched when certain things happen on this Sprite or its components
65+
* @type Events
66+
*/
3367
this.events = new Phaser.Events(this);
3468

3569
/**
@@ -38,27 +72,10 @@ Phaser.Sprite = function (game, x, y, key, frame) {
3872
*/
3973
this.animations = new Phaser.AnimationManager(this);
4074

41-
if (this.game.cache.isSpriteSheet(key))
42-
{
43-
this.animations.loadFrameData(this.game.cache.getFrameData(key));
44-
45-
if (frame !== null)
46-
{
47-
if (typeof frame === 'string')
48-
{
49-
this.frameName = frame;
50-
}
51-
else
52-
{
53-
this.frame = frame;
54-
}
55-
}
56-
}
57-
else
58-
{
59-
this.currentFrame = this.game.cache.getFrame(key);
60-
}
61-
75+
/**
76+
* The Input Handler Component
77+
* @type InputHandler
78+
*/
6279
this.input = new Phaser.InputHandler(this);
6380

6481
/**

0 commit comments

Comments
 (0)