mirror of
https://github.com/wassname/phaser.git
synced 2026-08-14 12:40:17 +08:00
StateManager fixes for when you change state in the create function.
TilemapLayer update for WebGL.
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
// mods by Patrick OReilly
|
||||
// Twitter: @pato_reilly Web: http://patricko.byethost9.com
|
||||
// twitter: @pato_reilly
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.tilemap('matching', 'assets/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
|
||||
game.load.image('tiles', 'assets/tiles/phaser_tiles.png');
|
||||
|
||||
game.load.tilemap('matching', 'assets/tilemaps/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
|
||||
game.load.tileset('tiles', 'assets/tilemaps/tiles/phaser_tiles.png', 100, 100, -1, 1, 1);
|
||||
}
|
||||
|
||||
var timeCheck = 0;
|
||||
@@ -37,36 +36,39 @@ var tileBack = 25;
|
||||
var timesUp = '+';
|
||||
var youWin = '+';
|
||||
|
||||
|
||||
function create() {
|
||||
|
||||
map = game.add.tilemap('matching');
|
||||
map = game.add.tilemap('matching');
|
||||
|
||||
// tileset = game.add.tileset('tiles');
|
||||
tileset = game.add.tileset('tiles');
|
||||
|
||||
layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
|
||||
|
||||
// layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
|
||||
|
||||
marker = game.add.graphics();
|
||||
marker.lineStyle(2, 0x00FF00, 1);
|
||||
marker.drawRect(0, 0, 100, 100);
|
||||
//layer.resizeWorld();
|
||||
|
||||
marker = game.add.graphics();
|
||||
marker.lineStyle(2, 0x00FF00, 1);
|
||||
marker.drawRect(0, 0, 100, 100);
|
||||
|
||||
randomizeTiles();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
|
||||
countDownTimer();
|
||||
|
||||
|
||||
if (layer.getTileX(game.input.activePointer.worldX) <= 5) // to prevent the marker from going out of bounds
|
||||
{
|
||||
marker.x = layer.getTileX(game.input.activePointer.worldX) * 100;
|
||||
marker.y = layer.getTileY(game.input.activePointer.worldY) * 100;
|
||||
}
|
||||
|
||||
if (flipFlag == true)
|
||||
if (flipFlag == true)
|
||||
{
|
||||
if (game.time.now - timeCheck > 1000)
|
||||
{
|
||||
if (game.time.now - timeCheck >1000)
|
||||
{
|
||||
flipBack();
|
||||
}
|
||||
}
|
||||
@@ -74,97 +76,93 @@ function update() {
|
||||
{
|
||||
processClick();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function countDownTimer() {
|
||||
|
||||
|
||||
var timeLimit = 120;
|
||||
|
||||
|
||||
myTime = game.time.now;
|
||||
mySeconds = parseInt(myTime / 1000);
|
||||
mySeconds = parseInt(myTime/1000);
|
||||
myCountdownSeconds = timeLimit - mySeconds;
|
||||
|
||||
if (myCountdownSeconds <= 0)
|
||||
{
|
||||
|
||||
if (myCountdownSeconds <= 0)
|
||||
{
|
||||
// time is up
|
||||
timesUp = 'Time is up!';
|
||||
timesUp = 'Time is up!';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function processClick() {
|
||||
|
||||
|
||||
currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y));
|
||||
currentTilePosition = ((layer.getTileY(game.input.activePointer.worldY) + 1) * 6) - (6 - (layer.getTileX(game.input.activePointer.worldX) + 1));
|
||||
|
||||
currentTilePosition = ((layer.getTileY(game.input.activePointer.worldY)+1)*6)-(6-(layer.getTileX(game.input.activePointer.worldX)+1));
|
||||
|
||||
if (game.input.mousePointer.isDown)
|
||||
{
|
||||
{
|
||||
// check to make sure the tile is not already flipped
|
||||
if (currentTile == tileBack)
|
||||
if (currentTile == tileBack)
|
||||
{
|
||||
// get the corresponding item out of squareList
|
||||
currentNum = squareList[currentTilePosition - 1];
|
||||
currentNum = squareList[currentTilePosition-1];
|
||||
flipOver();
|
||||
squareCounter++;
|
||||
|
||||
squareCounter++;
|
||||
// is the second tile of pair flipped?
|
||||
if (squareCounter == 2)
|
||||
if (squareCounter == 2)
|
||||
{
|
||||
// reset squareCounter
|
||||
squareCounter = 0;
|
||||
square2Num = currentNum;
|
||||
|
||||
// check for match
|
||||
if (square1Num == square2Num)
|
||||
{
|
||||
masterCounter++;
|
||||
|
||||
if (masterCounter == 18)
|
||||
masterCounter++;
|
||||
|
||||
if (masterCounter == 18)
|
||||
{
|
||||
// go "win"
|
||||
youWin = 'Got them all!';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
savedSquareX2 = layer.getTileX(marker.x);
|
||||
savedSquareY2 = layer.getTileY(marker.y);
|
||||
flipFlag = true;
|
||||
timeCheck = game.time.now;
|
||||
}
|
||||
}
|
||||
flipFlag = true;
|
||||
timeCheck = game.time.now;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
savedSquareX1 = layer.getTileX(marker.x);
|
||||
savedSquareY1 = layer.getTileY(marker.y);
|
||||
square1Num = currentNum;
|
||||
}
|
||||
}
|
||||
}
|
||||
square1Num = currentNum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function flipOver() {
|
||||
|
||||
|
||||
map.putTile(currentNum, layer.getTileX(marker.x), layer.getTileY(marker.y));
|
||||
}
|
||||
|
||||
|
||||
function flipBack() {
|
||||
|
||||
|
||||
flipFlag = false;
|
||||
|
||||
|
||||
map.putTile(tileBack, savedSquareX1, savedSquareY1);
|
||||
map.putTile(tileBack, savedSquareX2, savedSquareY2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function randomizeTiles() {
|
||||
|
||||
for (num = 1; num <= 18; num++)
|
||||
{
|
||||
startList.push(num);
|
||||
}
|
||||
|
||||
for (num = 1; num <= 18; num++)
|
||||
{
|
||||
startList.push(num);
|
||||
@@ -172,24 +170,24 @@ function randomizeTiles() {
|
||||
|
||||
// for debugging
|
||||
myString1 = startList.toString();
|
||||
|
||||
|
||||
// randomize squareList
|
||||
for (i = 1; i <= 36; i++)
|
||||
for (i = 1; i <=36; i++)
|
||||
{
|
||||
randomPosition = game.rnd.integerInRange(0, startList.length);
|
||||
|
||||
thisNumber = startList[randomPosition];
|
||||
|
||||
randomPosition = game.rnd.integerInRange(0,startList.length);
|
||||
|
||||
thisNumber = startList[ randomPosition ];
|
||||
|
||||
squareList.push(thisNumber);
|
||||
|
||||
|
||||
a = startList.indexOf(thisNumber);
|
||||
|
||||
startList.splice(a, 1);
|
||||
|
||||
startList.splice( a, 1);
|
||||
}
|
||||
|
||||
|
||||
// for debugging
|
||||
myString2 = squareList.toString();
|
||||
|
||||
|
||||
for (col = 0; col < 6; col++)
|
||||
{
|
||||
for (row = 0; row < 6; row++)
|
||||
@@ -200,8 +198,8 @@ function randomizeTiles() {
|
||||
}
|
||||
|
||||
function getHiddenTile() {
|
||||
|
||||
thisTile = squareList[currentTilePosition - 1];
|
||||
|
||||
thisTile = squareList[currentTilePosition-1];
|
||||
return thisTile;
|
||||
}
|
||||
|
||||
@@ -211,7 +209,13 @@ function render() {
|
||||
game.debug.renderText(youWin, 620, 240, 'rgb(0,255,0)');
|
||||
|
||||
game.debug.renderText('Time: ' + myCountdownSeconds, 620, 15, 'rgb(0,255,0)');
|
||||
|
||||
//game.debug.renderText('squareCounter: ' + squareCounter, 620, 272, 'rgb(0,0,255)');
|
||||
game.debug.renderText('Matched Pairs: ' + masterCounter, 620, 304, 'rgb(0,0,255)');
|
||||
|
||||
//game.debug.renderText('startList: ' + myString1, 620, 208, 'rgb(255,0,0)');
|
||||
//game.debug.renderText('squareList: ' + myString2, 620, 240, 'rgb(255,0,0)');
|
||||
|
||||
game.debug.renderText('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 620, 48, 'rgb(255,0,0)');
|
||||
|
||||
game.debug.renderText('LayerX: ' + layer.getTileX(marker.x), 620, 80, 'rgb(255,0,0)');
|
||||
@@ -219,5 +223,4 @@ function render() {
|
||||
|
||||
game.debug.renderText('Tile Position: ' + currentTilePosition, 620, 144, 'rgb(255,0,0)');
|
||||
game.debug.renderText('Hidden Tile: ' + getHiddenTile(), 620, 176, 'rgb(255,0,0)');
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user