Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Created October 13, 2014 10:26
Show Gist options
  • Save mandarinx/51847880dfd22e023534 to your computer and use it in GitHub Desktop.
Save mandarinx/51847880dfd22e023534 to your computer and use it in GitHub Desktop.
Phaser 2.1.0 removeTile()
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
var map;
var tileset;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var bg;
var marker;
var erase = false;
var curIndex = {x:0, y:0};
var currentTile = "You haven't removed a tile yet";
function preload() {
game.load.tilemap('level1', 'assets/games/starstruck/level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
game.load.spritesheet('droid', 'assets/games/starstruck/droid.png', 32, 32);
game.load.image('tiles-1', 'assets/games/starstruck/tiles-1.png');
game.load.image('starSmall', 'assets/games/starstruck/star.png');
game.load.image('starBig', 'assets/games/starstruck/star2.png');
game.load.image('background', 'assets/games/starstruck/background2.png');
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#000000';
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
map = game.add.tilemap('level1');
map.addTilesetImage('tiles-1');
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
layer = map.createLayer('Tile Layer 1');
layer.debug = true;
layer.resizeWorld();
game.physics.arcade.gravity.y = 250;
player = game.add.sprite(32, 32, 'dude');
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.bounce.y = 0.2;
player.body.collideWorldBounds = true;
player.body.setSize(20, 32, 5, 16);
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('turn', [4], 20, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
marker = game.add.graphics();
marker.lineStyle(2, 0x000000, 1);
marker.drawRect(0, 0, map.tileWidth, map.tileHeight);
}
function update() {
game.physics.arcade.collide(player, layer);
player.body.velocity.x = 0;
if (cursors.left.isDown) {
player.body.velocity.x = -150;
if (facing != 'left') {
player.animations.play('left');
facing = 'left';
}
} else if (cursors.right.isDown) {
player.body.velocity.x = 150;
if (facing != 'right') {
player.animations.play('right');
facing = 'right';
}
} else {
if (facing != 'idle') {
player.animations.stop();
if (facing == 'left') {
player.frame = 0;
} else {
player.frame = 5;
}
facing = 'idle';
}
}
if (jumpButton.isDown &&
player.body.onFloor() &&
game.time.now > jumpTimer) {
player.body.velocity.y = -250;
jumpTimer = game.time.now + 750;
}
if (erase) {
marker.clear();
marker.lineStyle(2, 0xff0000, 1);
marker.drawRect(0, 0, map.tileWidth, map.tileHeight);
} else {
marker.clear();
marker.lineStyle(2, 0x000000, 1);
marker.drawRect(0, 0, map.tileWidth, map.tileHeight);
}
curIndex.x = layer.getTileX(game.input.activePointer.worldX);
curIndex.y = layer.getTileY(game.input.activePointer.worldY);
marker.x = curIndex.x * map.tileWidth;
marker.y = curIndex.y * map.tileHeight;
if (game.input.keyboard.isDown(Phaser.Keyboard.SHIFT)) {
erase = true;
if (game.input.mousePointer.isDown) {
map.removeTile(curIndex.x, curIndex.y, layer);
currentTile = map.getTile(layer.getTileX(marker.x),
layer.getTileY(marker.y));
}
} else {
erase = false;
}
}
function render () {
game.debug.text('Hold Shift while clicking with the left '+
'mouse button to remove a tile', 32, 32);
game.debug.text('The removed tile: '+currentTile, 32, 64);
}
@mandarinx
Copy link
Author

This test confirms that removing a tile does not cause an error, like reported in Phaser issue #862.

To try this out, clone phaser-examples and replace the contents of ./examples/games/starstruck.js with this Gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment