User prompt
Make the cars realistic ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'var localPos = game.toLocal(obj.parent.toGlobal({' Line Number: 124
Code edit (1 edits merged)
Please save this source code
User prompt
Police Pursuit: Crash & Catch
Initial prompt
A police driving game where you crash into robbers
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var PoliceCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('policeCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.maxSpeed = 15;
self.acceleration = 0.8;
self.friction = 0.92;
self.vx = 0;
self.vy = 0;
self.update = function () {
self.vx *= self.friction;
self.vy *= self.friction;
self.x += self.vx;
self.y += self.vy;
if (self.x < 60) self.x = 60;
if (self.x > 2048 - 60) self.x = 2048 - 60;
if (self.y < 60) self.y = 60;
if (self.y > 2732 - 60) self.y = 2732 - 60;
};
return self;
});
var RobberCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('robberCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 0;
self.vy = 0;
self.speed = 4 + Math.random() * 3;
self.directionChangeTimer = 0;
self.directionChangeInterval = 60 + Math.floor(Math.random() * 120);
self.lastX = self.x;
self.lastY = self.y;
self.lastIntersecting = false;
self.update = function () {
self.directionChangeTimer--;
if (self.directionChangeTimer <= 0) {
var angle = Math.random() * Math.PI * 2;
self.vx = Math.cos(angle) * self.speed;
self.vy = Math.sin(angle) * self.speed;
self.directionChangeTimer = self.directionChangeInterval;
self.directionChangeInterval = 60 + Math.floor(Math.random() * 120);
}
self.lastX = self.x;
self.lastY = self.y;
self.x += self.vx;
self.y += self.vy;
if (self.x < 50) self.x = 50;
if (self.x > 2048 - 50) self.x = 2048 - 50;
if (self.y < 50) self.y = 50;
if (self.y > 2732 - 50) self.y = 2732 - 50;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var policeCar = game.addChild(new PoliceCar());
policeCar.x = 1024;
policeCar.y = 2400;
var robberCars = [];
var score = 0;
var spawnTimer = 0;
var spawnInterval = 120;
var difficultyMultiplier = 1;
var targetScore = 50;
var dragActive = false;
var dragOffsetX = 0;
var dragOffsetY = 0;
var scoreTxt = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
dragActive = true;
var localPos = game.toLocal(obj.parent.toGlobal({
x: x,
y: y
}));
dragOffsetX = policeCar.x - localPos.x;
dragOffsetY = policeCar.y - localPos.y;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragActive = false;
};
function handleMove(x, y, obj) {
if (dragActive) {
var localPos = game.toLocal(obj.parent.toGlobal({
x: x,
y: y
}));
var targetX = localPos.x + dragOffsetX;
var targetY = localPos.y + dragOffsetY;
var dx = targetX - policeCar.x;
var dy = targetY - policeCar.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
var maxAccel = policeCar.maxSpeed;
policeCar.vx = dx / distance * maxAccel;
policeCar.vy = dy / distance * maxAccel;
}
}
}
game.move = handleMove;
game.update = function () {
policeCar.update();
spawnTimer--;
if (spawnTimer <= 0) {
var newRobber = game.addChild(new RobberCar());
var side = Math.floor(Math.random() * 4);
if (side === 0) {
newRobber.x = Math.random() * 2048;
newRobber.y = -50;
} else if (side === 1) {
newRobber.x = 2048 + 50;
newRobber.y = Math.random() * 2732;
} else if (side === 2) {
newRobber.x = Math.random() * 2048;
newRobber.y = 2732 + 50;
} else {
newRobber.x = -50;
newRobber.y = Math.random() * 2732;
}
robberCars.push(newRobber);
spawnTimer = Math.max(60, spawnInterval - Math.floor(difficultyMultiplier * 10));
}
for (var a = robberCars.length - 1; a >= 0; a--) {
var robber = robberCars[a];
robber.update();
var offScreen = robber.x < -100 || robber.x > 2148 || robber.y < -100 || robber.y > 2832;
if (offScreen) {
robber.destroy();
robberCars.splice(a, 1);
continue;
}
var currentIntersecting = policeCar.intersects(robber);
if (!robber.lastIntersecting && currentIntersecting) {
score += 10;
difficultyMultiplier += 0.1;
scoreTxt.setText('Score: ' + score);
LK.getSound('apprehend').play();
LK.effects.flashScreen(0x00ff00, 300);
robber.destroy();
robberCars.splice(a, 1);
if (score >= targetScore) {
LK.showYouWin();
}
continue;
}
robber.lastIntersecting = currentIntersecting;
}
};
LK.playMusic('bgmusic'); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,177 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var PoliceCar = Container.expand(function () {
+ var self = Container.call(this);
+ var carGraphics = self.attachAsset('policeCar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 0;
+ self.maxSpeed = 15;
+ self.acceleration = 0.8;
+ self.friction = 0.92;
+ self.vx = 0;
+ self.vy = 0;
+ self.update = function () {
+ self.vx *= self.friction;
+ self.vy *= self.friction;
+ self.x += self.vx;
+ self.y += self.vy;
+ if (self.x < 60) self.x = 60;
+ if (self.x > 2048 - 60) self.x = 2048 - 60;
+ if (self.y < 60) self.y = 60;
+ if (self.y > 2732 - 60) self.y = 2732 - 60;
+ };
+ return self;
+});
+var RobberCar = Container.expand(function () {
+ var self = Container.call(this);
+ var carGraphics = self.attachAsset('robberCar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.vx = 0;
+ self.vy = 0;
+ self.speed = 4 + Math.random() * 3;
+ self.directionChangeTimer = 0;
+ self.directionChangeInterval = 60 + Math.floor(Math.random() * 120);
+ self.lastX = self.x;
+ self.lastY = self.y;
+ self.lastIntersecting = false;
+ self.update = function () {
+ self.directionChangeTimer--;
+ if (self.directionChangeTimer <= 0) {
+ var angle = Math.random() * Math.PI * 2;
+ self.vx = Math.cos(angle) * self.speed;
+ self.vy = Math.sin(angle) * self.speed;
+ self.directionChangeTimer = self.directionChangeInterval;
+ self.directionChangeInterval = 60 + Math.floor(Math.random() * 120);
+ }
+ self.lastX = self.x;
+ self.lastY = self.y;
+ self.x += self.vx;
+ self.y += self.vy;
+ if (self.x < 50) self.x = 50;
+ if (self.x > 2048 - 50) self.x = 2048 - 50;
+ if (self.y < 50) self.y = 50;
+ if (self.y > 2732 - 50) self.y = 2732 - 50;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a1a2e
+});
+
+/****
+* Game Code
+****/
+var policeCar = game.addChild(new PoliceCar());
+policeCar.x = 1024;
+policeCar.y = 2400;
+var robberCars = [];
+var score = 0;
+var spawnTimer = 0;
+var spawnInterval = 120;
+var difficultyMultiplier = 1;
+var targetScore = 50;
+var dragActive = false;
+var dragOffsetX = 0;
+var dragOffsetY = 0;
+var scoreTxt = new Text2('Score: 0', {
+ size: 120,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+game.down = function (x, y, obj) {
+ dragActive = true;
+ var localPos = game.toLocal(obj.parent.toGlobal({
+ x: x,
+ y: y
+ }));
+ dragOffsetX = policeCar.x - localPos.x;
+ dragOffsetY = policeCar.y - localPos.y;
+ handleMove(x, y, obj);
+};
+game.up = function (x, y, obj) {
+ dragActive = false;
+};
+function handleMove(x, y, obj) {
+ if (dragActive) {
+ var localPos = game.toLocal(obj.parent.toGlobal({
+ x: x,
+ y: y
+ }));
+ var targetX = localPos.x + dragOffsetX;
+ var targetY = localPos.y + dragOffsetY;
+ var dx = targetX - policeCar.x;
+ var dy = targetY - policeCar.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 0) {
+ var maxAccel = policeCar.maxSpeed;
+ policeCar.vx = dx / distance * maxAccel;
+ policeCar.vy = dy / distance * maxAccel;
+ }
+ }
+}
+game.move = handleMove;
+game.update = function () {
+ policeCar.update();
+ spawnTimer--;
+ if (spawnTimer <= 0) {
+ var newRobber = game.addChild(new RobberCar());
+ var side = Math.floor(Math.random() * 4);
+ if (side === 0) {
+ newRobber.x = Math.random() * 2048;
+ newRobber.y = -50;
+ } else if (side === 1) {
+ newRobber.x = 2048 + 50;
+ newRobber.y = Math.random() * 2732;
+ } else if (side === 2) {
+ newRobber.x = Math.random() * 2048;
+ newRobber.y = 2732 + 50;
+ } else {
+ newRobber.x = -50;
+ newRobber.y = Math.random() * 2732;
+ }
+ robberCars.push(newRobber);
+ spawnTimer = Math.max(60, spawnInterval - Math.floor(difficultyMultiplier * 10));
+ }
+ for (var a = robberCars.length - 1; a >= 0; a--) {
+ var robber = robberCars[a];
+ robber.update();
+ var offScreen = robber.x < -100 || robber.x > 2148 || robber.y < -100 || robber.y > 2832;
+ if (offScreen) {
+ robber.destroy();
+ robberCars.splice(a, 1);
+ continue;
+ }
+ var currentIntersecting = policeCar.intersects(robber);
+ if (!robber.lastIntersecting && currentIntersecting) {
+ score += 10;
+ difficultyMultiplier += 0.1;
+ scoreTxt.setText('Score: ' + score);
+ LK.getSound('apprehend').play();
+ LK.effects.flashScreen(0x00ff00, 300);
+ robber.destroy();
+ robberCars.splice(a, 1);
+ if (score >= targetScore) {
+ LK.showYouWin();
+ }
+ continue;
+ }
+ robber.lastIntersecting = currentIntersecting;
+ }
+};
+LK.playMusic('bgmusic');
\ No newline at end of file