/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Unicorn = Container.expand(function () { var self = Container.call(this); // Create unicorn parts var body = self.attachAsset('unicornBody', { anchorX: 0.5, anchorY: 0.5, x: 0, y: 50 }); var head = self.attachAsset('unicornHead', { anchorX: 0.5, anchorY: 0.5, x: 0, y: -80 }); var horn = self.attachAsset('unicornHorn', { anchorX: 0.5, anchorY: 1, x: 0, y: -160 }); var mane = self.attachAsset('unicornMane', { anchorX: 0.5, anchorY: 0.5, x: -60, y: -80 }); var tail = self.attachAsset('unicornTail', { anchorX: 0.5, anchorY: 0.5, x: -180, y: 20 }); var leftHoof = self.attachAsset('unicornHoof', { anchorX: 0.5, anchorY: 0.5, x: -60, y: 180 }); var rightHoof = self.attachAsset('unicornHoof', { anchorX: 0.5, anchorY: 0.5, x: 60, y: 180 }); // Store references for animations self.horn = horn; self.leftHoof = leftHoof; self.rightHoof = rightHoof; self.isLullabyMode = false; self.lullabyInterval = null; // Animation methods self.glowHorn = function (bright) { if (bright) { tween(self.horn, { tint: 0xFFFFFF }, { duration: 100 }); } else { tween(self.horn, { tint: 0xFFD700 }, { duration: 300 }); } }; self.pulseHorn = function () { tween(self.horn, { alpha: 0.6 }, { duration: 1000, easing: tween.easeInOut, onFinish: function onFinish() { if (self.isLullabyMode) { tween(self.horn, { alpha: 1 }, { duration: 1000, easing: tween.easeInOut, onFinish: function onFinish() { if (self.isLullabyMode) { self.pulseHorn(); } } }); } } }); }; self.startLullabyMode = function () { self.isLullabyMode = true; self.pulseHorn(); // Play lullaby chimes every 2 seconds self.lullabyInterval = LK.setInterval(function () { if (self.isLullabyMode) { LK.getSound('lullabyChime').play(); } }, 2000); }; self.stopLullabyMode = function () { self.isLullabyMode = false; if (self.lullabyInterval) { LK.clearInterval(self.lullabyInterval); self.lullabyInterval = null; } tween.stop(self.horn, { alpha: true }); self.horn.alpha = 1; self.horn.tint = 0xFFD700; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var unicorn = game.addChild(new Unicorn()); unicorn.x = 2048 / 2; unicorn.y = 2732 / 2; // Double tap detection variables var lastTapTime = 0; var doubleTapThreshold = 500; // 500ms for double tap detection // Touch interaction unicorn.down = function (x, y, obj) { var currentTime = Date.now(); var timeDifference = currentTime - lastTapTime; if (timeDifference < doubleTapThreshold) { // Double tap detected if (unicorn.isLullabyMode) { unicorn.stopLullabyMode(); } else { unicorn.startLullabyMode(); } } else { // Single tap if (!unicorn.isLullabyMode) { LK.getSound('trottingChime').play(); unicorn.glowHorn(true); LK.setTimeout(function () { unicorn.glowHorn(false); }, 200); } } lastTapTime = currentTime; }; // Add mode indicator text var modeText = new Text2('Tap the unicorn!', { size: 60, fill: 0xFFFFFF }); modeText.anchor.set(0.5, 0); LK.gui.top.addChild(modeText); // Update mode indicator game.update = function () { if (unicorn.isLullabyMode) { modeText.setText('Lullaby Mode Active ♫'); } else { modeText.setText('Tap the unicorn!'); } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Unicorn = Container.expand(function () {
var self = Container.call(this);
// Create unicorn parts
var body = self.attachAsset('unicornBody', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: 50
});
var head = self.attachAsset('unicornHead', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: -80
});
var horn = self.attachAsset('unicornHorn', {
anchorX: 0.5,
anchorY: 1,
x: 0,
y: -160
});
var mane = self.attachAsset('unicornMane', {
anchorX: 0.5,
anchorY: 0.5,
x: -60,
y: -80
});
var tail = self.attachAsset('unicornTail', {
anchorX: 0.5,
anchorY: 0.5,
x: -180,
y: 20
});
var leftHoof = self.attachAsset('unicornHoof', {
anchorX: 0.5,
anchorY: 0.5,
x: -60,
y: 180
});
var rightHoof = self.attachAsset('unicornHoof', {
anchorX: 0.5,
anchorY: 0.5,
x: 60,
y: 180
});
// Store references for animations
self.horn = horn;
self.leftHoof = leftHoof;
self.rightHoof = rightHoof;
self.isLullabyMode = false;
self.lullabyInterval = null;
// Animation methods
self.glowHorn = function (bright) {
if (bright) {
tween(self.horn, {
tint: 0xFFFFFF
}, {
duration: 100
});
} else {
tween(self.horn, {
tint: 0xFFD700
}, {
duration: 300
});
}
};
self.pulseHorn = function () {
tween(self.horn, {
alpha: 0.6
}, {
duration: 1000,
easing: tween.easeInOut,
onFinish: function onFinish() {
if (self.isLullabyMode) {
tween(self.horn, {
alpha: 1
}, {
duration: 1000,
easing: tween.easeInOut,
onFinish: function onFinish() {
if (self.isLullabyMode) {
self.pulseHorn();
}
}
});
}
}
});
};
self.startLullabyMode = function () {
self.isLullabyMode = true;
self.pulseHorn();
// Play lullaby chimes every 2 seconds
self.lullabyInterval = LK.setInterval(function () {
if (self.isLullabyMode) {
LK.getSound('lullabyChime').play();
}
}, 2000);
};
self.stopLullabyMode = function () {
self.isLullabyMode = false;
if (self.lullabyInterval) {
LK.clearInterval(self.lullabyInterval);
self.lullabyInterval = null;
}
tween.stop(self.horn, {
alpha: true
});
self.horn.alpha = 1;
self.horn.tint = 0xFFD700;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var unicorn = game.addChild(new Unicorn());
unicorn.x = 2048 / 2;
unicorn.y = 2732 / 2;
// Double tap detection variables
var lastTapTime = 0;
var doubleTapThreshold = 500; // 500ms for double tap detection
// Touch interaction
unicorn.down = function (x, y, obj) {
var currentTime = Date.now();
var timeDifference = currentTime - lastTapTime;
if (timeDifference < doubleTapThreshold) {
// Double tap detected
if (unicorn.isLullabyMode) {
unicorn.stopLullabyMode();
} else {
unicorn.startLullabyMode();
}
} else {
// Single tap
if (!unicorn.isLullabyMode) {
LK.getSound('trottingChime').play();
unicorn.glowHorn(true);
LK.setTimeout(function () {
unicorn.glowHorn(false);
}, 200);
}
}
lastTapTime = currentTime;
};
// Add mode indicator text
var modeText = new Text2('Tap the unicorn!', {
size: 60,
fill: 0xFFFFFF
});
modeText.anchor.set(0.5, 0);
LK.gui.top.addChild(modeText);
// Update mode indicator
game.update = function () {
if (unicorn.isLullabyMode) {
modeText.setText('Lullaby Mode Active ♫');
} else {
modeText.setText('Tap the unicorn!');
}
};