Code edit (1 edits merged)
Please save this source code
User prompt
Toca Friends Village
Initial prompt
Toca friends (2014-2015). Tap on twitter’s house 🐦, jacky’s house 🐰, Harvey’s house 🐶, ginger’s house 🐱, or buzzy’s house 🐹 to get started, tap on the doorbell to ring it from the beginning, tap on feeding mode, sleeping mode, bath mode, balloon blowing mode, or bubble blowing mode to get started.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var ActivityButton = Container.expand(function (activity, x, y) {
var self = Container.call(this);
self.activity = activity;
var buttonGraphics = self.attachAsset(activity + 'Button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(activity.charAt(0).toUpperCase() + activity.slice(1), {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.y = 5;
self.addChild(buttonText);
self.x = x;
self.y = y;
self.down = function () {
tween(buttonGraphics, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
tween(buttonGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
LK.setTimeout(function () {
startActivity(activity);
}, 200);
};
return self;
});
var Balloon = Container.expand(function () {
var self = Container.call(this);
var balloonGraphics = self.attachAsset('balloon', {
anchorX: 0.5,
anchorY: 1.0
});
var colors = [0xFF1493, 0x00BFFF, 0x32CD32, 0xFFD700, 0xFF6347];
balloonGraphics.tint = colors[Math.floor(Math.random() * colors.length)];
self.speedY = -1 - Math.random() * 2;
self.speedX = (Math.random() - 0.5) * 1;
self.update = function () {
self.y += self.speedY;
self.x += self.speedX;
if (self.y < -150) {
self.destroy();
var index = balloons.indexOf(self);
if (index > -1) {
balloons.splice(index, 1);
}
}
};
self.down = function () {
currentCharacterObj.playHappyAnimation();
tween(self, {
alpha: 0,
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 400,
onFinish: function onFinish() {
self.destroy();
var index = balloons.indexOf(self);
if (index > -1) {
balloons.splice(index, 1);
}
}
});
};
return self;
});
var Bubble = Container.expand(function () {
var self = Container.call(this);
var bubbleGraphics = self.attachAsset('bubble', {
anchorX: 0.5,
anchorY: 0.5
});
bubbleGraphics.alpha = 0.7;
self.speedY = -2 - Math.random() * 3;
self.speedX = (Math.random() - 0.5) * 2;
self.update = function () {
self.y += self.speedY;
self.x += self.speedX;
if (self.y < -100) {
self.destroy();
var index = bubbles.indexOf(self);
if (index > -1) {
bubbles.splice(index, 1);
}
}
};
self.down = function () {
LK.getSound('bubble').play();
tween(self, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
var index = bubbles.indexOf(self);
if (index > -1) {
bubbles.splice(index, 1);
}
}
});
};
return self;
});
var Character = Container.expand(function (characterName) {
var self = Container.call(this);
self.characterName = characterName;
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
var colors = {
'Twitter': 0x00BFFF,
'Jacky': 0xFF6347,
'Harvey': 0x32CD32,
'Ginger': 0xFFA500,
'Buzzy': 0xFFD700
};
characterGraphics.tint = colors[characterName] || 0x8B4513;
self.x = 1024;
self.y = 1000;
self.playHappyAnimation = function () {
tween(characterGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 300
});
tween(characterGraphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 300
});
LK.getSound('happy').play();
};
return self;
});
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
self.eaten = false;
self.down = function () {
if (!self.eaten) {
self.eaten = true;
currentCharacterObj.playHappyAnimation();
tween(self, {
alpha: 0,
scaleX: 0.1,
scaleY: 0.1
}, {
duration: 500,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
var House = Container.expand(function (houseId, color, name, x, y) {
var self = Container.call(this);
self.houseId = houseId;
self.characterName = name;
var houseGraphics = self.attachAsset('house' + houseId, {
anchorX: 0.5,
anchorY: 1.0
});
var doorbell = self.attachAsset('doorbell', {
anchorX: 0.5,
anchorY: 0.5,
x: 120,
y: -150
});
var nameText = new Text2(name, {
size: 40,
fill: 0xFFFFFF
});
nameText.anchor.set(0.5, 0.5);
nameText.x = 0;
nameText.y = -350;
self.addChild(nameText);
self.x = x;
self.y = y;
self.down = function (x, y, obj) {
var localPos = self.toLocal(obj.parent.toGlobal(obj.position));
if (localPos.x > 80 && localPos.x < 160 && localPos.y > -180 && localPos.y < -120) {
LK.getSound('doorbell').play();
tween(doorbell, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200
});
tween(doorbell, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200
});
LK.setTimeout(function () {
currentState = 'character';
currentCharacter = self.characterName;
showCharacterScreen();
}, 400);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var currentState = 'village';
var currentCharacter = '';
var currentCharacterObj = null;
var currentActivity = '';
var houses = [];
var activityButtons = [];
var foods = [];
var bubbles = [];
var balloons = [];
var titleText = new Text2('Toca Friends Village', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
var backButton = null;
function createVillage() {
var houseData = [{
name: 'Twitter',
x: 400,
y: 1200
}, {
name: 'Jacky',
x: 900,
y: 1200
}, {
name: 'Harvey',
x: 1400,
y: 1200
}, {
name: 'Ginger',
x: 400,
y: 1800
}, {
name: 'Buzzy',
x: 900,
y: 1800
}];
for (var i = 0; i < houseData.length; i++) {
var houseInfo = houseData[i];
var house = new House(i + 1, 0xFF6B9D, houseInfo.name, houseInfo.x, houseInfo.y);
houses.push(house);
game.addChild(house);
}
}
function showCharacterScreen() {
clearVillage();
currentCharacterObj = new Character(currentCharacter);
game.addChild(currentCharacterObj);
var activities = ['feed', 'sleep', 'bath', 'balloon', 'bubble'];
var buttonPositions = [{
x: 300,
y: 1600
}, {
x: 500,
y: 1600
}, {
x: 700,
y: 1600
}, {
x: 900,
y: 1600
}, {
x: 1100,
y: 1600
}];
for (var i = 0; i < activities.length; i++) {
var button = new ActivityButton(activities[i], buttonPositions[i].x, buttonPositions[i].y);
activityButtons.push(button);
game.addChild(button);
}
createBackButton();
}
function startActivity(activity) {
currentActivity = activity;
clearActivityButtons();
if (activity === 'feed') {
startFeedingActivity();
} else if (activity === 'sleep') {
startSleepingActivity();
} else if (activity === 'bath') {
startBathingActivity();
} else if (activity === 'balloon') {
startBalloonActivity();
} else if (activity === 'bubble') {
startBubbleActivity();
}
}
function startFeedingActivity() {
for (var i = 0; i < 6; i++) {
var food = new Food();
food.x = 300 + i % 3 * 200 + Math.random() * 100;
food.y = 1400 + Math.floor(i / 3) * 150 + Math.random() * 100;
foods.push(food);
game.addChild(food);
}
}
function startSleepingActivity() {
currentCharacterObj.playHappyAnimation();
tween(currentCharacterObj, {
alpha: 0.5
}, {
duration: 2000
});
tween(currentCharacterObj, {
alpha: 1.0
}, {
duration: 2000
});
}
function startBathingActivity() {
currentCharacterObj.playHappyAnimation();
tween(currentCharacterObj, {
tint: 0x87CEEB
}, {
duration: 1000
});
tween(currentCharacterObj, {
tint: 0xFFFFFF
}, {
duration: 1000
});
}
function startBalloonActivity() {
// Balloons will be created on tap
}
function startBubbleActivity() {
// Bubbles will be created on tap
}
function createBackButton() {
backButton = game.addChild(LK.getAsset('backButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2000
}));
var backText = new Text2('Back', {
size: 30,
fill: 0xFFFFFF
});
backText.anchor.set(0.5, 0.5);
backText.x = 1024;
backText.y = 2000;
game.addChild(backText);
}
function clearVillage() {
for (var i = 0; i < houses.length; i++) {
houses[i].destroy();
}
houses = [];
}
function clearActivityButtons() {
for (var i = 0; i < activityButtons.length; i++) {
activityButtons[i].destroy();
}
activityButtons = [];
}
function clearActivity() {
for (var i = 0; i < foods.length; i++) {
foods[i].destroy();
}
foods = [];
for (var i = 0; i < bubbles.length; i++) {
bubbles[i].destroy();
}
bubbles = [];
for (var i = 0; i < balloons.length; i++) {
balloons[i].destroy();
}
balloons = [];
currentActivity = '';
}
function returnToVillage() {
currentState = 'village';
currentCharacter = '';
if (currentCharacterObj) {
currentCharacterObj.destroy();
currentCharacterObj = null;
}
if (backButton) {
backButton.destroy();
backButton = null;
}
clearActivityButtons();
clearActivity();
createVillage();
}
game.down = function (x, y, obj) {
if (currentState === 'character' && backButton) {
var globalPos = obj.parent.toGlobal(obj.position);
var localPos = game.toLocal(globalPos);
if (localPos.x > 974 && localPos.x < 1074 && localPos.y > 1970 && localPos.y < 2030) {
returnToVillage();
return;
}
}
if (currentActivity === 'balloon') {
var balloon = new Balloon();
balloon.x = x + Math.random() * 100 - 50;
balloon.y = y;
balloons.push(balloon);
game.addChild(balloon);
} else if (currentActivity === 'bubble') {
var bubble = new Bubble();
bubble.x = x + Math.random() * 50 - 25;
bubble.y = y;
bubbles.push(bubble);
game.addChild(bubble);
LK.getSound('bubble').play();
}
};
game.update = function () {
for (var i = bubbles.length - 1; i >= 0; i--) {
if (bubbles[i] && bubbles[i].update) {
bubbles[i].update();
}
}
for (var i = balloons.length - 1; i >= 0; i--) {
if (balloons[i] && balloons[i].update) {
balloons[i].update();
}
}
};
createVillage(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,465 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var ActivityButton = Container.expand(function (activity, x, y) {
+ var self = Container.call(this);
+ self.activity = activity;
+ var buttonGraphics = self.attachAsset(activity + 'Button', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var buttonText = new Text2(activity.charAt(0).toUpperCase() + activity.slice(1), {
+ size: 30,
+ fill: 0xFFFFFF
+ });
+ buttonText.anchor.set(0.5, 0.5);
+ buttonText.y = 5;
+ self.addChild(buttonText);
+ self.x = x;
+ self.y = y;
+ self.down = function () {
+ tween(buttonGraphics, {
+ scaleX: 0.9,
+ scaleY: 0.9
+ }, {
+ duration: 100
+ });
+ tween(buttonGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 100
+ });
+ LK.setTimeout(function () {
+ startActivity(activity);
+ }, 200);
+ };
+ return self;
+});
+var Balloon = Container.expand(function () {
+ var self = Container.call(this);
+ var balloonGraphics = self.attachAsset('balloon', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ var colors = [0xFF1493, 0x00BFFF, 0x32CD32, 0xFFD700, 0xFF6347];
+ balloonGraphics.tint = colors[Math.floor(Math.random() * colors.length)];
+ self.speedY = -1 - Math.random() * 2;
+ self.speedX = (Math.random() - 0.5) * 1;
+ self.update = function () {
+ self.y += self.speedY;
+ self.x += self.speedX;
+ if (self.y < -150) {
+ self.destroy();
+ var index = balloons.indexOf(self);
+ if (index > -1) {
+ balloons.splice(index, 1);
+ }
+ }
+ };
+ self.down = function () {
+ currentCharacterObj.playHappyAnimation();
+ tween(self, {
+ alpha: 0,
+ scaleX: 1.5,
+ scaleY: 1.5
+ }, {
+ duration: 400,
+ onFinish: function onFinish() {
+ self.destroy();
+ var index = balloons.indexOf(self);
+ if (index > -1) {
+ balloons.splice(index, 1);
+ }
+ }
+ });
+ };
+ return self;
+});
+var Bubble = Container.expand(function () {
+ var self = Container.call(this);
+ var bubbleGraphics = self.attachAsset('bubble', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ bubbleGraphics.alpha = 0.7;
+ self.speedY = -2 - Math.random() * 3;
+ self.speedX = (Math.random() - 0.5) * 2;
+ self.update = function () {
+ self.y += self.speedY;
+ self.x += self.speedX;
+ if (self.y < -100) {
+ self.destroy();
+ var index = bubbles.indexOf(self);
+ if (index > -1) {
+ bubbles.splice(index, 1);
+ }
+ }
+ };
+ self.down = function () {
+ LK.getSound('bubble').play();
+ tween(self, {
+ alpha: 0,
+ scaleX: 2,
+ scaleY: 2
+ }, {
+ duration: 300,
+ onFinish: function onFinish() {
+ self.destroy();
+ var index = bubbles.indexOf(self);
+ if (index > -1) {
+ bubbles.splice(index, 1);
+ }
+ }
+ });
+ };
+ return self;
+});
+var Character = Container.expand(function (characterName) {
+ var self = Container.call(this);
+ self.characterName = characterName;
+ var characterGraphics = self.attachAsset('character', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var colors = {
+ 'Twitter': 0x00BFFF,
+ 'Jacky': 0xFF6347,
+ 'Harvey': 0x32CD32,
+ 'Ginger': 0xFFA500,
+ 'Buzzy': 0xFFD700
+ };
+ characterGraphics.tint = colors[characterName] || 0x8B4513;
+ self.x = 1024;
+ self.y = 1000;
+ self.playHappyAnimation = function () {
+ tween(characterGraphics, {
+ scaleX: 1.2,
+ scaleY: 1.2
+ }, {
+ duration: 300
+ });
+ tween(characterGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 300
+ });
+ LK.getSound('happy').play();
+ };
+ return self;
+});
+var Food = Container.expand(function () {
+ var self = Container.call(this);
+ var foodGraphics = self.attachAsset('food', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.eaten = false;
+ self.down = function () {
+ if (!self.eaten) {
+ self.eaten = true;
+ currentCharacterObj.playHappyAnimation();
+ tween(self, {
+ alpha: 0,
+ scaleX: 0.1,
+ scaleY: 0.1
+ }, {
+ duration: 500,
+ onFinish: function onFinish() {
+ self.destroy();
+ }
+ });
+ }
+ };
+ return self;
+});
+var House = Container.expand(function (houseId, color, name, x, y) {
+ var self = Container.call(this);
+ self.houseId = houseId;
+ self.characterName = name;
+ var houseGraphics = self.attachAsset('house' + houseId, {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ var doorbell = self.attachAsset('doorbell', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 120,
+ y: -150
+ });
+ var nameText = new Text2(name, {
+ size: 40,
+ fill: 0xFFFFFF
+ });
+ nameText.anchor.set(0.5, 0.5);
+ nameText.x = 0;
+ nameText.y = -350;
+ self.addChild(nameText);
+ self.x = x;
+ self.y = y;
+ self.down = function (x, y, obj) {
+ var localPos = self.toLocal(obj.parent.toGlobal(obj.position));
+ if (localPos.x > 80 && localPos.x < 160 && localPos.y > -180 && localPos.y < -120) {
+ LK.getSound('doorbell').play();
+ tween(doorbell, {
+ scaleX: 1.2,
+ scaleY: 1.2
+ }, {
+ duration: 200
+ });
+ tween(doorbell, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 200
+ });
+ LK.setTimeout(function () {
+ currentState = 'character';
+ currentCharacter = self.characterName;
+ showCharacterScreen();
+ }, 400);
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var currentState = 'village';
+var currentCharacter = '';
+var currentCharacterObj = null;
+var currentActivity = '';
+var houses = [];
+var activityButtons = [];
+var foods = [];
+var bubbles = [];
+var balloons = [];
+var titleText = new Text2('Toca Friends Village', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+titleText.anchor.set(0.5, 0);
+LK.gui.top.addChild(titleText);
+var backButton = null;
+function createVillage() {
+ var houseData = [{
+ name: 'Twitter',
+ x: 400,
+ y: 1200
+ }, {
+ name: 'Jacky',
+ x: 900,
+ y: 1200
+ }, {
+ name: 'Harvey',
+ x: 1400,
+ y: 1200
+ }, {
+ name: 'Ginger',
+ x: 400,
+ y: 1800
+ }, {
+ name: 'Buzzy',
+ x: 900,
+ y: 1800
+ }];
+ for (var i = 0; i < houseData.length; i++) {
+ var houseInfo = houseData[i];
+ var house = new House(i + 1, 0xFF6B9D, houseInfo.name, houseInfo.x, houseInfo.y);
+ houses.push(house);
+ game.addChild(house);
+ }
+}
+function showCharacterScreen() {
+ clearVillage();
+ currentCharacterObj = new Character(currentCharacter);
+ game.addChild(currentCharacterObj);
+ var activities = ['feed', 'sleep', 'bath', 'balloon', 'bubble'];
+ var buttonPositions = [{
+ x: 300,
+ y: 1600
+ }, {
+ x: 500,
+ y: 1600
+ }, {
+ x: 700,
+ y: 1600
+ }, {
+ x: 900,
+ y: 1600
+ }, {
+ x: 1100,
+ y: 1600
+ }];
+ for (var i = 0; i < activities.length; i++) {
+ var button = new ActivityButton(activities[i], buttonPositions[i].x, buttonPositions[i].y);
+ activityButtons.push(button);
+ game.addChild(button);
+ }
+ createBackButton();
+}
+function startActivity(activity) {
+ currentActivity = activity;
+ clearActivityButtons();
+ if (activity === 'feed') {
+ startFeedingActivity();
+ } else if (activity === 'sleep') {
+ startSleepingActivity();
+ } else if (activity === 'bath') {
+ startBathingActivity();
+ } else if (activity === 'balloon') {
+ startBalloonActivity();
+ } else if (activity === 'bubble') {
+ startBubbleActivity();
+ }
+}
+function startFeedingActivity() {
+ for (var i = 0; i < 6; i++) {
+ var food = new Food();
+ food.x = 300 + i % 3 * 200 + Math.random() * 100;
+ food.y = 1400 + Math.floor(i / 3) * 150 + Math.random() * 100;
+ foods.push(food);
+ game.addChild(food);
+ }
+}
+function startSleepingActivity() {
+ currentCharacterObj.playHappyAnimation();
+ tween(currentCharacterObj, {
+ alpha: 0.5
+ }, {
+ duration: 2000
+ });
+ tween(currentCharacterObj, {
+ alpha: 1.0
+ }, {
+ duration: 2000
+ });
+}
+function startBathingActivity() {
+ currentCharacterObj.playHappyAnimation();
+ tween(currentCharacterObj, {
+ tint: 0x87CEEB
+ }, {
+ duration: 1000
+ });
+ tween(currentCharacterObj, {
+ tint: 0xFFFFFF
+ }, {
+ duration: 1000
+ });
+}
+function startBalloonActivity() {
+ // Balloons will be created on tap
+}
+function startBubbleActivity() {
+ // Bubbles will be created on tap
+}
+function createBackButton() {
+ backButton = game.addChild(LK.getAsset('backButton', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 1024,
+ y: 2000
+ }));
+ var backText = new Text2('Back', {
+ size: 30,
+ fill: 0xFFFFFF
+ });
+ backText.anchor.set(0.5, 0.5);
+ backText.x = 1024;
+ backText.y = 2000;
+ game.addChild(backText);
+}
+function clearVillage() {
+ for (var i = 0; i < houses.length; i++) {
+ houses[i].destroy();
+ }
+ houses = [];
+}
+function clearActivityButtons() {
+ for (var i = 0; i < activityButtons.length; i++) {
+ activityButtons[i].destroy();
+ }
+ activityButtons = [];
+}
+function clearActivity() {
+ for (var i = 0; i < foods.length; i++) {
+ foods[i].destroy();
+ }
+ foods = [];
+ for (var i = 0; i < bubbles.length; i++) {
+ bubbles[i].destroy();
+ }
+ bubbles = [];
+ for (var i = 0; i < balloons.length; i++) {
+ balloons[i].destroy();
+ }
+ balloons = [];
+ currentActivity = '';
+}
+function returnToVillage() {
+ currentState = 'village';
+ currentCharacter = '';
+ if (currentCharacterObj) {
+ currentCharacterObj.destroy();
+ currentCharacterObj = null;
+ }
+ if (backButton) {
+ backButton.destroy();
+ backButton = null;
+ }
+ clearActivityButtons();
+ clearActivity();
+ createVillage();
+}
+game.down = function (x, y, obj) {
+ if (currentState === 'character' && backButton) {
+ var globalPos = obj.parent.toGlobal(obj.position);
+ var localPos = game.toLocal(globalPos);
+ if (localPos.x > 974 && localPos.x < 1074 && localPos.y > 1970 && localPos.y < 2030) {
+ returnToVillage();
+ return;
+ }
+ }
+ if (currentActivity === 'balloon') {
+ var balloon = new Balloon();
+ balloon.x = x + Math.random() * 100 - 50;
+ balloon.y = y;
+ balloons.push(balloon);
+ game.addChild(balloon);
+ } else if (currentActivity === 'bubble') {
+ var bubble = new Bubble();
+ bubble.x = x + Math.random() * 50 - 25;
+ bubble.y = y;
+ bubbles.push(bubble);
+ game.addChild(bubble);
+ LK.getSound('bubble').play();
+ }
+};
+game.update = function () {
+ for (var i = bubbles.length - 1; i >= 0; i--) {
+ if (bubbles[i] && bubbles[i].update) {
+ bubbles[i].update();
+ }
+ }
+ for (var i = balloons.length - 1; i >= 0; i--) {
+ if (balloons[i] && balloons[i].update) {
+ balloons[i].update();
+ }
+ }
+};
+createVillage();
\ No newline at end of file