Code edit (1 edits merged)
Please save this source code
User prompt
Surprise Ball Toys
Initial prompt
Toca surprise (2004). The powerpuff girls have 1 red ball, 1 teal ball, 1 green ball, 1 purple ball, 1 orange ball, 1 yellow ball, and 1 cyan ball. Tap on the ball to shake it then tap it to open. It will be a toy monkey, or a toy frog, or a toy Violet cat, or a maraca, or a jelly, or a party blower inside. Tap your toy to see what it does, tap on your toy and see what happens
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var SurpriseBall = Container.expand(function (ballType, toyType, position) {
var self = Container.call(this);
self.ballType = ballType;
self.toyType = toyType;
self.state = 'closed'; // 'closed', 'shaken', 'opened'
self.isShaking = false;
var ballGraphics = self.attachAsset(ballType, {
anchorX: 0.5,
anchorY: 0.5
});
var toyGraphics = null;
self.x = position.x;
self.y = position.y;
function createToy() {
toyGraphics = self.attachAsset(self.toyType, {
anchorX: 0.5,
anchorY: 0.5
});
toyGraphics.visible = false;
}
createToy();
function shake() {
if (self.isShaking) return;
self.isShaking = true;
self.state = 'shaken';
LK.getSound('shake').play();
var originalX = self.x;
tween(self, {
x: originalX + 10
}, {
duration: 100,
easing: tween.easeInOut
});
tween(self, {
x: originalX - 10
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
x: originalX + 8
}, {
duration: 80,
easing: tween.easeInOut
});
tween(self, {
x: originalX - 8
}, {
duration: 80,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
x: originalX
}, {
duration: 60,
easing: tween.easeInOut,
onFinish: function onFinish() {
self.isShaking = false;
}
});
}
});
}
});
}
function openBall() {
self.state = 'opened';
LK.getSound('open').play();
ballGraphics.visible = false;
toyGraphics.visible = true;
tween(toyGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
function interactWithToy() {
if (self.state !== 'opened') return;
LK.getSound('toyInteract').play();
if (self.toyType === 'monkey') {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.5
}, {
duration: 300,
easing: tween.bounceOut
});
} else if (self.toyType === 'frog') {
tween(toyGraphics, {
scaleY: 1.3
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleY: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
} else if (self.toyType === 'violetCat') {
tween(toyGraphics, {
y: toyGraphics.y - 30
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
y: toyGraphics.y + 30
}, {
duration: 200,
easing: tween.bounceOut
});
}
});
} else if (self.toyType === 'maraca') {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.25
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(toyGraphics, {
rotation: toyGraphics.rotation - Math.PI * 0.5
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.25
}, {
duration: 100,
easing: tween.easeInOut
});
}
});
}
});
} else if (self.toyType === 'jelly') {
tween(toyGraphics, {
scaleX: 1.4,
scaleY: 0.8
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.elasticOut
});
}
});
} else if (self.toyType === 'partyBlower') {
tween(toyGraphics, {
scaleX: 1.5
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
}
self.down = function (x, y, obj) {
if (self.state === 'closed') {
shake();
} else if (self.state === 'shaken') {
openBall();
} else if (self.state === 'opened') {
interactWithToy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf0f8ff
});
/****
* Game Code
****/
var balls = [];
var ballTypes = ['redBall', 'tealBall', 'greenBall', 'purpleBall', 'orangeBall', 'yellowBall', 'cyanBall'];
var toyTypes = ['monkey', 'frog', 'violetCat', 'maraca', 'jelly', 'partyBlower'];
var positions = [{
x: 350,
y: 600
}, {
x: 700,
y: 600
}, {
x: 1050,
y: 600
}, {
x: 1400,
y: 600
}, {
x: 525,
y: 900
}, {
x: 875,
y: 900
}, {
x: 1225,
y: 900
}];
function getRandomToy() {
return toyTypes[Math.floor(Math.random() * toyTypes.length)];
}
for (var i = 0; i < ballTypes.length; i++) {
var ball = new SurpriseBall(ballTypes[i], getRandomToy(), positions[i]);
balls.push(ball);
game.addChild(ball);
}
var titleText = new Text2('Surprise Ball Toys', {
size: 100,
fill: 0x333333
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 300;
game.addChild(titleText);
var instructionText = new Text2('Tap a ball to shake it, then tap again to open!', {
size: 60,
fill: 0x666666
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 450;
game.addChild(instructionText); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var SurpriseBall = Container.expand(function (ballType, toyType, position) {
var self = Container.call(this);
self.ballType = ballType;
self.toyType = toyType;
self.state = 'closed'; // 'closed', 'shaken', 'opened'
self.isShaking = false;
var ballGraphics = self.attachAsset(ballType, {
anchorX: 0.5,
anchorY: 0.5
});
var toyGraphics = null;
self.x = position.x;
self.y = position.y;
function createToy() {
toyGraphics = self.attachAsset(self.toyType, {
anchorX: 0.5,
anchorY: 0.5
});
toyGraphics.visible = false;
}
createToy();
function shake() {
if (self.isShaking) return;
self.isShaking = true;
self.state = 'shaken';
LK.getSound('shake').play();
var originalX = self.x;
tween(self, {
x: originalX + 10
}, {
duration: 100,
easing: tween.easeInOut
});
tween(self, {
x: originalX - 10
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
x: originalX + 8
}, {
duration: 80,
easing: tween.easeInOut
});
tween(self, {
x: originalX - 8
}, {
duration: 80,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
x: originalX
}, {
duration: 60,
easing: tween.easeInOut,
onFinish: function onFinish() {
self.isShaking = false;
}
});
}
});
}
});
}
function openBall() {
self.state = 'opened';
LK.getSound('open').play();
ballGraphics.visible = false;
toyGraphics.visible = true;
tween(toyGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
function interactWithToy() {
if (self.state !== 'opened') return;
LK.getSound('toyInteract').play();
if (self.toyType === 'monkey') {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.5
}, {
duration: 300,
easing: tween.bounceOut
});
} else if (self.toyType === 'frog') {
tween(toyGraphics, {
scaleY: 1.3
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleY: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
} else if (self.toyType === 'violetCat') {
tween(toyGraphics, {
y: toyGraphics.y - 30
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
y: toyGraphics.y + 30
}, {
duration: 200,
easing: tween.bounceOut
});
}
});
} else if (self.toyType === 'maraca') {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.25
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(toyGraphics, {
rotation: toyGraphics.rotation - Math.PI * 0.5
}, {
duration: 100,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(toyGraphics, {
rotation: toyGraphics.rotation + Math.PI * 0.25
}, {
duration: 100,
easing: tween.easeInOut
});
}
});
}
});
} else if (self.toyType === 'jelly') {
tween(toyGraphics, {
scaleX: 1.4,
scaleY: 0.8
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.elasticOut
});
}
});
} else if (self.toyType === 'partyBlower') {
tween(toyGraphics, {
scaleX: 1.5
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(toyGraphics, {
scaleX: 1
}, {
duration: 150,
easing: tween.easeIn
});
}
});
}
}
self.down = function (x, y, obj) {
if (self.state === 'closed') {
shake();
} else if (self.state === 'shaken') {
openBall();
} else if (self.state === 'opened') {
interactWithToy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf0f8ff
});
/****
* Game Code
****/
var balls = [];
var ballTypes = ['redBall', 'tealBall', 'greenBall', 'purpleBall', 'orangeBall', 'yellowBall', 'cyanBall'];
var toyTypes = ['monkey', 'frog', 'violetCat', 'maraca', 'jelly', 'partyBlower'];
var positions = [{
x: 350,
y: 600
}, {
x: 700,
y: 600
}, {
x: 1050,
y: 600
}, {
x: 1400,
y: 600
}, {
x: 525,
y: 900
}, {
x: 875,
y: 900
}, {
x: 1225,
y: 900
}];
function getRandomToy() {
return toyTypes[Math.floor(Math.random() * toyTypes.length)];
}
for (var i = 0; i < ballTypes.length; i++) {
var ball = new SurpriseBall(ballTypes[i], getRandomToy(), positions[i]);
balls.push(ball);
game.addChild(ball);
}
var titleText = new Text2('Surprise Ball Toys', {
size: 100,
fill: 0x333333
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 300;
game.addChild(titleText);
var instructionText = new Text2('Tap a ball to shake it, then tap again to open!', {
size: 60,
fill: 0x666666
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 450;
game.addChild(instructionText);