User prompt
In the Swap Gems method on board, please make sure that the row and column values on each individual gems are also correctly swapped.
Code edit (1 edits merged)
Please save this source code
User prompt
In gem after generating type where you attach a gem element use five different types of gems
Code edit (6 edits merged)
Please save this source code
User prompt
So, please add a method to the board that allows me to swap two gems.
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Initial prompt
Match 3 Game
/**** * Classes ****/ // Class for the game board var Board = Container.expand(function () { var self = Container.call(this); self.grid = []; self.rows = 8; self.cols = 8; self.spacing = 10; var mesureGem = new Gem(); self.gemSize = mesureGem.width + self.spacing; self.init = function () { for (var row = 0; row < self.rows; row++) { self.grid[row] = []; for (var col = 0; col < self.cols; col++) { var gem = new Gem(); gem.x = gem.targetX = col * self.gemSize; gem.y = gem.targetY = row * self.gemSize; self.grid[row][col] = gem; self.addChild(gem); } } }; }); //<Assets used in the game will automatically appear here> // Class for a single gem var Gem = Container.expand(function () { var self = Container.call(this); var gemGraphics = self.attachAsset('gem', { anchorX: 0.5, anchorY: 0.5 }); self.targetX = 0; self.targetY = 0; self.type = Math.floor(Math.random() * 5); // Random type for the gem self.update = function () { // Update logic for the gem if needed }; self.down = function (x, y, obj) { // Logic for selecting the gem game.selectGem(self, x, y); }; self.update = function () { if (currentDragNode != self) { self.x += (self.targetX - self.x) / 10; self.y += (self.targetY - self.y) / 10; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var selectedGem = null; var board = new Board(); board.x = (2048 - board.cols * board.gemSize + board.gemSize + board.spacing) / 2; board.y = (2732 - board.rows * board.gemSize + board.gemSize + board.spacing) / 2; board.init(); game.addChild(board); var currentDragNode = undefined; var currentDragNodeX = 0; var currentDragNodeY = 0; game.selectGem = function (gem, offsetX, offsetY) { currentDragNode = gem; currentDragNodeX = offsetX; currentDragNodeY = offsetY; game.addChild(currentDragNode); }; game.move = function (x, y) { if (currentDragNode) { currentDragNode.x = x - board.x - currentDragNodeX; currentDragNode.y = y - board.y - currentDragNodeY; } }; game.up = function () { currentDragNode = undefined; };
===================================================================
--- original.js
+++ change.js
@@ -14,30 +14,15 @@
for (var row = 0; row < self.rows; row++) {
self.grid[row] = [];
for (var col = 0; col < self.cols; col++) {
var gem = new Gem();
- gem.x = col * self.gemSize;
- gem.y = row * self.gemSize;
+ gem.x = gem.targetX = col * self.gemSize;
+ gem.y = gem.targetY = row * self.gemSize;
self.grid[row][col] = gem;
self.addChild(gem);
}
}
};
- self.swapGems = function (gem1, gem2) {
- // Logic to swap two gems
- };
- self.checkMatches = function () {
- // Logic to check for matches
- };
- self.removeMatches = function () {
- // Logic to remove matched gems
- };
- self.fillEmptySpaces = function () {
- // Logic to fill empty spaces after removing matches
- };
- self.update = function () {
- // Update logic for the board
- };
});
//<Assets used in the game will automatically appear here>
// Class for a single gem
var Gem = Container.expand(function () {
@@ -45,16 +30,24 @@
var gemGraphics = self.attachAsset('gem', {
anchorX: 0.5,
anchorY: 0.5
});
+ self.targetX = 0;
+ self.targetY = 0;
self.type = Math.floor(Math.random() * 5); // Random type for the gem
self.update = function () {
// Update logic for the gem if needed
};
self.down = function (x, y, obj) {
// Logic for selecting the gem
- game.selectGem(self);
+ game.selectGem(self, x, y);
};
+ self.update = function () {
+ if (currentDragNode != self) {
+ self.x += (self.targetX - self.x) / 10;
+ self.y += (self.targetY - self.y) / 10;
+ }
+ };
});
/****
* Initialize Game
@@ -67,20 +60,26 @@
* Game Code
****/
var selectedGem = null;
var board = new Board();
-board.x = (0 - board.cols * board.gemSize + board.gemSize / 2 + board.spacing / 2) / 2;
-board.y = (0 - board.rows * board.gemSize + board.gemSize / 2 + board.spacing / 2) / 2;
+board.x = (2048 - board.cols * board.gemSize + board.gemSize + board.spacing) / 2;
+board.y = (2732 - board.rows * board.gemSize + board.gemSize + board.spacing) / 2;
board.init();
game.addChild(board);
-game.selectGem = function (gem) {
- if (selectedGem) {
- board.swapGems(selectedGem, gem);
- selectedGem = null;
- } else {
- selectedGem = gem;
+var currentDragNode = undefined;
+var currentDragNodeX = 0;
+var currentDragNodeY = 0;
+game.selectGem = function (gem, offsetX, offsetY) {
+ currentDragNode = gem;
+ currentDragNodeX = offsetX;
+ currentDragNodeY = offsetY;
+ game.addChild(currentDragNode);
+};
+game.move = function (x, y) {
+ if (currentDragNode) {
+ currentDragNode.x = x - board.x - currentDragNodeX;
+ currentDragNode.y = y - board.y - currentDragNodeY;
}
};
-game.update = function () {
- board.update();
- // Additional game update logic
+game.up = function () {
+ currentDragNode = undefined;
};
\ No newline at end of file