Code edit (1 edits merged)
Please save this source code
User prompt
Nursery Rhyme Sing-Along
Initial prompt
Toca nursery rhymes (2017). Tap on ginger 🐱 or Harvey 🐶 to make it sing Mary had a little lamb, little bo peep, hey diddle diddle, Robin Hood, ring around the Rosie, or grand old Duke of York, press the chalkboard to make it draw the notes
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function (characterType) {
var self = Container.call(this);
var body = self.attachAsset(characterType, {
anchorX: 0.5,
anchorY: 0.5
});
var leftEye = self.attachAsset(characterType + 'Eyes', {
anchorX: 0.5,
anchorY: 0.5,
x: -60,
y: -80
});
var rightEye = self.attachAsset(characterType + 'Eyes', {
anchorX: 0.5,
anchorY: 0.5,
x: 60,
y: -80
});
var mouth = self.attachAsset('mouth', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: 40,
scaleY: 0.5
});
self.isSinging = false;
self.originalScale = 1;
self.startSinging = function () {
self.isSinging = true;
tween(self, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 200,
easing: tween.easeOut
});
tween(mouth, {
scaleY: 1
}, {
duration: 150
});
};
self.stopSinging = function () {
self.isSinging = false;
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeOut
});
tween(mouth, {
scaleY: 0.5
}, {
duration: 200
});
};
self.down = function (x, y, obj) {
if (!currentlyPlaying) {
LK.getSound('tapSound').play();
startNurseryRhyme(characterType);
}
};
return self;
});
var MusicalNote = Container.expand(function () {
var self = Container.call(this);
var note = self.attachAsset('musicalNote', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -2;
self.life = 300;
self.maxLife = 300;
self.update = function () {
self.y += self.speed;
self.life--;
var alpha = self.life / self.maxLife;
note.alpha = alpha;
if (self.life <= 0) {
self.destroy();
for (var i = musicalNotes.length - 1; i >= 0; i--) {
if (musicalNotes[i] === self) {
musicalNotes.splice(i, 1);
break;
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var nurseryRhymes = ['maryHadALittleLamb', 'littleBoBeep', 'heyDiddleDiddle', 'robinHood', 'ringAroundTheRosie', 'grandOldDukeOfYork'];
var nurseryRhymeNames = ['Mary Had a Little Lamb', 'Little Bo Peep', 'Hey Diddle Diddle', 'Robin Hood', 'Ring Around the Rosie', 'Grand Old Duke of York'];
var currentRhymeIndex = 0;
var currentlyPlaying = false;
var musicalNotes = [];
var noteSpawnTimer = 0;
// Create chalkboard
var chalkboard = game.addChild(LK.getAsset('chalkboard', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 800
}));
// Create characters
var ginger = game.addChild(new Character('ginger'));
ginger.x = 600;
ginger.y = 1400;
var harvey = game.addChild(new Character('harvey'));
harvey.x = 1400;
harvey.y = 1400;
// Create title text
var titleText = new Text2('Nursery Rhyme Sing-Along', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 120;
// Create current song text
var currentSongText = new Text2('Tap Ginger or Harvey to sing!', {
size: 60,
fill: 0xFFFFFF
});
currentSongText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(currentSongText);
currentSongText.y = -50;
function startNurseryRhyme(character) {
if (currentlyPlaying) return;
currentlyPlaying = true;
var rhymeId = nurseryRhymes[currentRhymeIndex];
var rhymeName = nurseryRhymeNames[currentRhymeIndex];
currentSongText.setText('Now Playing: ' + rhymeName);
if (character === 'ginger') {
ginger.startSinging();
harvey.stopSinging();
} else {
harvey.startSinging();
ginger.stopSinging();
}
LK.playMusic(rhymeId, {
loop: false
});
// Stop music and singing after song duration (assuming 30 seconds max)
LK.setTimeout(function () {
stopCurrentSong();
currentRhymeIndex = (currentRhymeIndex + 1) % nurseryRhymes.length;
}, 30000);
}
function stopCurrentSong() {
currentlyPlaying = false;
ginger.stopSinging();
harvey.stopSinging();
currentSongText.setText('Tap Ginger or Harvey to sing!');
LK.stopMusic();
}
function spawnMusicalNote() {
if (!currentlyPlaying) return;
var note = new MusicalNote();
note.x = Math.random() * 1400 + 300;
note.y = chalkboard.y + 400;
var randomScale = 0.8 + Math.random() * 0.6;
note.scaleX = randomScale;
note.scaleY = randomScale;
musicalNotes.push(note);
game.addChild(note);
}
game.update = function () {
// Spawn musical notes while playing
if (currentlyPlaying) {
noteSpawnTimer++;
if (noteSpawnTimer >= 20) {
spawnMusicalNote();
noteSpawnTimer = 0;
}
}
// Clean up destroyed notes
for (var i = musicalNotes.length - 1; i >= 0; i--) {
var note = musicalNotes[i];
if (!note.parent) {
musicalNotes.splice(i, 1);
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function (characterType) {
var self = Container.call(this);
var body = self.attachAsset(characterType, {
anchorX: 0.5,
anchorY: 0.5
});
var leftEye = self.attachAsset(characterType + 'Eyes', {
anchorX: 0.5,
anchorY: 0.5,
x: -60,
y: -80
});
var rightEye = self.attachAsset(characterType + 'Eyes', {
anchorX: 0.5,
anchorY: 0.5,
x: 60,
y: -80
});
var mouth = self.attachAsset('mouth', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: 40,
scaleY: 0.5
});
self.isSinging = false;
self.originalScale = 1;
self.startSinging = function () {
self.isSinging = true;
tween(self, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 200,
easing: tween.easeOut
});
tween(mouth, {
scaleY: 1
}, {
duration: 150
});
};
self.stopSinging = function () {
self.isSinging = false;
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeOut
});
tween(mouth, {
scaleY: 0.5
}, {
duration: 200
});
};
self.down = function (x, y, obj) {
if (!currentlyPlaying) {
LK.getSound('tapSound').play();
startNurseryRhyme(characterType);
}
};
return self;
});
var MusicalNote = Container.expand(function () {
var self = Container.call(this);
var note = self.attachAsset('musicalNote', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -2;
self.life = 300;
self.maxLife = 300;
self.update = function () {
self.y += self.speed;
self.life--;
var alpha = self.life / self.maxLife;
note.alpha = alpha;
if (self.life <= 0) {
self.destroy();
for (var i = musicalNotes.length - 1; i >= 0; i--) {
if (musicalNotes[i] === self) {
musicalNotes.splice(i, 1);
break;
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var nurseryRhymes = ['maryHadALittleLamb', 'littleBoBeep', 'heyDiddleDiddle', 'robinHood', 'ringAroundTheRosie', 'grandOldDukeOfYork'];
var nurseryRhymeNames = ['Mary Had a Little Lamb', 'Little Bo Peep', 'Hey Diddle Diddle', 'Robin Hood', 'Ring Around the Rosie', 'Grand Old Duke of York'];
var currentRhymeIndex = 0;
var currentlyPlaying = false;
var musicalNotes = [];
var noteSpawnTimer = 0;
// Create chalkboard
var chalkboard = game.addChild(LK.getAsset('chalkboard', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 800
}));
// Create characters
var ginger = game.addChild(new Character('ginger'));
ginger.x = 600;
ginger.y = 1400;
var harvey = game.addChild(new Character('harvey'));
harvey.x = 1400;
harvey.y = 1400;
// Create title text
var titleText = new Text2('Nursery Rhyme Sing-Along', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 120;
// Create current song text
var currentSongText = new Text2('Tap Ginger or Harvey to sing!', {
size: 60,
fill: 0xFFFFFF
});
currentSongText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(currentSongText);
currentSongText.y = -50;
function startNurseryRhyme(character) {
if (currentlyPlaying) return;
currentlyPlaying = true;
var rhymeId = nurseryRhymes[currentRhymeIndex];
var rhymeName = nurseryRhymeNames[currentRhymeIndex];
currentSongText.setText('Now Playing: ' + rhymeName);
if (character === 'ginger') {
ginger.startSinging();
harvey.stopSinging();
} else {
harvey.startSinging();
ginger.stopSinging();
}
LK.playMusic(rhymeId, {
loop: false
});
// Stop music and singing after song duration (assuming 30 seconds max)
LK.setTimeout(function () {
stopCurrentSong();
currentRhymeIndex = (currentRhymeIndex + 1) % nurseryRhymes.length;
}, 30000);
}
function stopCurrentSong() {
currentlyPlaying = false;
ginger.stopSinging();
harvey.stopSinging();
currentSongText.setText('Tap Ginger or Harvey to sing!');
LK.stopMusic();
}
function spawnMusicalNote() {
if (!currentlyPlaying) return;
var note = new MusicalNote();
note.x = Math.random() * 1400 + 300;
note.y = chalkboard.y + 400;
var randomScale = 0.8 + Math.random() * 0.6;
note.scaleX = randomScale;
note.scaleY = randomScale;
musicalNotes.push(note);
game.addChild(note);
}
game.update = function () {
// Spawn musical notes while playing
if (currentlyPlaying) {
noteSpawnTimer++;
if (noteSpawnTimer >= 20) {
spawnMusicalNote();
noteSpawnTimer = 0;
}
}
// Clean up destroyed notes
for (var i = musicalNotes.length - 1; i >= 0; i--) {
var note = musicalNotes[i];
if (!note.parent) {
musicalNotes.splice(i, 1);
}
}
};