Skip to content

Commit 7e8b79a

Browse files
committed
Tilemap painting example and other smaller updates.
1 parent 275fa46 commit 7e8b79a

7 files changed

Lines changed: 255 additions & 25 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Version 1.0.7 (in progress in the dev branch)
124124
* Fixed Rectangle.union (thanks andron77)
125125
* Debug.renderSpriteBody updated to use a the new Sprite.Body.screenX/Y properties.
126126
* Added Text.destroy() and BitmapText.destroy(), also updated Group.remove to make it more bullet-proof when an element doesn't have any events.
127+
* Added Phaser.Utils.shuffle to shuffle an array.
128+
* Added Graphics.destroy, x, y and updated angle functions.
129+
127130

128131

129132

examples/tilemaps/paint tiles.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
$title = "Painting Tiles";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
9+
10+
function preload() {
11+
12+
game.load.tilemap('desert', 'assets/maps/desert.json', null, Phaser.Tilemap.TILED_JSON);
13+
game.load.tileset('tiles', 'assets/tiles/tmw_desert_spacing.png', 32, 32, -1, 1, 1);
14+
15+
}
16+
17+
var map;
18+
var tileset;
19+
var layer;
20+
21+
var marker;
22+
var currentTile = 0;
23+
24+
function create() {
25+
26+
map = game.add.tilemap('desert');
27+
28+
tileset = game.add.tileset('tiles');
29+
30+
layer = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 0);
31+
32+
layer.resizeWorld();
33+
34+
marker = game.add.graphics();
35+
marker.lineStyle(2, 0x000000, 1);
36+
marker.drawRect(0, 0, 32, 32);
37+
38+
}
39+
40+
function update() {
41+
42+
marker.x = layer.getTileX(game.input.activePointer.worldX) * 32;
43+
marker.y = layer.getTileY(game.input.activePointer.worldY) * 32;
44+
45+
if (game.input.mousePointer.isDown)
46+
{
47+
if (game.input.keyboard.isDown(Phaser.Keyboard.SHIFT))
48+
{
49+
currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y));
50+
}
51+
else
52+
{
53+
if (map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile)
54+
{
55+
map.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y))
56+
}
57+
}
58+
}
59+
60+
}
61+
62+
function render() {
63+
64+
game.debug.renderText('Left-click to paint. Shift + Left-click to select tile.', 32, 32, 'rgb(0,0,0)');
65+
game.debug.renderText('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 32, 48, 'rgb(0,0,0)');
66+
67+
}
68+
69+
</script>
70+
71+
<?php
72+
require('../foot.php');
73+
?>

examples/tilemaps/randomise tiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function randomiseTiles() {
5050

5151
// You can also pass in x, y, width, height values to control the area in which the replace happens
5252

53-
map.random(layer.getTileX(sprite.x), layer.getTileY(sprite.y), 6, 6);
53+
map.shuffle(layer.getTileX(sprite.x), layer.getTileY(sprite.y), 6, 6);
5454

5555
}
5656

src/gameobjects/Graphics.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
Phaser.Graphics = function (game, x, y) {
1919

20+
this.game = game;
21+
2022
PIXI.Graphics.call(this);
2123

2224
/**
@@ -28,18 +30,59 @@ Phaser.Graphics = function (game, x, y) {
2830

2931
Phaser.Graphics.prototype = Object.create(PIXI.Graphics.prototype);
3032
Phaser.Graphics.prototype.constructor = Phaser.Graphics;
31-
Phaser.Graphics.prototype = Phaser.Utils.extend(true, Phaser.Graphics.prototype, Phaser.Sprite.prototype);
3233

3334
// Add our own custom methods
3435

36+
/**
37+
* Description.
38+
*
39+
* @method Phaser.Sprite.prototype.destroy
40+
*/
41+
Phaser.Graphics.prototype.destroy = function() {
42+
43+
this.clear();
44+
45+
if (this.group)
46+
{
47+
this.group.remove(this);
48+
}
49+
50+
this.game = null;
51+
52+
}
53+
3554
Object.defineProperty(Phaser.Graphics.prototype, 'angle', {
3655

3756
get: function() {
38-
return Phaser.Math.radToDeg(this.rotation);
57+
return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.rotation));
58+
},
59+
60+
set: function(value) {
61+
this.rotation = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value));
62+
}
63+
64+
});
65+
66+
Object.defineProperty(Phaser.Graphics.prototype, 'x', {
67+
68+
get: function() {
69+
return this.position.x;
70+
},
71+
72+
set: function(value) {
73+
this.position.x = value;
74+
}
75+
76+
});
77+
78+
Object.defineProperty(Phaser.Graphics.prototype, 'y', {
79+
80+
get: function() {
81+
return this.position.y;
3982
},
4083

4184
set: function(value) {
42-
this.rotation = Phaser.Math.degToRad(value);
85+
this.position.y = value;
4386
}
4487

4588
});

0 commit comments

Comments
 (0)