Code edit (1 edits merged)
Please save this source code
User prompt
Powerpuff Girls Interactive Car Gift
Initial prompt
Toca interactive car (2012). Open the gift to see what’s inside, a new car for the powerpuff girls! Tap on the horn to honk, when the powerpuff girls go near it the music plays too, and tap the engine to make sounds.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Car = Container.expand(function () {
var self = Container.call(this);
var carBody = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
var horn = self.attachAsset('carHorn', {
anchorX: 0.5,
anchorY: 0.5,
x: -200,
y: -80
});
var engine = self.attachAsset('carEngine', {
anchorX: 0.5,
anchorY: 0.5,
x: 200,
y: 0
});
var leftDoor = self.attachAsset('carDoor', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
y: 0
});
var rightDoor = self.attachAsset('carDoor', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: 0
});
self.hornPressed = function () {
LK.getSound('hornHonk').play();
tween(horn, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
onFinish: function onFinish() {
tween(horn, {
scaleX: 1,
scaleY: 1
}, {
duration: 200
});
}
});
};
self.enginePressed = function () {
var sounds = ['engineStart', 'engineIdle'];
var randomSound = sounds[Math.floor(Math.random() * sounds.length)];
LK.getSound(randomSound).play();
tween(engine, {
tint: 0xff4500
}, {
duration: 300,
onFinish: function onFinish() {
tween(engine, {
tint: 0xffffff
}, {
duration: 300
});
}
});
};
self.doorPressed = function () {
LK.getSound('doorOpen').play();
tween(leftDoor, {
x: -150
}, {
duration: 400,
onFinish: function onFinish() {
tween(leftDoor, {
x: -100
}, {
duration: 400
});
}
});
};
self.down = function (x, y, obj) {
var localPos = self.toLocal(obj.position);
// Check if horn was pressed
if (Math.abs(localPos.x - horn.x) < 40 && Math.abs(localPos.y - horn.y) < 40) {
self.hornPressed();
}
// Check if engine was pressed
else if (Math.abs(localPos.x - engine.x) < 75 && Math.abs(localPos.y - engine.y) < 50) {
self.enginePressed();
}
// Check if door was pressed
else if (Math.abs(localPos.x - leftDoor.x) < 50 && Math.abs(localPos.y - leftDoor.y) < 100) {
self.doorPressed();
}
};
return self;
});
var GiftBox = Container.expand(function () {
var self = Container.call(this);
var giftBox = self.attachAsset('giftBox', {
anchorX: 0.5,
anchorY: 0.5
});
var ribbonH = self.attachAsset('giftRibbon', {
anchorX: 0.5,
anchorY: 0.5
});
var ribbonV = self.attachAsset('giftRibbon', {
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / 2
});
self.isUnwrapped = false;
self.unwrap = function () {
if (self.isUnwrapped) return;
self.isUnwrapped = true;
LK.getSound('unwrap').play();
// Create sparkle effect
for (var i = 0; i < 8; i++) {
var sparkle = game.addChild(LK.getAsset('sparkle', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 300,
y: self.y + (Math.random() - 0.5) * 300
}));
tween(sparkle, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1000,
onFinish: function onFinish() {
sparkle.destroy();
}
});
}
// Animate gift box disappearing
tween(self, {
alpha: 0,
scaleX: 0.1,
scaleY: 0.1
}, {
duration: 800,
onFinish: function onFinish() {
self.destroy();
showCar();
}
});
};
self.down = function (x, y, obj) {
self.unwrap();
};
return self;
});
var PowerpuffGirl = Container.expand(function (color) {
var self = Container.call(this);
var girl = self.attachAsset(color, {
anchorX: 0.5,
anchorY: 0.5
});
self.lastNearCar = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var giftBox = null;
var car = null;
var blossom = null;
var bubbles = null;
var buttercup = null;
var draggedGirl = null;
var musicPlaying = false;
// Initialize gift box
giftBox = game.addChild(new GiftBox());
giftBox.x = 1024;
giftBox.y = 1000;
function showCar() {
car = game.addChild(new Car());
car.x = 1024;
car.y = 1000;
car.alpha = 0;
// Animate car appearing
tween(car, {
alpha: 1,
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 1000,
onFinish: function onFinish() {
tween(car, {
scaleX: 1,
scaleY: 1
}, {
duration: 300
});
}
});
// Create Powerpuff Girls
blossom = game.addChild(new PowerpuffGirl('blossom'));
blossom.x = 300;
blossom.y = 1400;
bubbles = game.addChild(new PowerpuffGirl('bubbles'));
bubbles.x = 1024;
bubbles.y = 1400;
buttercup = game.addChild(new PowerpuffGirl('buttercup'));
buttercup.x = 1700;
buttercup.y = 1400;
}
function checkMusicTrigger() {
if (!car || !blossom || !bubbles || !buttercup) return;
var carPos = {
x: car.x,
y: car.y
};
var girls = [blossom, bubbles, buttercup];
var anyGirlNear = false;
for (var i = 0; i < girls.length; i++) {
var girl = girls[i];
var distance = Math.sqrt(Math.pow(girl.x - carPos.x, 2) + Math.pow(girl.y - carPos.y, 2));
if (distance < 400) {
anyGirlNear = true;
break;
}
}
if (anyGirlNear && !musicPlaying) {
LK.playMusic('powerpuffTheme');
musicPlaying = true;
} else if (!anyGirlNear && musicPlaying) {
LK.stopMusic();
musicPlaying = false;
}
}
game.down = function (x, y, obj) {
if (!car) return;
var girls = [blossom, bubbles, buttercup];
for (var i = 0; i < girls.length; i++) {
var girl = girls[i];
if (girl && Math.abs(x - girl.x) < 60 && Math.abs(y - girl.y) < 60) {
draggedGirl = girl;
break;
}
}
};
game.move = function (x, y, obj) {
if (draggedGirl) {
draggedGirl.x = x;
draggedGirl.y = y;
}
};
game.up = function (x, y, obj) {
draggedGirl = null;
};
game.update = function () {
checkMusicTrigger();
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Car = Container.expand(function () {
var self = Container.call(this);
var carBody = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
var horn = self.attachAsset('carHorn', {
anchorX: 0.5,
anchorY: 0.5,
x: -200,
y: -80
});
var engine = self.attachAsset('carEngine', {
anchorX: 0.5,
anchorY: 0.5,
x: 200,
y: 0
});
var leftDoor = self.attachAsset('carDoor', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
y: 0
});
var rightDoor = self.attachAsset('carDoor', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: 0
});
self.hornPressed = function () {
LK.getSound('hornHonk').play();
tween(horn, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
onFinish: function onFinish() {
tween(horn, {
scaleX: 1,
scaleY: 1
}, {
duration: 200
});
}
});
};
self.enginePressed = function () {
var sounds = ['engineStart', 'engineIdle'];
var randomSound = sounds[Math.floor(Math.random() * sounds.length)];
LK.getSound(randomSound).play();
tween(engine, {
tint: 0xff4500
}, {
duration: 300,
onFinish: function onFinish() {
tween(engine, {
tint: 0xffffff
}, {
duration: 300
});
}
});
};
self.doorPressed = function () {
LK.getSound('doorOpen').play();
tween(leftDoor, {
x: -150
}, {
duration: 400,
onFinish: function onFinish() {
tween(leftDoor, {
x: -100
}, {
duration: 400
});
}
});
};
self.down = function (x, y, obj) {
var localPos = self.toLocal(obj.position);
// Check if horn was pressed
if (Math.abs(localPos.x - horn.x) < 40 && Math.abs(localPos.y - horn.y) < 40) {
self.hornPressed();
}
// Check if engine was pressed
else if (Math.abs(localPos.x - engine.x) < 75 && Math.abs(localPos.y - engine.y) < 50) {
self.enginePressed();
}
// Check if door was pressed
else if (Math.abs(localPos.x - leftDoor.x) < 50 && Math.abs(localPos.y - leftDoor.y) < 100) {
self.doorPressed();
}
};
return self;
});
var GiftBox = Container.expand(function () {
var self = Container.call(this);
var giftBox = self.attachAsset('giftBox', {
anchorX: 0.5,
anchorY: 0.5
});
var ribbonH = self.attachAsset('giftRibbon', {
anchorX: 0.5,
anchorY: 0.5
});
var ribbonV = self.attachAsset('giftRibbon', {
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / 2
});
self.isUnwrapped = false;
self.unwrap = function () {
if (self.isUnwrapped) return;
self.isUnwrapped = true;
LK.getSound('unwrap').play();
// Create sparkle effect
for (var i = 0; i < 8; i++) {
var sparkle = game.addChild(LK.getAsset('sparkle', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 300,
y: self.y + (Math.random() - 0.5) * 300
}));
tween(sparkle, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1000,
onFinish: function onFinish() {
sparkle.destroy();
}
});
}
// Animate gift box disappearing
tween(self, {
alpha: 0,
scaleX: 0.1,
scaleY: 0.1
}, {
duration: 800,
onFinish: function onFinish() {
self.destroy();
showCar();
}
});
};
self.down = function (x, y, obj) {
self.unwrap();
};
return self;
});
var PowerpuffGirl = Container.expand(function (color) {
var self = Container.call(this);
var girl = self.attachAsset(color, {
anchorX: 0.5,
anchorY: 0.5
});
self.lastNearCar = false;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var giftBox = null;
var car = null;
var blossom = null;
var bubbles = null;
var buttercup = null;
var draggedGirl = null;
var musicPlaying = false;
// Initialize gift box
giftBox = game.addChild(new GiftBox());
giftBox.x = 1024;
giftBox.y = 1000;
function showCar() {
car = game.addChild(new Car());
car.x = 1024;
car.y = 1000;
car.alpha = 0;
// Animate car appearing
tween(car, {
alpha: 1,
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 1000,
onFinish: function onFinish() {
tween(car, {
scaleX: 1,
scaleY: 1
}, {
duration: 300
});
}
});
// Create Powerpuff Girls
blossom = game.addChild(new PowerpuffGirl('blossom'));
blossom.x = 300;
blossom.y = 1400;
bubbles = game.addChild(new PowerpuffGirl('bubbles'));
bubbles.x = 1024;
bubbles.y = 1400;
buttercup = game.addChild(new PowerpuffGirl('buttercup'));
buttercup.x = 1700;
buttercup.y = 1400;
}
function checkMusicTrigger() {
if (!car || !blossom || !bubbles || !buttercup) return;
var carPos = {
x: car.x,
y: car.y
};
var girls = [blossom, bubbles, buttercup];
var anyGirlNear = false;
for (var i = 0; i < girls.length; i++) {
var girl = girls[i];
var distance = Math.sqrt(Math.pow(girl.x - carPos.x, 2) + Math.pow(girl.y - carPos.y, 2));
if (distance < 400) {
anyGirlNear = true;
break;
}
}
if (anyGirlNear && !musicPlaying) {
LK.playMusic('powerpuffTheme');
musicPlaying = true;
} else if (!anyGirlNear && musicPlaying) {
LK.stopMusic();
musicPlaying = false;
}
}
game.down = function (x, y, obj) {
if (!car) return;
var girls = [blossom, bubbles, buttercup];
for (var i = 0; i < girls.length; i++) {
var girl = girls[i];
if (girl && Math.abs(x - girl.x) < 60 && Math.abs(y - girl.y) < 60) {
draggedGirl = girl;
break;
}
}
};
game.move = function (x, y, obj) {
if (draggedGirl) {
draggedGirl.x = x;
draggedGirl.y = y;
}
};
game.up = function (x, y, obj) {
draggedGirl = null;
};
game.update = function () {
checkMusicTrigger();
};