User prompt
There will be 3 available shapes: A Circle, A square and A triangle. By every click the player changes the shape to one of those 3.
User prompt
Fix Bug: 'TypeError: shapes[a].containsPoint is not a function' in this line: 'if (shapes[a] && shapes[a].containsPoint(pos)) {' Line Number: 51
User prompt
Fix Bug: 'TypeError: shapes[a] is undefined' in this line: 'if (shapes[a].containsPoint(pos)) {' Line Number: 51
User prompt
Fix Bug: 'TypeError: shapes[a] is undefined' in this line: 'if (shapes[a].matched) {' Line Number: 32
Initial prompt
Shape Match FRVR
var Circle = Container.expand(function () {
var self = Container.call(this);
var circleGraphics = self.createAsset('circle', 'Circle Graphics', .5, .5);
self.matched = false;
self.match = function () {
self.matched = true;
};
});
var Square = Container.expand(function () {
var self = Container.call(this);
var squareGraphics = self.createAsset('square', 'Square Graphics', .5, .5);
self.matched = false;
self.match = function () {
self.matched = true;
};
});
var Triangle = Container.expand(function () {
var self = Container.call(this);
var triangleGraphics = self.createAsset('triangle', 'Triangle Graphics', .5, .5);
self.matched = false;
self.match = function () {
self.matched = true;
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.createAsset('player', 'Player Graphics', .5, .5);
self.score = 0;
self.shape = 'circle';
self.incrementScore = function () {
self.score += 1;
};
self.changeShape = function () {
if (self.shape === 'circle') {
self.shape = 'square';
} else if (self.shape === 'square') {
self.shape = 'triangle';
} else {
self.shape = 'circle';
}
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var shapes = [];
var player = self.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.setText(player.score);
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
LK.on('tick', function () {
for (var a = shapes.length - 1; a >= 0; a--) {
if (shapes[a] && shapes[a].matched) {
shapes[a].destroy();
shapes.splice(a, 1);
player.incrementScore();
scoreTxt.setText(player.score);
}
}
if (LK.ticks % 60 == 0) {
var newShape = new Shape();
newShape.x = Math.random() * 2048;
newShape.y = Math.random() * 2732;
shapes.push(newShape);
self.addChild(newShape);
}
});
stage.on('down', function (obj) {
player.changeShape();
var event = obj.event;
var pos = event.getLocalPosition(self);
for (var a = shapes.length - 1; a >= 0; a--) {
if (shapes[a] && shapes[a].containsPoint({
x: pos.x,
y: pos.y
}) && shapes[a] instanceof window[player.shape]) {
shapes[a].match();
}
}
});
});
===================================================================
--- original.js
+++ change.js
@@ -1,19 +1,45 @@
-var Shape = Container.expand(function () {
+var Circle = Container.expand(function () {
var self = Container.call(this);
- var shapeGraphics = self.createAsset('shape', 'Shape Graphics', .5, .5);
+ var circleGraphics = self.createAsset('circle', 'Circle Graphics', .5, .5);
self.matched = false;
self.match = function () {
self.matched = true;
};
});
+var Square = Container.expand(function () {
+ var self = Container.call(this);
+ var squareGraphics = self.createAsset('square', 'Square Graphics', .5, .5);
+ self.matched = false;
+ self.match = function () {
+ self.matched = true;
+ };
+});
+var Triangle = Container.expand(function () {
+ var self = Container.call(this);
+ var triangleGraphics = self.createAsset('triangle', 'Triangle Graphics', .5, .5);
+ self.matched = false;
+ self.match = function () {
+ self.matched = true;
+ };
+});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.createAsset('player', 'Player Graphics', .5, .5);
self.score = 0;
+ self.shape = 'circle';
self.incrementScore = function () {
self.score += 1;
};
+ self.changeShape = function () {
+ if (self.shape === 'circle') {
+ self.shape = 'square';
+ } else if (self.shape === 'square') {
+ self.shape = 'triangle';
+ } else {
+ self.shape = 'circle';
+ }
+ };
});
var Game = Container.expand(function () {
var self = Container.call(this);
var shapes = [];
@@ -44,15 +70,16 @@
self.addChild(newShape);
}
});
stage.on('down', function (obj) {
+ player.changeShape();
var event = obj.event;
var pos = event.getLocalPosition(self);
for (var a = shapes.length - 1; a >= 0; a--) {
if (shapes[a] && shapes[a].containsPoint({
x: pos.x,
y: pos.y
- })) {
+ }) && shapes[a] instanceof window[player.shape]) {
shapes[a].match();
}
}
});