@@ -211,6 +211,207 @@ I think that's it! Time to clean it all up...
211211
212212
213213
214+ Ok finally... here is the source for my modifications of the "blank tilemap" example which scales layer 1 constantly.
215+
216+
217+
218+
219+ // var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
220+ var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create, update: update, render: render });
221+
222+
223+ function preload() {
224+
225+ game.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
226+
227+ }
228+
229+ var map;
230+ var layer1;
231+ var layer2;
232+ var layer3;
233+
234+ var marker;
235+ var currentTile = 0;
236+ var currentLayer;
237+
238+ var cursors;
239+ var showLayersKey;
240+ var layer1Key;
241+ var layer2Key;
242+ var layer3Key;
243+
244+ var layer1Scale = new Phaser.Point(1, 1);
245+ var sx = 0.03;
246+ var sy = 0.02;
247+
248+
249+ function create() {
250+
251+ game.stage.backgroundColor = '#2d2d2d';
252+
253+ // Creates a blank tilemap
254+ map = game.add.tilemap();
255+
256+ // Add a Tileset image to the map
257+ map.addTilesetImage('ground_1x1');
258+
259+ // Creates a new blank layer and sets the map dimensions.
260+ // In this case the map is 40x30 tiles in size and the tiles are 32x32 pixels in size.
261+ layer1 = map.create('level1', 40, 30, 32, 32);
262+ layer1.scrollFactorX = 0.5;
263+ layer1.scrollFactorY = 0.5;
264+
265+ // Resize the world
266+ layer1.resizeWorld();
267+
268+ layer2 = map.createBlankLayer('level2', 40, 30, 32, 32);
269+ layer2.scrollFactorX = 0.8;
270+ layer2.scrollFactorY = 0.8;
271+
272+ layer3 = map.createBlankLayer('level3', 40, 30, 32, 32);
273+
274+ currentLayer = layer3;
275+
276+ // Create our tile selector at the top of the screen
277+ createTileSelector();
278+
279+ game.input.addMoveCallback(updateMarker, this);
280+
281+ cursors = game.input.keyboard.createCursorKeys();
282+
283+ showLayersKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
284+ layer1Key = game.input.keyboard.addKey(Phaser.Keyboard.ONE);
285+ layer2Key = game.input.keyboard.addKey(Phaser.Keyboard.TWO);
286+ layer3Key = game.input.keyboard.addKey(Phaser.Keyboard.THREE);
287+
288+ showLayersKey.onDown.add(changeLayer, this);
289+ layer1Key.onDown.add(changeLayer, this);
290+ layer2Key.onDown.add(changeLayer, this);
291+ layer3Key.onDown.add(changeLayer, this);
292+
293+ console.log(layer1.index);
294+ console.log(layer2.index);
295+ console.log(layer3.index);
296+
297+ }
298+
299+ function changeLayer(key) {
300+
301+ switch (key.keyCode)
302+ {
303+ case Phaser.Keyboard.SPACEBAR:
304+ layer1.alpha = 1;
305+ layer2.alpha = 1;
306+ layer3.alpha = 1;
307+ break;
308+
309+ case Phaser.Keyboard.ONE:
310+ currentLayer = layer1;
311+ layer1.alpha = 1;
312+ layer2.alpha = 0.2;
313+ layer3.alpha = 0.2;
314+ break;
315+
316+ case Phaser.Keyboard.TWO:
317+ currentLayer = layer2;
318+ layer1.alpha = 0.2;
319+ layer2.alpha = 1;
320+ layer3.alpha = 0.2;
321+ break;
322+
323+ case Phaser.Keyboard.THREE:
324+ currentLayer = layer3;
325+ layer1.alpha = 0.2;
326+ layer2.alpha = 0.2;
327+ layer3.alpha = 1;
328+ break;
329+ }
330+
331+ }
332+
333+ function pickTile(sprite, pointer) {
334+
335+ currentTile = game.math.snapToFloor(pointer.x, 32) / 32;
336+
337+ }
338+
339+ function updateMarker() {
340+
341+ marker.x = currentLayer.getTileX(game.input.activePointer.worldX) * 32;
342+ marker.y = currentLayer.getTileY(game.input.activePointer.worldY) * 32;
343+
344+ if (game.input.mousePointer.isDown)
345+ {
346+ map.putTile(currentTile, currentLayer.getTileX(marker.x), currentLayer.getTileY(marker.y), currentLayer);
347+ // map.fill(currentTile, currentLayer.getTileX(marker.x), currentLayer.getTileY(marker.y), 4, 4, currentLayer);
348+ }
349+
350+ }
351+
352+ function update() {
353+
354+ if (cursors.left.isDown)
355+ {
356+ game.camera.x -= 4;
357+ }
358+ else if (cursors.right.isDown)
359+ {
360+ game.camera.x += 4;
361+ }
362+
363+ if (cursors.up.isDown)
364+ {
365+ game.camera.y -= 4;
366+ }
367+ else if (cursors.down.isDown)
368+ {
369+ game.camera.y += 4;
370+ }
371+
372+ // animate the layer1 scale constantly
373+ layer1Scale.x += sx;
374+ if ( layer1Scale.x < 0.5 || layer1Scale.x > 2.0 ) sx = -sx;
375+ layer1Scale.y += sy;
376+ if ( layer1Scale.y < 0.5 || layer1Scale.y > 2.0 ) sy = -sy;
377+ layer1.scale.set( layer1Scale.x, layer1Scale.y );
378+ layer1.updateTransform();
379+ }
380+
381+ function render() {
382+
383+ game.debug.text('Current Layer: ' + currentLayer.name, 16, 550);
384+ game.debug.text('1-3 Switch Layers. SPACE = Show All. Cursors = Move Camera', 16, 570);
385+
386+ }
387+
388+ function createTileSelector() {
389+
390+ // Our tile selection window
391+ var tileSelector = game.add.group();
392+
393+ var tileSelectorBackground = game.make.graphics();
394+ tileSelectorBackground.beginFill(0x000000, 0.5);
395+ tileSelectorBackground.drawRect(0, 0, 800, 34);
396+ tileSelectorBackground.endFill();
397+
398+ tileSelector.add(tileSelectorBackground);
399+
400+ var tileStrip = tileSelector.create(1, 1, 'ground_1x1');
401+ tileStrip.inputEnabled = true;
402+ tileStrip.events.onInputDown.add(pickTile, this);
403+
404+ tileSelector.fixedToCamera = true;
405+
406+ // Our painting marker
407+ marker = game.add.graphics();
408+ marker.lineStyle(2, 0x000000, 1);
409+ marker.drawRect(0, 0, 32, 32);
410+
411+ }
412+
413+
414+
214415
215416
216417
0 commit comments