/****
* Classes
****/
var DragButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
self.isPressed = false;
self.down = function (x, y, obj) {
self.isPressed = true;
};
self.up = function (x, y, obj) {
self.isPressed = false;
if (currentDraggedTower) {
currentDraggedTower = null;
}
};
return self;
});
var TestTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('testTower', {
anchorX: 0.5,
anchorY: 0.5
});
self.isDragging = false;
self.down = function (x, y, obj) {
self.isDragging = true;
};
self.up = function (x, y, obj) {
self.isDragging = false;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var towers = [];
var currentDraggedTower = null;
var dragButton = null;
// Create the drag button at the bottom of the screen
dragButton = game.addChild(new DragButton());
dragButton.x = 2048 / 2;
dragButton.y = 2732 - 100;
// Add button label
var buttonText = new Text2('Hold to Drag Tower', {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = dragButton.x;
buttonText.y = dragButton.y;
game.addChild(buttonText);
function handleMove(x, y, obj) {
if (dragButton.isPressed && currentDraggedTower) {
currentDraggedTower.x = x;
currentDraggedTower.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
if (dragButton.isPressed && !currentDraggedTower) {
// Create a new tower clone to drag
var newTower = new TestTower();
newTower.x = x;
newTower.y = y;
towers.push(newTower);
game.addChild(newTower);
currentDraggedTower = newTower;
}
};
game.up = function (x, y, obj) {
if (currentDraggedTower && !dragButton.isPressed) {
currentDraggedTower = null;
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,86 @@
-/****
+/****
+* Classes
+****/
+var DragButton = Container.expand(function () {
+ var self = Container.call(this);
+ var buttonGraphics = self.attachAsset('button', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.isPressed = false;
+ self.down = function (x, y, obj) {
+ self.isPressed = true;
+ };
+ self.up = function (x, y, obj) {
+ self.isPressed = false;
+ if (currentDraggedTower) {
+ currentDraggedTower = null;
+ }
+ };
+ return self;
+});
+var TestTower = Container.expand(function () {
+ var self = Container.call(this);
+ var towerGraphics = self.attachAsset('testTower', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.isDragging = false;
+ self.down = function (x, y, obj) {
+ self.isDragging = true;
+ };
+ self.up = function (x, y, obj) {
+ self.isDragging = false;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+var towers = [];
+var currentDraggedTower = null;
+var dragButton = null;
+// Create the drag button at the bottom of the screen
+dragButton = game.addChild(new DragButton());
+dragButton.x = 2048 / 2;
+dragButton.y = 2732 - 100;
+// Add button label
+var buttonText = new Text2('Hold to Drag Tower', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+buttonText.anchor.set(0.5, 0.5);
+buttonText.x = dragButton.x;
+buttonText.y = dragButton.y;
+game.addChild(buttonText);
+function handleMove(x, y, obj) {
+ if (dragButton.isPressed && currentDraggedTower) {
+ currentDraggedTower.x = x;
+ currentDraggedTower.y = y;
+ }
+}
+game.move = handleMove;
+game.down = function (x, y, obj) {
+ if (dragButton.isPressed && !currentDraggedTower) {
+ // Create a new tower clone to drag
+ var newTower = new TestTower();
+ newTower.x = x;
+ newTower.y = y;
+ towers.push(newTower);
+ game.addChild(newTower);
+ currentDraggedTower = newTower;
+ }
+};
+game.up = function (x, y, obj) {
+ if (currentDraggedTower && !dragButton.isPressed) {
+ currentDraggedTower = null;
+ }
+};
\ No newline at end of file