User prompt
Please fix the bug: 'layerManager.addToLayer is not a function' in or related to this line: 'layerManager.addToLayer(background, LAYERS.BACKGROUND);' Line Number: 1617
User prompt
Please fix the bug: 'layerManager.addToLayer is not a function' in or related to this line: 'layerManager.addToLayer(background, LAYERS.BACKGROUND);' Line Number: 1622
User prompt
Please fix the bug: 'layerManager.addToLayer is not a function' in or related to this line: 'layerManager.addToLayer(background, LAYERS.BACKGROUND);' Line Number: 1611
User prompt
Please fix the bug: 'The supplied index is out of bounds' in or related to this line: 'game.setChildIndex(grassBack, 5); // Set grassBack to be rendered after Bird3' Line Number: 1154
User prompt
Please fix the bug: 'layerManager.addToLayer is not a function' in or related to this line: 'layerManager.addToLayer(background, LAYERS.BACKGROUND);' Line Number: 840
Code edit (1 edits merged)
Please save this source code
User prompt
rename 'electro' to 'dead2'
User prompt
create a layer for mirror like the other assets
Code edit (1 edits merged)
Please save this source code
User prompt
debug the app
Code edit (1 edits merged)
Please save this source code
User prompt
Each image element must have their own layer. Refactor catdefender.js layers system and set their order as below : var LAYERS = { BACKGROUND: 0, EFFECTS: 1, SUN: 2, JET: 3, CLOUD1: 4, UFO: 5, CLOUD2: 6, GRASS_BACK: 7, BIRD1: 8, TREE: 9, STACK: 10, BIRD2: 11, GRASS_FRONT: 12, CAT: 13, LASER: 14, RETICLE: 15, UI: 16 }; Do not modify or remove any other code.
User prompt
debug the app
Code edit (4 edits merged)
Please save this source code
User prompt
debug the code
User prompt
reorder layers in this order : BACKGROUND: 0, EFFECTS: 1, SUN: 2, JET: 3, CLOUD1: 4, UFO: 5, CLOUD2: 6, GRASS_BACK: 7, BIRD1: 8, TREE: 9, STACK: 10, BIRD2: 11, GRASS_FRONT: 12, CAT: 13, LASER: 14, RETICLE: 15, UI: 16
Code edit (2 edits merged)
Please save this source code
User prompt
make sure objects are rendered in the correct order as per LAYERS
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
mirror is supposed to render before grass-front, but i dont think it is
Code edit (2 edits merged)
Please save this source code
User prompt
if no layer for the mirror exists, please make one like the others and add it to the layer system
User prompt
increase the hit boxes of bird1, bird2, and ufo to the dimensions of the object
Code edit (1 edits merged)
Please save this source code
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ // Background class to represent the background image var Background = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.attachAsset('background', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(backgroundGraphics); }); // Branch asset // Bird1 class to represent the first kind of bird var Bird1 = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird1', { anchorX: 0.5, anchorY: 0.5 }); self.movement = new Bird1Movement(self, birdGraphics); }); // Bird2 class to represent the second kind of bird var Bird2 = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird2', { anchorX: 0.5, anchorY: 0.5 }); self.movement = new Bird2Movement(self, birdGraphics); }); // Cat class to manage cat behavior var Cat = Container.expand(function () { var self = Container.call(this); var catGraphics = self.attachAsset('cat', { anchorX: 0.5, anchorY: 1 }); self.update = function () { // Add any specific update logic for the cat here }; }); // Cloud class to represent cloud behavior var Cloud = Container.expand(function () { var self = Container.call(this); var cloudType = CLOUD_TYPES[Math.floor(Math.random() * CLOUD_TYPES.length)]; var cloudGraphics = self.attachAsset(cloudType, { anchorX: 0.5, anchorY: 0.5, alpha: 0.8 // Set default opacity to 80% }); self.movement = new CloudMovement(self, cloudGraphics); }); // CloudMovement class to handle cloud movement and fade effects var CloudMovement = Container.expand(function () { var self = Container.call(this); var cloudGraphics = self.attachAsset('cloud1', { anchorX: 0.5, anchorY: 0.5, alpha: 0.8 // Set default opacity to 80% }); self.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8 self.hasAccelerated = false; // Track if the cloud has already accelerated self.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left self.lastX = self.x; // Track last X position for future checks self.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections self.update = function () { flipImageVerticallyBasedOnDirection(cloudGraphics, self.lastX, self.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis self.x += self.speed * self.direction; // If the cloud moves off-screen, reposition it to the opposite side if (self.lastX <= 2048 + self.width / 2 && self.x > 2048 + self.width / 2) { self.x = -self.width / 2; } else if (self.lastX >= -self.width / 2 && self.x < -self.width / 2) { self.x = 2048 + self.width / 2; } // Check for overlap with other clouds for (var i = 0; i < clouds.length; i++) { if (clouds[i] !== self && self.intersects(clouds[i])) { if (!self.hasAccelerated) { self.speed *= 1.5; // Increase speed by 50% self.hasAccelerated = true; // Mark as accelerated } break; } else if (self.hasAccelerated && !self.intersects(clouds[i])) { self.speed /= 1.5; // Reset speed to original self.hasAccelerated = false; // Reset acceleration state } } // Check if the cloud intersects with the sun if (!self.lastIntersecting && self.intersects(sun)) { self.speed *= 2; // Double the speed when touching the sun tween(cloudGraphics, { alpha: 0.5 }, { duration: 3000, easing: tween.linear }); } else if (self.lastIntersecting && !self.intersects(sun)) { self.speed /= 2; // Reset speed to original when not touching the sun tween(cloudGraphics, { alpha: 1.0 }, { duration: 3000, easing: tween.linear }); } self.lastIntersecting = self.intersects(sun); self.lastX = self.x; // Update lastX after movement }; }); // GrassBack class to represent the grass image var GrassBack = Container.expand(function () { var self = Container.call(this); var grassGraphics = self.attachAsset('grass-back', { anchorX: 0.5, anchorY: 1 }); self.update = function () { // Add any specific update logic for the grass here // Example: Slightly move the grass to simulate wind effect self.x += Math.sin((LK.ticks + 100) / 90) * 0.15; // Swaying effect, increased speed }; self.addChild(grassGraphics); // Add grass graphics to the container }); // GrassFront class to represent the second grass image var GrassFront = Container.expand(function () { var self = Container.call(this); var grass2Graphics = self.attachAsset('grass-front', { anchorX: 0.5, anchorY: 1 }); self.lastX = self.x; // Initialize lastX for tracking changes on X self.addChild(grass2Graphics); // Add grass2 graphics to the container self.update = function () { // Add any specific update logic for the grass here // Example: Slightly move the grass to simulate wind effect self.x += Math.sin((LK.ticks + 50) / 100) * 0.125; // Swaying effect, reduced by 50% self.lastX = self.x; // Update lastX after movement }; }); // Jet1 class to represent a jet flying across the screen var Jet1 = Container.expand(function () { var self = Container.call(this); var jetGraphics = self.attachAsset('jet1', { anchorX: 0.5, anchorY: 0.5, flipX: 1 // Apply the same directional image flipping logic as bird1 and bird2 }); // Add a label to the jet var jetLabel = new Text2('Jet', { size: 50, fill: 0xFFFFFF }); jetLabel.anchor.set(0.5, 0.5); // Center the label on the jet jetLabel.y = -60; // Position the label above the jet self.addChild(jetLabel); self.speed = 10; // Set the speed of the jet self.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left self.lastX = self.x; // Track last X position for future checks self.x = Math.random() < 0.5 ? 2048 + self.width / 2 : -self.width / 2; // Start from either the far right or left edge of the screen self.update = function () { flipImageVerticallyBasedOnDirection(jetGraphics, self.lastX, self.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis self.x += self.speed * self.direction; // If the jet moves off-screen, destroy it and set it to null if (!game.jetSoundPlayed) { LK.getSound('jet1').play(); // Play jet1 sound when the jet is on screen game.jetSoundPlayed = true; // Ensure it only plays once } if (self.lastX <= 2048 + self.width / 2 && self.x > 2048 + self.width / 2 || self.lastX >= -self.width / 2 && self.x < -self.width / 2) { self.destroy(); game.jet = null; game.jetSoundPlayed = false; // Reset the flag when the jet is removed // Set a timer to respawn the jet between 10-20 seconds var respawnTime = Math.random() * 10000 + 10000; // Random time between 10-20 seconds game.jetSpawnTimer = LK.setTimeout(function () { var jet = new Jet1(); jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen game.addChild(jet); game.jet = jet; if (!game.jetSoundPlayed) { LK.getSound('jetSound').play(); // Play jet sound when it appears game.jetSoundPlayed = true; // Ensure it only plays once } game.jetSpawnTimer = null; // Clear the timer reference }, respawnTime); } self.lastX = self.x; // Update lastX after movement }; }); // Laser class to represent a laser shot from the cat var Laser = Container.expand(function (startX, startY, targetX, targetY) { var self = Container.call(this); var laserGraphics = self.attachAsset('laser2', { anchorX: 0.5, anchorY: 0.5, brightness: 2.0 // Increase brightness by 200% }); // Play the laser1 sound when the laser is created LK.getSound('laser1').play(); // Calculate direction and speed var dx = targetX - startX; var dy = targetY - startY; var distance = Math.sqrt(dx * dx + dy * dy); var speed = 60; // Speed of the laser increased by 200% self.vx = dx / distance * speed; self.vy = dy / distance * speed; // Set initial position self.x = startX; self.y = startY; // Update function to move the laser self.update = function () { game.children.forEach(function (child) { if (AABBIntersect(child, bird)) { // Debug visualization: Draw a rectangle around the intersecting objects var debugGraphics = new Graphics(); debugGraphics.lineStyle(2, 0xFF0000, 1); debugGraphics.drawRect(bird.x, bird.y, bird.width, bird.height); game.addChild(debugGraphics); LK.setTimeout(function () { game.removeChild(debugGraphics); }, 100); // Remove debug visualization after 100ms } }); // Close the loop for iterating over game children self.x += self.vx; self.y += self.vy; // Remove laser if it goes off-screen if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { self.destroy(); } }; }); // Light1 class to represent a light object var Light1 = Container.expand(function () { var self = Container.call(this); var lightGraphics = self.attachAsset('light1', { anchorX: 0.5, anchorY: 0.5, alpha: 0.2 // Set transparency to 20% }); self.x = 450, self.y = 450; self.update = function () { // Add any specific update logic for the light here }; self.pulse = new Light1Pulse(lightGraphics); self.pulse.startPulsating(); }); // Mirror class to manage score display var Mirror = Container.expand(function () { var self = Container.call(this); var scoreGraphics = self.attachAsset('mirror1', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI / 36 // Rotate image left 5 degrees }); var scoreText = new Text2('0', { size: 175, fill: 0x800080, font: "Courier New, Courier, monospace", // Segmented clock style font stroke: 0x00FF00, strokeThickness: 15, dropShadow: true, dropShadowColor: 0x000000, dropShadowBlur: 5, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); scoreText.y = 160; // Move score text down by 110px scoreText.x = -155; // Move score text left by 10px scoreText.rotation = -65 * (Math.PI / 180); // Rotate score text left by 65 degrees x = 100; y = 100; self.addChild(scoreText); self.updateScore = function (newScore) { scoreText.setText(newScore); }; }); // Mirror1 class to represent the mirror image var Mirror1 = Container.expand(function () { var self = Container.call(this); var mirror1Graphics = self.attachAsset('mirror1', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(mirror1Graphics); }); // Reticle class to manage reticle behavior var Reticle = Container.expand(function () { var self = Container.call(this); var reticleGraphics = self.attachAsset('reticle1', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Add any specific update logic for the reticle here }; // Start the pulsating effect for the reticle startPulsating(self); }); // Reticle1 class to represent the reticle image var Reticle1 = Container.expand(function () { var self = Container.call(this); var reticle1Graphics = self.attachAsset('reticle1', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(reticle1Graphics); }); // Stack1 class to represent the stack1 image var Stack1 = Container.expand(function () { var self = Container.call(this); var stackGraphics = self.attachAsset('stack1', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI / -12 }); self.update = function () { // Add any specific update logic for stack1 here }; }); // ScoreManager class to manage score logic // Sun class to represent a sun in the top right corner var Sun = Container.expand(function () { var self = Container.call(this); var sunGraphics = self.attachAsset('sun', { anchorX: 0.5, anchorY: 0.5 }); self.pulse = new SunPulse(sunGraphics); self.pulse.startPulsating(); }); // Function to create a pulsating effect // Tree class to represent a tree with a 9:16 aspect ratio var Tree = Container.expand(function () { var self = Container.call(this); var treeGraphics = self.attachAsset('tree1', { anchorX: 0.5, anchorY: 1, antialias: true, // Apply antialias effect stroke: 0x000000, // Add a black stroke strokeThickness: 15 // Set stroke thickness to 15px }); self.update = function () { // Add any specific update logic for the tree here }; }); // Tree2 class to represent the second type of tree var Tree2 = Container.expand(function () { var self = Container.call(this); var tree2Graphics = self.attachAsset('tree2', { anchorX: 0.5, anchorY: 1 }); self.addChild(tree2Graphics); }); // UFO class to represent a UFO var UFO = Container.expand(function () { var self = Container.call(this); var ufoGraphics = self.attachAsset('ufo1', { anchorX: 0.5, anchorY: 0.5 }); self.movement = new UFOMovement(self, ufoGraphics); }); // UFO2 class to represent the second type of UFO var UFO2 = Container.expand(function () { var self = Container.call(this); var ufo2Graphics = self.attachAsset('ufo2', { anchorX: 0.5, anchorY: 0.5 }); self.movement = new UFOMovement(self, ufo2Graphics); }); /**** * Initialize Game ****/ // UFOMovement class to handle UFO movement /**** * Assets LK.init.shape('branch', {width:50, height:10, color:0x8b4513, shape:'box'}) ****/ // Initialize clouds array var game = new LK.Game({ // No title, no description backgroundColor: 0x87CEEB // Sky blue }); /**** * Game Code ****/ // Initialize the layer manager /**** * Layer Constants ****/ var LAYERS = { BACKGROUND: 0, EFFECTS: 1, SUN: 2, JET: 3, CLOUDS: 4, UFO: 5, BIRDS: 6, GRASS_BACK: 7, TREE: 8, STACK: 9, GRASS_FRONT: 10, CAT: 11, RETICLE: 12, LASER: 13, MIRROR: 14, UI: 15 }; /**** * Layer Management System ****/ var LayerManager = function LayerManager(gameInstance) { var self = this; self.game = gameInstance; self.layerMap = {}; // Maps objects to their layers /** * Add an object to a specific layer * @param {Object} object - The game object to add * @param {Number} layerId - The layer ID to add the object to */ self.addToLayer = function (object, layerId) { if (!object) { return; } // Add to game if not already added if (!self.game.children.includes(object)) { self.game.addChild(object); } // Set the object's index based on the layer self.setLayerIndex(object, layerId); // Store the layer information for this object self.layerMap[object.id] = layerId; return object; }; /** * Set the layer index for an object * @param {Object} object - The game object * @param {Number} layerId - The layer ID */ self.setLayerIndex = function (object, layerId) { if (!object || !self.game.children.includes(object)) { return; } // Ensure the layer ID is valid layerId = Math.max(0, Math.min(layerId, self.game.children.length - 1)); // Set the child index self.game.setChildIndex(object, layerId); // Update the layer map self.layerMap[object.id] = layerId; }; /** * Get the current layer of an object * @param {Object} object - The game object * @returns {Number} The layer ID or -1 if not found */ self.getObjectLayer = function (object) { if (!object || !object.id) { return -1; } return self.layerMap[object.id] || -1; }; /** * Move an object to a different layer * @param {Object} object - The game object * @param {Number} newLayerId - The new layer ID */ self.moveToLayer = function (object, newLayerId) { if (!object) { return; } self.setLayerIndex(object, newLayerId); }; /** * Get all objects in a specific layer * @param {Number} layerId - The layer ID * @returns {Array} Array of objects in the layer */ self.getObjectsInLayer = function (layerId) { var objects = []; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id) { objects.push(child); break; } } } } return objects; }; /** * Remove an object from layer tracking * @param {Object} object - The game object to remove */ self.removeFromTracking = function (object) { if (!object || !object.id) { return; } delete self.layerMap[object.id]; }; /** * Bring an object to the front of its layer * @param {Object} object - The game object */ self.bringToFrontOfLayer = function (object) { if (!object || !object.id) { return; } var layerId = self.getObjectLayer(object); if (layerId === -1) { return; } // Find the highest index within this layer var highestIndex = layerId; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id && i > highestIndex) { highestIndex = i; } } } } // Set the object to the highest index within its layer self.game.setChildIndex(object, highestIndex); }; /** * Send an object to the back of its layer * @param {Object} object - The game object */ self.sendToBackOfLayer = function (object) { if (!object || !object.id) { return; } var layerId = self.getObjectLayer(object); if (layerId === -1) { return; } // Find the lowest index within this layer var lowestIndex = layerId; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id && i < lowestIndex) { lowestIndex = i; } } } } // Set the object to the lowest index within its layer self.game.setChildIndex(object, lowestIndex); }; /** * Helper function to add multiple objects to a layer at once * @param {Array} objects - Array of game objects * @param {Number} layerId - The layer ID */ self.addMultipleToLayer = function (objects, layerId) { if (!Array.isArray(objects)) { return; } objects.forEach(function (object) { self.addToLayer(object, layerId); }); }; }; var layerManager = new LayerManager(game); // Using UFO sound ID as placeholder function flipImageVerticallyBasedOnDirection(graphics, lastX, currentX) { if (graphics) { if (lastX < currentX && graphics.scaleX < 0) { graphics.scaleX *= -1; // Flip horizontally to face right } else if (lastX > currentX && graphics.scaleX > 0) { graphics.scaleX *= -1; // Flip horizontally to face left } } } function flipImageBasedOnDirection(graphics, lastX, currentX) { if (graphics) { if (lastX < currentX) { graphics.scaleY = Math.abs(graphics.scaleY); // Face right when moving right by ensuring scaleY is positive } else if (lastX > currentX) { graphics.scaleY = -Math.abs(graphics.scaleY); // Face left when moving left by ensuring scaleY is negative } } } // SunPulse class to handle the pulsating effect for Sun function AABBIntersect(obj1, obj2) { if (obj1 && obj2) { return obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y; } return false; } var CLOUD_TYPES = ['cloud1', 'cloud2']; var SunPulse = function SunPulse(sunGraphics) { this.sunGraphics = sunGraphics; this.startPulsating = function () { function pulsate() { tween(this.sunGraphics, { scaleX: 1.1, scaleY: 1.1 }, { duration: 1000, easing: tween.easeInOut, onFinish: function () { tween(this.sunGraphics, { scaleX: 1.0, scaleY: 1.0 }, { duration: 1000, easing: tween.easeInOut, onFinish: pulsate.bind(this) }); }.bind(this) }); } pulsate.call(this); }; }; // Light1Pulse class to handle the pulsating effect for Light1 var Light1Pulse = function Light1Pulse(lightGraphics) { this.lightGraphics = lightGraphics; this.startPulsating = function () { function pulsate() { tween(this.lightGraphics, { scaleX: 1.6, scaleY: 1.6 }, { duration: 1000, easing: tween.easeInOut, onFinish: function () { tween(this.lightGraphics, { scaleX: 1.0, scaleY: 1.0 }, { duration: 1500, easing: tween.easeInOut, onFinish: pulsate.bind(this) }); }.bind(this) }); } pulsate.call(this); }; }; var bird2; // Define bird2 variable in the global scope // Bird2Effects class to handle effects and animations for Bird2 // Function to create a pulsating effect function startPulsating(target) { var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 888; function pulsate() { tween(target, { scaleX: 1.0, scaleY: 1.0 }, { duration: duration, easing: tween.easeInOut, onFinish: function onFinish() { tween(target, { scaleX: 1.0, scaleY: 1.0 }, { duration: duration, easing: tween.easeInOut, onFinish: pulsate }); } }); } pulsate(); } // UFOSound class to manage UFO sound effects // UFOMovement class to handle UFO movement var UFOMovement = function UFOMovement(ufo, ufoGraphics) { this.ufo = ufo; this.ufoGraphics = ufoGraphics; this.ufo.speed = 3.2; // Decrease speed by 20% this.ufo.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left this.ufo.lastX = this.ufo.x; // Track last X position for future checks this.ufo.update = function () { this.x += this.speed * this.direction; this.y = 100 + Math.sin(this.x / 200) * 250; // Slow down the wave pattern further // Stop playing ufo1 sound after UFO leaves the screen if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2 || this.lastX >= -this.width / 2 && this.x < -this.width / 2) { this.destroy(); ufo = null; LK.getSound('ufo1').stop(); } this.lastX = this.x; // Update lastX after movement }; }; var UFOSound = function UFOSound() { this.play = function () { LK.getSound('ufo1').play(); }; }; // BackgroundMusic class to manage background music var BackgroundMusic = function BackgroundMusic() { this.play = function () { LK.playMusic('bgm1', { loop: true, fade: { start: 0, end: 1, duration: 4000 }, onEnd: onBgm1End }); }; }; // LaserSound class to manage laser sound effects var LaserSound = function LaserSound() { this.play = function () { LK.getSound('laser1').play(); }; }; var Bird1Movement = function Bird1Movement(bird, birdGraphics) { this.bird = bird; this.birdGraphics = birdGraphics; this.bird.speed = Math.random() * 1.6 + 1; this.bird.lastY = storage.bird1_lastY || this.bird.y; // Initialize lastY for tracking changes on Y this.bird.lastX = storage.bird1_lastX || this.bird.x; // Initialize lastX for tracking changes on X this.bird.update = function () { this.y += this.speed; this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30% // Check if the bird has moved off-screen if (this.lastY <= 2732 && this.y > 2732) { this.y = -this.height; this.x = Math.random() < 0.33 ? Math.random() * 2048 : Math.random() < 0.5 ? 0 : 2048; } flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis this.lastX = this.x; // Track position for next update this.lastY = this.y; // Update lastY after movement storage.bird1_lastX = this.lastX; // Store lastX using storage plugin storage.bird1_lastY = this.lastY; // Store lastY using storage plugin }; }; var Bird2Movement = function Bird2Movement(bird, birdGraphics) { this.bird = bird; this.birdGraphics = birdGraphics; this.bird.speed = Math.random() * 1.6 + 1; this.bird.lastY = storage.bird2_lastY || this.bird.y; // Initialize lastY for tracking changes on Y this.bird.lastX = storage.bird2_lastX || this.bird.x; // Initialize lastX for tracking changes on X this.bird.update = function () { this.y += this.speed; this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30% flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis // Check if the bird has moved off-screen if (this.lastY <= 2732 && this.y > 2732) { this.y = Math.random() * 2732; // Random initial y position within the screen height this.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side } this.lastX = this.x; // Track position for next update this.lastY = this.y; // Update lastY after movement storage.bird2_lastX = this.lastX; // Store lastX using storage plugin storage.bird2_lastY = this.lastY; // Store lastY using storage plugin }; }; var CloudMovement = function CloudMovement(cloud, cloudGraphics) { this.cloud = cloud; this.cloudGraphics = cloudGraphics; this.cloud.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8 this.cloud.hasAccelerated = false; // Track if the cloud has already accelerated this.cloud.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left this.cloud.lastX = this.cloud.x; // Track last X position for future checks this.cloud.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections this.cloud.update = function () { flipImageVerticallyBasedOnDirection(this.cloudGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis this.x += this.speed * this.direction; // If the cloud moves off-screen, reposition it to the opposite side if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2) { this.x = -this.width / 2; } else if (this.lastX >= -this.width / 2 && this.x < -this.width / 2) { this.x = 2048 + this.width / 2; } // Check for overlap with other clouds for (var i = 0; i < clouds.length; i++) { if (clouds[i] !== this && this.intersects(clouds[i])) { if (!this.hasAccelerated) { this.speed *= 1.5; // Increase speed by 50% this.hasAccelerated = true; // Mark as accelerated } break; } else if (this.hasAccelerated && !this.intersects(clouds[i])) { this.speed /= 1.5; // Reset speed to original this.hasAccelerated = false; // Reset acceleration state } } // Check if the cloud intersects with the sun if (!this.lastIntersecting && this.intersects(sun)) { this.speed *= 2; // Double the speed when touching the sun if (this.cloudGraphics) { tween(this.cloudGraphics, { alpha: 0.5 }, { duration: 3000, easing: tween.linear }); } } else if (this.lastIntersecting && !this.intersects(sun)) { this.speed /= 2; // Reset speed to original when not touching the sun if (this.cloudGraphics) { tween(this.cloudGraphics, { alpha: 1.0 }, { duration: 3000, easing: tween.linear }); } } this.lastIntersecting = this.intersects(sun); this.lastX = this.x; // Update lastX after movement }; }; var Bird2Effects = function Bird2Effects(bird) { this.bird = bird; this.applyEffects = function () { // Add any specific effects or animations for Bird2 here }; }; // Bird1Effects class to handle effects and animations for Bird1 var Bird1Effects = function Bird1Effects(bird) { this.bird = bird; this.applyEffects = function () { // Add any specific effects or animations for Bird1 here }; }; // Add background to the BACKGROUND layer var background = new Background(); background.x = 2048 / 2; background.y = 2732 / 2 - 140; layerManager.addToLayer(background, LAYERS.BACKGROUND); // ScoreManager class to manage score logic var ScoreManager = function ScoreManager() { var self = this; self.score = 0; self.addScore = function (points) { self.score += points; return self.score; }; self.resetScore = function () { self.score = 0; }; self.getScore = function () { return self.score; }; }; var birds = []; game.down = function (x, y, obj) { if (!game.reticle) { game.reticle = game.addChild(new Reticle()); if (game.children.includes(game.reticle)) { game.setChildIndex(game.reticle, 7); // Set reticle to be rendered after the tree } } game.reticle.x = x; game.reticle.y = y; var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point game.addChild(laser); if (game.children.includes(laser) && game.children.includes(game.reticle)) { game.setChildIndex(laser, game.getChildIndex(game.reticle) + 1); // Ensure laser is rendered after the reticle } }; var VOLUME_BGM1 = 0.02; var VOLUME_BREEZE1 = 0.02; var VOLUME_CRICKET1 = 0.02; var VOLUME_FROG1 = 0.02; var VOLUME_SONGBIRD1 = 0.02; var VOLUME_UFO1 = 0.02; var VOLUME_WINGS1 = 0.02; game.move = function (x, y, obj) { if (!game.reticle) { game.reticle = game.addChild(new Reticle()); } if (obj.event) { var x = obj.event.x; var y = obj.event.y; } game.reticle.x = x; game.reticle.y = y; }; // Add a sun to the game in the top left corner var sun = new Sun(); sun.x = 480; sun.y = 680; layerManager.addToLayer(sun, LAYERS.SUN); var light1 = new Light1(); light1.x = 510; // Move light1 right by 50px light1.y = 1500; // Move light1 down by 210px layerManager.addToLayer(light1, LAYERS.EFFECTS); // Function to add a UFO to the game function addUFO() { if (!ufo) { ufo = new UFO(); game.addChild(ufo); } // Ensure UFO is added to the game before setting its index if (game.children.includes(ufo) && game.children.includes(clouds[0])) { game.setChildIndex(ufo, game.getChildIndex(clouds[0]) + 1); // Set UFO to be rendered after the first cloud } ufo.x = Math.random() < 0.5 ? 2048 + ufo.width / 2 : -ufo.width / 2; // Start from either the far right or left edge of the screen ufo.y = Math.random() * (2732 / 2 - ufo.height); // Random initial y position within the top half of the screen ufo.customUpdate = function () { ufo.update(); }; // Removed ufo sound playing at startup return ufo; } // Initialize clouds array var clouds = []; for (var i = 0; i < 3; i++) { // Add 3 clouds for variety var cloud = new Cloud(); cloud.x = Math.random() * 2048; cloud.y = Math.random() * (2732 / 2); // Position clouds in the upper half of the screen layerManager.addToLayer(cloud, LAYERS.CLOUDS); clouds.push(cloud); } for (var i = 0; i < 3; i++) { // Add 3 cloud2 for variety var cloud2 = new Cloud(); cloud2.x = Math.random() * 2048; cloud2.y = Math.random() * (2732 / 2); // Position cloud in the upper half of the screen layerManager.addToLayer(cloud2, LAYERS.CLOUDS); clouds.push(cloud2); } var bird1; // Define bird1 variable in the global scope var bird; // Define bird variable in the global scope // Initialize birds spawnBird1(); spawnBird2(); spawnBird2(); // Initialize a timer to add a UFO at a random time between 20 and 30 seconds var ufo; // Define the ufo variable in the global scope var laser; // Define the laser variable in the global scope var ufoTimer = LK.setTimeout(function () { addUFO(); // Reset the timer for the UFO to reappear between 10-20 seconds ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000); }, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds game.update = function () { // Update clouds for (var i = 0; i < clouds.length; i++) { clouds[i].update(); } // Add a jet to the game if it doesn't exist and no jet spawn timer is active if (!game.jet && !game.jetSpawnTimer) { var jet = new Jet1(); jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen game.addChild(jet); if (game.children.includes(jet) && game.children.includes(clouds[0])) { // Set jet to render after cloud1 but before cloud2 game.setChildIndex(jet, game.getChildIndex(clouds[0]) + 1); } game.jet = jet; if (!game.jetSoundPlayed) { LK.getSound('jetSound').play(); // Play jet sound when it appears game.jetSoundPlayed = true; // Ensure it only plays once } } // Update jet if (game.jet) { game.jet.update(); } if (ufo) { ufo.customUpdate(); // Update each laser game.children.forEach(function (child) { if (child instanceof Laser) { child.update(); // Check for intersections with birds and UFO birds.forEach(function (bird) { if (!child.lastIntersecting && AABBIntersect(child, bird)) { if (bird instanceof Bird1) { score += 5; } else if (bird instanceof Bird2 || bird instanceof Bird3) { score += 1; } scoreDisplay.updateScore(score); // Update the score display bird.destroy(); // Remove the bird when hit by a laser birds.splice(birds.indexOf(bird), 1); // Remove bird from the array child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }); }); if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) { score += 25; scoreDisplay.updateScore(score); child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }) || ufo && child.intersects(ufo); } }); // Check for intersections between UFO and birds birds.forEach(function (bird) { if (!ufo.lastIntersecting && ufo.intersects(bird)) { bird.destroy(); // Remove the bird when touched by the UFO birds.splice(birds.indexOf(bird), 1); // Remove bird from the array LK.getSound('electro').play(); // Play the 'electro' sound } ufo.lastIntersecting = birds.some(function (bird) { return ufo.intersects(bird); }); }); if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) { // Destroy the UFO and set it to null ufo.destroy(); ufo = null; // Start a timer for the UFO to reappear between 30-50 seconds ufoTimer = LK.setTimeout(function () { ufo = addUFO(); }, Math.random() * 20000 + 30000); } else {} ufo.lastX = ufo.x; // Update lastX for the UFO } // Removed auto-targeting update function }; // Function to spawn a third kind of bird function spawnBird1() { var bird1 = new Bird1(); bird1.x = Math.random() * 2048; bird1.y = -bird1.height; bird1.speed = 1 + Math.random() * 0.6; bird1.lastIntersecting = false; bird1.type = 'bird1'; // Specific property for Bird1 bird1.color = 0x746130; // Specific color for Bird1 LK.setTimeout(function () { game.addChild(bird1); // Ensure bird1 is added to the game before setting its index if (game.children.includes(bird1)) { if (game.children.includes(bird1) && game.children.includes(tree)) { game.setChildIndex(bird1, game.getChildIndex(tree)); // Set Bird1 to be rendered before tree } } birds.push(bird1); }, 2000); } function spawnBird2() { var bird = new Bird2(); bird.y = Math.random() * (2732 / 2); // Random initial y position in the top half bird.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side bird.speed = 1 + Math.random() * 0.6; bird.lastIntersecting = false; bird.type = 'bird2'; // Specific property for Bird2 bird.color = 0xFFFFFF; // Specific color for Bird2 LK.setTimeout(function () { game.addChild(bird); // Ensure bird is added to the game before setting its index if (game.children.includes(bird)) { game.setChildIndex(bird, game.getChildIndex(stack1) + 1); // Set Bird2 to be rendered after stack1 } birds.push(bird); }, 2000); } // Update the main game update function to include bird updates var originalUpdate = game.update; game.update = function () { // Call the original update function first originalUpdate.call(this); // Update each bird birds.forEach(function (bird) { bird.update(); // Check if the bird has moved off-screen if (bird.lastY <= 2732 && bird.y > 2732) { bird.y = -bird.height; // Respawn the bird at the top bird.x = Math.random() * 2048; // Randomize the x position bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird } }); // Update UFO and check for collisions if (ufo) { ufo.customUpdate(); // Update each laser game.children.forEach(function (child) { if (child instanceof Laser) { child.update(); // Check for intersections with birds and UFO birds.forEach(function (bird) { if (!child.lastIntersecting && AABBIntersect(child, bird)) { if (bird instanceof Bird1) { score += 5; } else if (bird instanceof Bird2 || bird instanceof Bird3) { score += 1; } scoreDisplay.updateScore(score); // Update the score display bird.destroy(); // Remove the bird when hit by a laser birds.splice(birds.indexOf(bird), 1); // Remove bird from the array child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }); }); if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) { score += 25; scoreDisplay.updateScore(score); child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }) || ufo && child.intersects(ufo); } }); } }; // Add a tree to the game var tree = game.addChild(new Tree()); tree.x = 2048 / 2 + 600; // Move the tree 500px to the right tree.y = 2500; // Position the tree on the grass // Ensure tree is added to the game before setting its index if (game.children.includes(tree) && game.children.includes(bird1)) { game.setChildIndex(tree, game.getChildIndex(bird1) + 1); // Set tree to be rendered after bird1 } var score = 0; // Add stack1 image to the game var stack1 = game.addChild(new Stack1()); stack1.x = 1700; // Move 700px to the right stack1.y = 2250; // Move down by 800px // Ensure stack1 is added to the game before setting its index if (game.children.includes(stack1) && game.children.includes(tree)) { game.setChildIndex(stack1, game.getChildIndex(tree) + 1); // Set stack1 to be rendered after tree } // Add the grass floor to the game var grassBack = new GrassBack(); grassBack.x = 1020; grassBack.y = 2735; // Position grassBack at the bottom game.addChild(grassBack); // Ensure grassBack is added to the game before setting its index if (game.children.includes(grassBack)) { game.setChildIndex(grassBack, 5); // Set grassBack to be rendered after Bird3 } var grassFront = new GrassFront(); grassFront.x = 1020; grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px game.addChild(grassFront); // Ensure grassFront is added to the game before setting its index if (game.children.includes(grassFront) && game.children.includes(bird2)) { game.setChildIndex(grassFront, game.getChildIndex(bird2) + 1); // Set grassFront to be rendered after bird2 } var scoreDisplay = new Mirror(); scoreDisplay.x = 2048 / 2 + 400; // Move the score display 400px to the right scoreDisplay.y = 2732 - 185 - 80 - 390 + 30; // Move the score display up by an additional 80px and down by 30px game.addChild(scoreDisplay); var mirror = new Mirror(); mirror.x = 2048 / 2 + 400; // Move the mirror 400px to the right mirror.y = 2732 - 185 - 80 - 390 + 30; // Move the mirror up by an additional 80px and down by 30px game.addChild(mirror); // Ensure mirror is added to the game before setting its index if (game.children.includes(mirror) && game.children.includes(stack1)) { game.setChildIndex(mirror, game.getChildIndex(stack1) + 1); // Set mirror to be rendered after stack1 } // Ensure laser is added to the game before setting mirror's index if (game.children.includes(laser) && game.children.includes(mirror)) { game.setChildIndex(mirror, game.getChildIndex(laser) + 1); // Set mirror to be rendered after the laser } // Function to handle bgm1 end event function onBgm1End() { // Set a timer to replay bgm1 after 50-80 seconds var bgmTimer = LK.setTimeout(function () { LK.playMusic('bgm1', { loop: true, fade: { start: 0, end: 1, duration: 4000 }, onEnd: onBgm1End }); }, Math.random() * 30000 + 50000); } ; // Add the cat to the game var cat = game.addChild(new Cat()); cat.x = 230; // Move the cat 20px to the left cat.y = 2732; // Position the cat at the bottom of the screen // Ensure cat is added to the game before setting its index if (game.children.includes(cat)) { game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value } // Play bgm1 once on load and set a timer to replay it every 20-50 seconds LK.playMusic('bgm1', { loop: false, // Play once fade: { start: 0, end: 0.02, // Set to the lowest volume duration: 4000 }, onEnd: function onEnd() { // Set a timer to replay bgm1 every 20-50 seconds LK.setTimeout(function () { LK.playMusic('bgm1', { loop: false, // Play once fade: { start: 0, end: 0.5, // Set to the lowest volume duration: 4000 }, onEnd: onEnd // Set the onEnd function to replay bgm1 }); }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds } }); // Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds var wingsTimer = LK.setTimeout(function () { LK.getSound('wings1').play(); wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000); }, Math.random() * 20000 + 10000); // Create an array for all sounds except ufo1, including all birdsong sounds var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1']; var currentAmbientSound = null; // Sound volumes configuration // Initialize random timers for each sound to play between 30-50 seconds sounds.forEach(function (soundId) { var sound = LK.getSound(soundId); var ambientSoundTimer = LK.setTimeout(function () { if (currentAmbientSound) { currentAmbientSound.stop(); // Stop the currently playing ambient sound } sound.play(); currentAmbientSound = sound; // Set the current ambient sound // Set a timeout to reset the current ambient sound after it finishes playing LK.setTimeout(function () { currentAmbientSound = null; }, sound.duration * 1000); // Convert duration from seconds to milliseconds // Reset the timer to play the sound again between 30-50 seconds, plus an additional 5 seconds ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000 + 5000); }, Math.random() * 20000 + 30000); }); /**** * Layer Constants ****/ var LAYERS = { BACKGROUND: 0, CLOUDS: 1, SUN: 2, BIRDS: 3, UFO: 4, TREE: 5, GRASS_BACK: 6, CAT: 7, GRASS_FRONT: 8, STACK: 9, LASER: 10, JET: 11, RETICLE: 12, UI: 13, EFFECTS: 14 }; /**** * Layer Management System ****/ var LayerManager = function LayerManager(gameInstance) { var self = this; self.game = gameInstance; self.layerMap = {}; // Maps objects to their layers /** * Add an object to a specific layer * @param {Object} object - The game object to add * @param {Number} layerId - The layer ID to add the object to */ self.addToLayer = function (object, layerId) { if (!object) { return; } // Add to game if not already added if (!self.game.children.includes(object)) { self.game.addChild(object); } // Set the object's index based on the layer self.setLayerIndex(object, layerId); // Store the layer information for this object self.layerMap[object.id] = layerId; return object; }; /** * Set the layer index for an object * @param {Object} object - The game object * @param {Number} layerId - The layer ID */ self.setLayerIndex = function (object, layerId) { if (!object || !self.game.children.includes(object)) { return; } // Ensure the layer ID is valid layerId = Math.max(0, Math.min(layerId, self.game.children.length - 1)); // Set the child index self.game.setChildIndex(object, layerId); // Update the layer map self.layerMap[object.id] = layerId; }; /** * Get the current layer of an object * @param {Object} object - The game object * @returns {Number} The layer ID or -1 if not found */ self.getObjectLayer = function (object) { if (!object || !object.id) { return -1; } return self.layerMap[object.id] || -1; }; /** * Move an object to a different layer * @param {Object} object - The game object * @param {Number} newLayerId - The new layer ID */ self.moveToLayer = function (object, newLayerId) { if (!object) { return; } self.setLayerIndex(object, newLayerId); }; /** * Get all objects in a specific layer * @param {Number} layerId - The layer ID * @returns {Array} Array of objects in the layer */ self.getObjectsInLayer = function (layerId) { var objects = []; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id) { objects.push(child); break; } } } } return objects; }; /** * Remove an object from layer tracking * @param {Object} object - The game object to remove */ self.removeFromTracking = function (object) { if (!object || !object.id) { return; } delete self.layerMap[object.id]; }; /** * Bring an object to the front of its layer * @param {Object} object - The game object */ self.bringToFrontOfLayer = function (object) { if (!object || !object.id) { return; } var layerId = self.getObjectLayer(object); if (layerId === -1) { return; } // Find the highest index within this layer var highestIndex = layerId; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id && i > highestIndex) { highestIndex = i; } } } } // Set the object to the highest index within its layer self.game.setChildIndex(object, highestIndex); }; /** * Send an object to the back of its layer * @param {Object} object - The game object */ self.sendToBackOfLayer = function (object) { if (!object || !object.id) { return; } var layerId = self.getObjectLayer(object); if (layerId === -1) { return; } // Find the lowest index within this layer var lowestIndex = layerId; for (var id in self.layerMap) { if (self.layerMap[id] === layerId) { // Find the object in the game's children for (var i = 0; i < self.game.children.length; i++) { var child = self.game.children[i]; if (child.id === id && i < lowestIndex) { lowestIndex = i; } } } } // Set the object to the lowest index within its layer self.game.setChildIndex(object, lowestIndex); }; /** * Helper function to add multiple objects to a layer at once * @param {Array} objects - Array of game objects * @param {Number} layerId - The layer ID */ self.addMultipleToLayer = function (objects, layerId) { if (!Array.isArray(objects)) { return; } objects.forEach(function (object) { self.addToLayer(object, layerId); }); }; }; // Initialize the layer manager var layerManager = new LayerManager(game); // Using UFO sound ID as placeholder var CLOUD_TYPES = ['cloud1', 'cloud2']; var SunPulse = function SunPulse(sunGraphics) { this.sunGraphics = sunGraphics; this.startPulsating = function () { function pulsate() { tween(this.sunGraphics, { scaleX: 1.1, scaleY: 1.1 }, { duration: 1000, easing: tween.easeInOut, onFinish: function () { tween(this.sunGraphics, { scaleX: 1.0, scaleY: 1.0 }, { duration: 1000, easing: tween.easeInOut, onFinish: pulsate.bind(this) }); }.bind(this) }); } pulsate.call(this); }; }; // Light1Pulse class to handle the pulsating effect for Light1 var Light1Pulse = function Light1Pulse(lightGraphics) { this.lightGraphics = lightGraphics; this.startPulsating = function () { function pulsate() { tween(this.lightGraphics, { scaleX: 1.6, scaleY: 1.6 }, { duration: 1000, easing: tween.easeInOut, onFinish: function () { tween(this.lightGraphics, { scaleX: 1.0, scaleY: 1.0 }, { duration: 1500, easing: tween.easeInOut, onFinish: pulsate.bind(this) }); }.bind(this) }); } pulsate.call(this); }; }; var bird2; // Define bird2 variable in the global scope // Bird2Effects class to handle effects and animations for Bird2 // Function to create a pulsating effect // UFOSound class to manage UFO sound effects // UFOMovement class to handle UFO movement var UFOMovement = function UFOMovement(ufo, ufoGraphics) { this.ufo = ufo; this.ufoGraphics = ufoGraphics; this.ufo.speed = 3.2; // Decrease speed by 20% this.ufo.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left this.ufo.lastX = this.ufo.x; // Track last X position for future checks this.ufo.update = function () { this.x += this.speed * this.direction; this.y = 100 + Math.sin(this.x / 200) * 250; // Slow down the wave pattern further // Stop playing ufo1 sound after UFO leaves the screen if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2 || this.lastX >= -this.width / 2 && this.x < -this.width / 2) { this.destroy(); ufo = null; LK.getSound('ufo1').stop(); } this.lastX = this.x; // Update lastX after movement }; }; var UFOSound = function UFOSound() { this.play = function () { LK.getSound('ufo1').play(); }; }; // BackgroundMusic class to manage background music var BackgroundMusic = function BackgroundMusic() { this.play = function () { LK.playMusic('bgm1', { loop: true, fade: { start: 0, end: 1, duration: 4000 }, onEnd: onBgm1End }); }; }; // LaserSound class to manage laser sound effects var LaserSound = function LaserSound() { this.play = function () { LK.getSound('laser1').play(); }; }; var Bird1Movement = function Bird1Movement(bird, birdGraphics) { this.bird = bird; this.birdGraphics = birdGraphics; this.bird.speed = Math.random() * 1.6 + 1; this.bird.lastY = storage.bird1_lastY || this.bird.y; // Initialize lastY for tracking changes on Y this.bird.lastX = storage.bird1_lastX || this.bird.x; // Initialize lastX for tracking changes on X this.bird.update = function () { this.y += this.speed; this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30% // Check if the bird has moved off-screen if (this.lastY <= 2732 && this.y > 2732) { this.y = -this.height; this.x = Math.random() < 0.33 ? Math.random() * 2048 : Math.random() < 0.5 ? 0 : 2048; } flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis this.lastX = this.x; // Track position for next update this.lastY = this.y; // Update lastY after movement storage.bird1_lastX = this.lastX; // Store lastX using storage plugin storage.bird1_lastY = this.lastY; // Store lastY using storage plugin }; }; var Bird2Movement = function Bird2Movement(bird, birdGraphics) { this.bird = bird; this.birdGraphics = birdGraphics; this.bird.speed = Math.random() * 1.6 + 1; this.bird.lastY = storage.bird2_lastY || this.bird.y; // Initialize lastY for tracking changes on Y this.bird.lastX = storage.bird2_lastX || this.bird.x; // Initialize lastX for tracking changes on X this.bird.update = function () { this.y += this.speed; this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30% flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis // Check if the bird has moved off-screen if (this.lastY <= 2732 && this.y > 2732) { this.y = Math.random() * 2732; // Random initial y position within the screen height this.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side } this.lastX = this.x; // Track position for next update this.lastY = this.y; // Update lastY after movement storage.bird2_lastX = this.lastX; // Store lastX using storage plugin storage.bird2_lastY = this.lastY; // Store lastY using storage plugin }; }; var CloudMovement = function CloudMovement(cloud, cloudGraphics) { this.cloud = cloud; this.cloudGraphics = cloudGraphics; this.cloud.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8 this.cloud.hasAccelerated = false; // Track if the cloud has already accelerated this.cloud.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left this.cloud.lastX = this.cloud.x; // Track last X position for future checks this.cloud.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections this.cloud.update = function () { flipImageVerticallyBasedOnDirection(this.cloudGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis this.x += this.speed * this.direction; // If the cloud moves off-screen, reposition it to the opposite side if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2) { this.x = -this.width / 2; } else if (this.lastX >= -this.width / 2 && this.x < -this.width / 2) { this.x = 2048 + this.width / 2; } // Check for overlap with other clouds for (var i = 0; i < clouds.length; i++) { if (clouds[i] !== this && this.intersects(clouds[i])) { if (!this.hasAccelerated) { this.speed *= 1.5; // Increase speed by 50% this.hasAccelerated = true; // Mark as accelerated } break; } else if (this.hasAccelerated && !this.intersects(clouds[i])) { this.speed /= 1.5; // Reset speed to original this.hasAccelerated = false; // Reset acceleration state } } // Check if the cloud intersects with the sun if (!this.lastIntersecting && this.intersects(sun)) { this.speed *= 2; // Double the speed when touching the sun if (this.cloudGraphics) { tween(this.cloudGraphics, { alpha: 0.5 }, { duration: 3000, easing: tween.linear }); } } else if (this.lastIntersecting && !this.intersects(sun)) { this.speed /= 2; // Reset speed to original when not touching the sun if (this.cloudGraphics) { tween(this.cloudGraphics, { alpha: 1.0 }, { duration: 3000, easing: tween.linear }); } } this.lastIntersecting = this.intersects(sun); this.lastX = this.x; // Update lastX after movement }; }; var Bird2Effects = function Bird2Effects(bird) { this.bird = bird; this.applyEffects = function () { // Add any specific effects or animations for Bird2 here }; }; // Bird1Effects class to handle effects and animations for Bird1 var Bird1Effects = function Bird1Effects(bird) { this.bird = bird; this.applyEffects = function () { // Add any specific effects or animations for Bird1 here }; }; // Add background to the BACKGROUND layer var background = new Background(); background.x = 2048 / 2; background.y = 2732 / 2 - 140; layerManager.addToLayer(background, LAYERS.BACKGROUND); // ScoreManager class to manage score logic var ScoreManager = function ScoreManager() { var self = this; self.score = 0; self.addScore = function (points) { self.score += points; return self.score; }; self.resetScore = function () { self.score = 0; }; self.getScore = function () { return self.score; }; }; var birds = []; game.down = function (x, y, obj) { if (!game.reticle) { game.reticle = game.addChild(new Reticle()); if (game.children.includes(game.reticle)) { game.setChildIndex(game.reticle, 7); // Set reticle to be rendered after the tree } } game.reticle.x = x; game.reticle.y = y; var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point game.addChild(laser); if (game.children.includes(laser) && game.children.includes(game.reticle)) { game.setChildIndex(laser, game.getChildIndex(game.reticle) + 1); // Ensure laser is rendered after the reticle } }; var VOLUME_BGM1 = 0.02; var VOLUME_BREEZE1 = 0.02; var VOLUME_CRICKET1 = 0.02; var VOLUME_FROG1 = 0.02; var VOLUME_SONGBIRD1 = 0.02; var VOLUME_UFO1 = 0.02; var VOLUME_WINGS1 = 0.02; game.move = function (x, y, obj) { if (!game.reticle) { game.reticle = game.addChild(new Reticle()); } if (obj.event) { var x = obj.event.x; var y = obj.event.y; } game.reticle.x = x; game.reticle.y = y; }; // Add a sun to the game in the top left corner var sun = new Sun(); sun.x = 480; sun.y = 680; layerManager.addToLayer(sun, LAYERS.SUN); var light1 = new Light1(); light1.x = 510; // Move light1 right by 50px light1.y = 1500; // Move light1 down by 210px layerManager.addToLayer(light1, LAYERS.EFFECTS); // Function to add a UFO to the game // Initialize clouds array var clouds = []; for (var i = 0; i < 3; i++) { // Add 3 clouds for variety var cloud = new Cloud(); cloud.x = Math.random() * 2048; cloud.y = Math.random() * (2732 / 2); // Position clouds in the upper half of the screen layerManager.addToLayer(cloud, LAYERS.CLOUDS); clouds.push(cloud); } for (var i = 0; i < 3; i++) { // Add 3 cloud2 for variety var cloud2 = new Cloud(); cloud2.x = Math.random() * 2048; cloud2.y = Math.random() * (2732 / 2); // Position cloud in the upper half of the screen layerManager.addToLayer(cloud2, LAYERS.CLOUDS); clouds.push(cloud2); } var bird1; // Define bird1 variable in the global scope var bird; // Define bird variable in the global scope // Initialize birds spawnBird1(); spawnBird2(); spawnBird2(); // Initialize a timer to add a UFO at a random time between 20 and 30 seconds var ufo; // Define the ufo variable in the global scope var laser; // Define the laser variable in the global scope var ufoTimer = LK.setTimeout(function () { addUFO(); // Reset the timer for the UFO to reappear between 10-20 seconds ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000); }, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds game.update = function () { // Update clouds for (var i = 0; i < clouds.length; i++) { clouds[i].update(); } // Add a jet to the game if it doesn't exist and no jet spawn timer is active if (!game.jet && !game.jetSpawnTimer) { var jet = new Jet1(); jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen game.addChild(jet); if (game.children.includes(jet) && game.children.includes(clouds[0])) { // Set jet to render after cloud1 but before cloud2 game.setChildIndex(jet, game.getChildIndex(clouds[0]) + 1); } game.jet = jet; if (!game.jetSoundPlayed) { LK.getSound('jet1').play(); // Play jet sound when it appears game.jetSoundPlayed = true; // Ensure it only plays once } } // Update jet if (game.jet) { game.jet.update(); } if (ufo) { ufo.customUpdate(); // Update each laser game.children.forEach(function (child) { if (child instanceof Laser) { child.update(); // Check for intersections with birds and UFO birds.forEach(function (bird) { if (!child.lastIntersecting && AABBIntersect(child, bird)) { if (bird instanceof Bird1) { score += 5; } else if (bird instanceof Bird2 || bird instanceof Bird3) { score += 1; } scoreDisplay.updateScore(score); // Update the score display LK.getSound('dead1').play(); // Play the 'dead1' sound when a bird is hit bird.destroy(); // Remove the bird when hit by a laser birds.splice(birds.indexOf(bird), 1); // Remove bird from the array child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }); }); if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) { score += 25; scoreDisplay.updateScore(score); child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }) || ufo && child.intersects(ufo); } }); // Check for intersections between UFO and birds birds.forEach(function (bird) { if (!ufo.lastIntersecting && ufo.intersects(bird)) { bird.destroy(); // Remove the bird when touched by the UFO birds.splice(birds.indexOf(bird), 1); // Remove bird from the array LK.getSound('electro').play(); // Play the 'electro' sound } ufo.lastIntersecting = birds.some(function (bird) { return ufo.intersects(bird); }); }); if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) { // Destroy the UFO and set it to null ufo.destroy(); ufo = null; // Start a timer for the UFO to reappear between 30-50 seconds ufoTimer = LK.setTimeout(function () { ufo = addUFO(); }, Math.random() * 20000 + 30000); } else {} ufo.lastX = ufo.x; // Update lastX for the UFO } // Removed auto-targeting update function }; // Function to spawn a third kind of bird // Update the main game update function to include bird updates var originalUpdate = game.update; game.update = function () { // Call the original update function first originalUpdate.call(this); // Update each bird birds.forEach(function (bird) { bird.update(); // Check if the bird has moved off-screen if (bird.lastY <= 2732 && bird.y > 2732) { bird.y = -bird.height; // Respawn the bird at the top bird.x = Math.random() * 2048; // Randomize the x position bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird } }); // Update UFO and check for collisions if (ufo) { ufo.customUpdate(); // Update each laser game.children.forEach(function (child) { if (child instanceof Laser) { child.update(); // Check for intersections with birds and UFO birds.forEach(function (bird) { if (!child.lastIntersecting && AABBIntersect(child, bird)) { if (bird instanceof Bird1) { score += 5; } else if (bird instanceof Bird2 || bird instanceof Bird3) { score += 1; } scoreDisplay.updateScore(score); // Update the score display bird.destroy(); // Remove the bird when hit by a laser birds.splice(birds.indexOf(bird), 1); // Remove bird from the array child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }); }); if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) { score += 25; scoreDisplay.updateScore(score); child.destroy(); } child.lastIntersecting = birds.some(function (bird) { return child.intersects(bird); }) || ufo && child.intersects(ufo); } }); } }; // Add a tree to the game var tree = game.addChild(new Tree()); tree.x = 2048 / 2 + 600; // Move the tree 500px to the right tree.y = 2500; // Position the tree on the grass // Ensure tree is added to the game before setting its index if (game.children.includes(tree) && game.children.includes(bird1)) { game.setChildIndex(tree, game.getChildIndex(bird1) + 1); // Set tree to be rendered after bird1 } var score = 0; // Add stack1 image to the game var stack1 = game.addChild(new Stack1()); stack1.x = 1700; // Move 700px to the right stack1.y = 2250; // Move down by 800px // Ensure stack1 is added to the game before setting its index if (game.children.includes(stack1) && game.children.includes(tree)) { game.setChildIndex(stack1, game.getChildIndex(tree) + 1); // Set stack1 to be rendered after tree } // Add the grass floor to the game var grassBack = new GrassBack(); grassBack.x = 1020; grassBack.y = 2735; // Position grassBack at the bottom game.addChild(grassBack); // Ensure grassBack is added to the game before setting its index if (game.children.includes(grassBack)) { game.setChildIndex(grassBack, 5); // Set grassBack to be rendered after Bird3 } var grassFront = new GrassFront(); grassFront.x = 1020; grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px game.addChild(grassFront); // Ensure grassFront is added to the game before setting its index if (game.children.includes(grassFront) && game.children.includes(bird2)) { game.setChildIndex(grassFront, game.getChildIndex(bird2) + 1); // Set grassFront to be rendered after bird2 } var scoreDisplay = new Mirror(); scoreDisplay.x = 2048 / 2 + 400; // Move the score display 400px to the right scoreDisplay.y = 2732 - 185 - 80 - 390 + 30; // Move the score display up by an additional 80px and down by 30px game.addChild(scoreDisplay); var mirror = new Mirror(); mirror.x = 2048 / 2 + 400; // Move the mirror 400px to the right mirror.y = 2732 - 185 - 80 - 390 + 30; // Move the mirror up by an additional 80px and down by 30px game.addChild(mirror); layerManager.addToLayer(mirror, LAYERS.MIRROR); // Ensure mirror is added to the game before setting its index if (game.children.includes(mirror) && game.children.includes(stack1)) { game.setChildIndex(mirror, game.getChildIndex(stack1) + 1); // Set mirror to be rendered after stack1 } // Ensure laser is added to the game before setting mirror's index if (game.children.includes(laser) && game.children.includes(mirror)) { game.setChildIndex(mirror, game.getChildIndex(laser) + 1); // Set mirror to be rendered after the laser } // Function to handle bgm1 end event ; // Add the cat to the game var cat = game.addChild(new Cat()); cat.x = 230; // Move the cat 20px to the left cat.y = 2732; // Position the cat at the bottom of the screen // Ensure cat is added to the game before setting its index if (game.children.includes(cat)) { game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value } // Play bgm1 once on load and set a timer to replay it every 20-50 seconds LK.playMusic('bgm1', { loop: false, // Play once fade: { start: 0, end: 0.02, // Set to the lowest volume duration: 4000 }, onEnd: function onEnd() { // Set a timer to replay bgm1 every 20-50 seconds LK.setTimeout(function () { LK.playMusic('bgm1', { loop: false, // Play once fade: { start: 0, end: 0.5, // Set to the lowest volume duration: 4000 }, onEnd: onEnd // Set the onEnd function to replay bgm1 }); }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds } }); // Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds var wingsTimer = LK.setTimeout(function () { LK.getSound('wings1').play(); wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000); }, Math.random() * 20000 + 10000); // Create an array for all sounds except ufo1, including all birdsong sounds var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1']; var currentAmbientSound = null; // Display volumes of all sounds in the console // Sound volumes configuration // Initialize random timers for each sound to play between 30-50 seconds sounds.forEach(function (soundId) { var sound = LK.getSound(soundId); var ambientSoundTimer = LK.setTimeout(function () { if (currentAmbientSound) { currentAmbientSound.stop(); // Stop the currently playing ambient sound } sound.play(); currentAmbientSound = sound; // Set the current ambient sound // Set a timeout to reset the current ambient sound after it finishes playing LK.setTimeout(function () { currentAmbientSound = null; }, sound.duration * 1000); // Convert duration from seconds to milliseconds // Reset the timer to play the sound again between 30-50 seconds, plus an additional 5 seconds ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000 + 5000); }, Math.random() * 20000 + 30000); });
===================================================================
--- original.js
+++ change.js
@@ -6,8 +6,9 @@
/****
* Classes
****/
+// Background class to represent the background image
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('background', {
anchorX: 0.5,
@@ -15,224 +16,243 @@
});
self.addChild(backgroundGraphics);
});
// Branch asset
+// Bird1 class to represent the first kind of bird
var Bird1 = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird1', {
anchorX: 0.5,
- anchorY: 0.5,
- hitArea: new Rectangle(-175, -172, 350, 344)
+ anchorY: 0.5
});
self.movement = new Bird1Movement(self, birdGraphics);
});
+// Bird2 class to represent the second kind of bird
var Bird2 = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird2', {
anchorX: 0.5,
- anchorY: 0.5,
- hitArea: new Rectangle(-125, -111, 250, 222)
+ anchorY: 0.5
});
self.movement = new Bird2Movement(self, birdGraphics);
});
+// Cat class to manage cat behavior
var Cat = Container.expand(function () {
var self = Container.call(this);
var catGraphics = self.attachAsset('cat', {
anchorX: 0.5,
anchorY: 1
});
- self.update = function () {};
+ self.update = function () {
+ // Add any specific update logic for the cat here
+ };
});
+// Cloud class to represent cloud behavior
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudType = CLOUD_TYPES[Math.floor(Math.random() * CLOUD_TYPES.length)];
var cloudGraphics = self.attachAsset(cloudType, {
anchorX: 0.5,
anchorY: 0.5,
- alpha: 0.8
+ alpha: 0.8 // Set default opacity to 80%
});
self.movement = new CloudMovement(self, cloudGraphics);
});
+// CloudMovement class to handle cloud movement and fade effects
var CloudMovement = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud1', {
anchorX: 0.5,
anchorY: 0.5,
- alpha: 0.8
+ alpha: 0.8 // Set default opacity to 80%
});
- self.speed = Math.random() * 0.55 + 0.25;
- self.hasAccelerated = false;
- self.direction = Math.random() < 0.5 ? 1 : -1;
- self.lastX = self.x;
- self.lastIntersecting = false;
+ self.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8
+ self.hasAccelerated = false; // Track if the cloud has already accelerated
+ self.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ self.lastX = self.x; // Track last X position for future checks
+ self.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections
self.update = function () {
- flipImageVerticallyBasedOnDirection(cloudGraphics, self.lastX, self.x);
+ flipImageVerticallyBasedOnDirection(cloudGraphics, self.lastX, self.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
self.x += self.speed * self.direction;
+ // If the cloud moves off-screen, reposition it to the opposite side
if (self.lastX <= 2048 + self.width / 2 && self.x > 2048 + self.width / 2) {
self.x = -self.width / 2;
} else if (self.lastX >= -self.width / 2 && self.x < -self.width / 2) {
self.x = 2048 + self.width / 2;
}
+ // Check for overlap with other clouds
for (var i = 0; i < clouds.length; i++) {
if (clouds[i] !== self && self.intersects(clouds[i])) {
if (!self.hasAccelerated) {
- self.speed *= 1.5;
- self.hasAccelerated = true;
+ self.speed *= 1.5; // Increase speed by 50%
+ self.hasAccelerated = true; // Mark as accelerated
}
break;
} else if (self.hasAccelerated && !self.intersects(clouds[i])) {
- self.speed /= 1.5;
- self.hasAccelerated = false;
+ self.speed /= 1.5; // Reset speed to original
+ self.hasAccelerated = false; // Reset acceleration state
}
}
+ // Check if the cloud intersects with the sun
if (!self.lastIntersecting && self.intersects(sun)) {
- self.speed *= 2;
+ self.speed *= 2; // Double the speed when touching the sun
tween(cloudGraphics, {
alpha: 0.5
}, {
duration: 3000,
easing: tween.linear
});
} else if (self.lastIntersecting && !self.intersects(sun)) {
- self.speed /= 2;
+ self.speed /= 2; // Reset speed to original when not touching the sun
tween(cloudGraphics, {
alpha: 1.0
}, {
duration: 3000,
easing: tween.linear
});
}
self.lastIntersecting = self.intersects(sun);
- self.lastX = self.x;
+ self.lastX = self.x; // Update lastX after movement
};
});
+// GrassBack class to represent the grass image
var GrassBack = Container.expand(function () {
var self = Container.call(this);
var grassGraphics = self.attachAsset('grass-back', {
anchorX: 0.5,
anchorY: 1
});
- self.addChild(grassGraphics);
self.update = function () {
- self.x += Math.sin((LK.ticks + 100) / 90) * 0.15; // Swaying effect
+ // Add any specific update logic for the grass here
+ // Example: Slightly move the grass to simulate wind effect
+ self.x += Math.sin((LK.ticks + 100) / 90) * 0.15; // Swaying effect, increased speed
};
+ self.addChild(grassGraphics); // Add grass graphics to the container
});
+// GrassFront class to represent the second grass image
var GrassFront = Container.expand(function () {
var self = Container.call(this);
var grass2Graphics = self.attachAsset('grass-front', {
anchorX: 0.5,
anchorY: 1
});
- self.lastX = self.x;
- self.addChild(grass2Graphics);
+ self.lastX = self.x; // Initialize lastX for tracking changes on X
+ self.addChild(grass2Graphics); // Add grass2 graphics to the container
self.update = function () {
- self.x += Math.sin((LK.ticks + Math.random() * 50) / 100) * 0.125;
- self.y += Math.cos((LK.ticks + Math.random() * 50) / 130) * 0.08;
- self.lastX = self.x;
+ // Add any specific update logic for the grass here
+ // Example: Slightly move the grass to simulate wind effect
+ self.x += Math.sin((LK.ticks + 50) / 100) * 0.125; // Swaying effect, reduced by 50%
+ self.lastX = self.x; // Update lastX after movement
};
});
+// Jet1 class to represent a jet flying across the screen
var Jet1 = Container.expand(function () {
var self = Container.call(this);
var jetGraphics = self.attachAsset('jet1', {
anchorX: 0.5,
anchorY: 0.5,
- flipX: 1
+ flipX: 1 // Apply the same directional image flipping logic as bird1 and bird2
});
+ // Add a label to the jet
var jetLabel = new Text2('Jet', {
size: 50,
fill: 0xFFFFFF
});
- jetLabel.anchor.set(0.5, 0.5);
- jetLabel.y = -60;
+ jetLabel.anchor.set(0.5, 0.5); // Center the label on the jet
+ jetLabel.y = -60; // Position the label above the jet
self.addChild(jetLabel);
- self.speed = 10;
- self.direction = Math.random() < 0.5 ? 1 : -1;
- self.lastX = self.x;
- self.x = Math.random() < 0.5 ? 2048 + self.width / 2 : -self.width / 2;
+ self.speed = 10; // Set the speed of the jet
+ self.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ self.lastX = self.x; // Track last X position for future checks
+ self.x = Math.random() < 0.5 ? 2048 + self.width / 2 : -self.width / 2; // Start from either the far right or left edge of the screen
self.update = function () {
- flipImageVerticallyBasedOnDirection(jetGraphics, self.lastX, self.x);
+ flipImageVerticallyBasedOnDirection(jetGraphics, self.lastX, self.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
self.x += self.speed * self.direction;
+ // If the jet moves off-screen, destroy it and set it to null
if (!game.jetSoundPlayed) {
- LK.getSound('jet1').play();
- game.jetSoundPlayed = true;
+ LK.getSound('jet1').play(); // Play jet1 sound when the jet is on screen
+ game.jetSoundPlayed = true; // Ensure it only plays once
}
if (self.lastX <= 2048 + self.width / 2 && self.x > 2048 + self.width / 2 || self.lastX >= -self.width / 2 && self.x < -self.width / 2) {
self.destroy();
game.jet = null;
- game.jetSoundPlayed = false;
- var respawnTime = Math.random() * 10000 + 10000;
+ game.jetSoundPlayed = false; // Reset the flag when the jet is removed
+ // Set a timer to respawn the jet between 10-20 seconds
+ var respawnTime = Math.random() * 10000 + 10000; // Random time between 10-20 seconds
game.jetSpawnTimer = LK.setTimeout(function () {
var jet = new Jet1();
- jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2;
- jet.y = Math.random() * (2732 / 2);
+ jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen
+ jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen
game.addChild(jet);
game.jet = jet;
if (!game.jetSoundPlayed) {
- LK.getSound('jetSound').play();
- game.jetSoundPlayed = true;
+ LK.getSound('jetSound').play(); // Play jet sound when it appears
+ game.jetSoundPlayed = true; // Ensure it only plays once
}
- game.jetSpawnTimer = null;
+ game.jetSpawnTimer = null; // Clear the timer reference
}, respawnTime);
}
- self.lastX = self.x;
+ self.lastX = self.x; // Update lastX after movement
};
});
+// Laser class to represent a laser shot from the cat
var Laser = Container.expand(function (startX, startY, targetX, targetY) {
var self = Container.call(this);
var laserGraphics = self.attachAsset('laser2', {
anchorX: 0.5,
anchorY: 0.5,
- brightness: 2.0
+ brightness: 2.0 // Increase brightness by 200%
});
+ // Play the laser1 sound when the laser is created
LK.getSound('laser1').play();
+ // Calculate direction and speed
var dx = targetX - startX;
var dy = targetY - startY;
var distance = Math.sqrt(dx * dx + dy * dy);
- var speed = 60;
+ var speed = 60; // Speed of the laser increased by 200%
self.vx = dx / distance * speed;
self.vy = dy / distance * speed;
+ // Set initial position
self.x = startX;
self.y = startY;
+ // Update function to move the laser
self.update = function () {
game.children.forEach(function (child) {
- if (child instanceof Bird1 || child instanceof Bird2) {
- if (AABBIntersect(child, bird)) {
- // Visual feedback
- var debugGraphics = new Graphics();
- debugGraphics.lineStyle(2, 0xFF0000, 1);
- debugGraphics.drawRect(bird.x, bird.y, bird.width, bird.height);
- game.addChild(debugGraphics);
- LK.setTimeout(function () {
- game.removeChild(debugGraphics);
- }, 100);
- // Play hit sound
- LK.getSound('dead1').play();
- // Remove both laser and bird
- child.destroy();
- self.destroy();
- }
+ if (AABBIntersect(child, bird)) {
+ // Debug visualization: Draw a rectangle around the intersecting objects
+ var debugGraphics = new Graphics();
+ debugGraphics.lineStyle(2, 0xFF0000, 1);
+ debugGraphics.drawRect(bird.x, bird.y, bird.width, bird.height);
+ game.addChild(debugGraphics);
+ LK.setTimeout(function () {
+ game.removeChild(debugGraphics);
+ }, 100); // Remove debug visualization after 100ms
}
- });
+ }); // Close the loop for iterating over game children
self.x += self.vx;
self.y += self.vy;
+ // Remove laser if it goes off-screen
if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
self.destroy();
}
};
});
+// Light1 class to represent a light object
var Light1 = Container.expand(function () {
var self = Container.call(this);
var lightGraphics = self.attachAsset('light1', {
anchorX: 0.5,
anchorY: 0.5,
- alpha: 0.2
+ alpha: 0.2 // Set transparency to 20%
});
- self.x = 450;
- self.y = 450;
- self.update = function () {};
+ self.x = 450, self.y = 450;
+ self.update = function () {
+ // Add any specific update logic for the light here
+ };
self.pulse = new Light1Pulse(lightGraphics);
self.pulse.startPulsating();
});
+// Mirror class to manage score display
var Mirror = Container.expand(function () {
var self = Container.call(this);
var scoreGraphics = self.attachAsset('mirror1', {
anchorX: 0.5,
@@ -242,47 +262,72 @@
var scoreText = new Text2('0', {
size: 175,
fill: 0x800080,
font: "Courier New, Courier, monospace",
+ // Segmented clock style font
stroke: 0x00FF00,
strokeThickness: 15,
dropShadow: true,
dropShadowColor: 0x000000,
dropShadowBlur: 5,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6
});
- scoreText.y = 50; // Move score text up by 20px
- scoreText.x = -145; // Move score text left by 10px
- scoreText.rotation = Math.PI / 36; // Rotate score text to match the mirror's angle
- self.addChild(scoreGraphics);
+ scoreText.y = 160; // Move score text down by 110px
+ scoreText.x = -155; // Move score text left by 10px
+ scoreText.rotation = -65 * (Math.PI / 180); // Rotate score text left by 65 degrees
+ x = 100;
+ y = 100;
self.addChild(scoreText);
self.updateScore = function (newScore) {
- scoreText.text = newScore.toString();
+ scoreText.setText(newScore);
};
});
+// Mirror1 class to represent the mirror image
+var Mirror1 = Container.expand(function () {
+ var self = Container.call(this);
+ var mirror1Graphics = self.attachAsset('mirror1', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.addChild(mirror1Graphics);
+});
+// Reticle class to manage reticle behavior
var Reticle = Container.expand(function () {
var self = Container.call(this);
var reticleGraphics = self.attachAsset('reticle1', {
anchorX: 0.5,
anchorY: 0.5
});
- self.addChild(reticleGraphics);
self.update = function () {
- // Add reticle movement logic here
- self.x = game.input.mouseX;
- self.y = game.input.mouseY;
+ // Add any specific update logic for the reticle here
};
+ // Start the pulsating effect for the reticle
+ startPulsating(self);
});
+// Reticle1 class to represent the reticle image
+var Reticle1 = Container.expand(function () {
+ var self = Container.call(this);
+ var reticle1Graphics = self.attachAsset('reticle1', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.addChild(reticle1Graphics);
+});
+// Stack1 class to represent the stack1 image
var Stack1 = Container.expand(function () {
var self = Container.call(this);
var stackGraphics = self.attachAsset('stack1', {
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / -12
});
- self.update = function () {};
+ self.update = function () {
+ // Add any specific update logic for stack1 here
+ };
});
+// ScoreManager class to manage score logic
+// Sun class to represent a sun in the top right corner
var Sun = Container.expand(function () {
var self = Container.call(this);
var sunGraphics = self.attachAsset('sun', {
anchorX: 0.5,
@@ -290,71 +335,61 @@
});
self.pulse = new SunPulse(sunGraphics);
self.pulse.startPulsating();
});
+// Function to create a pulsating effect
+// Tree class to represent a tree with a 9:16 aspect ratio
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree1', {
anchorX: 0.5,
anchorY: 1,
antialias: true,
+ // Apply antialias effect
stroke: 0x000000,
- strokeThickness: 15
+ // Add a black stroke
+ strokeThickness: 15 // Set stroke thickness to 15px
});
- self.update = function () {};
+ self.update = function () {
+ // Add any specific update logic for the tree here
+ };
});
+// Tree2 class to represent the second type of tree
var Tree2 = Container.expand(function () {
var self = Container.call(this);
var tree2Graphics = self.attachAsset('tree2', {
anchorX: 0.5,
anchorY: 1
});
- self.update = function () {};
+ self.addChild(tree2Graphics);
});
+// UFO class to represent a UFO
var UFO = Container.expand(function () {
var self = Container.call(this);
var ufoGraphics = self.attachAsset('ufo1', {
anchorX: 0.5,
- anchorY: 0.5,
- hitArea: new Rectangle(-175, -65, 350, 130)
+ anchorY: 0.5
});
self.movement = new UFOMovement(self, ufoGraphics);
});
+// UFO2 class to represent the second type of UFO
var UFO2 = Container.expand(function () {
var self = Container.call(this);
var ufo2Graphics = self.attachAsset('ufo2', {
anchorX: 0.5,
anchorY: 0.5
});
self.movement = new UFOMovement(self, ufo2Graphics);
});
-// Bird2Effects class to handle effects and animations for Bird2
-// Function to create a pulsating effect
-// UFOSound class to manage UFO sound effects
-// UFOMovement class to handle UFO movement
-var UFOMovement = Container.expand(function (ufo, ufoGraphics) {
- var self = Container.call(this);
- self.ufo = ufo;
- self.ufoGraphics = ufoGraphics;
- self.speed = 3.2;
- self.direction = Math.random() < 0.5 ? 1 : -1;
- self.lastX = self.x;
- self.update = function () {
- flipImageVerticallyBasedOnDirection(self.ufoGraphics, self.lastX, self.x);
- self.x += self.speed * self.direction;
- self.y = 100 + Math.sin(self.x / 100) * 250;
- if (self.lastX <= 2048 + self.width / 2 && self.x > 2048 + self.width / 2 || self.lastX >= -self.width / 2 && self.x < -self.width / 2) {
- LK.getSound('ufo1').stop();
- self.destroy();
- game.ufo = null;
- }
- self.lastX = self.x;
- };
-});
/****
* Initialize Game
****/
+// UFOMovement class to handle UFO movement
+/****
+* Assets
+LK.init.shape('branch', {width:50, height:10, color:0x8b4513, shape:'box'})
+****/
// Initialize clouds array
var game = new LK.Game({
// No title, no description
backgroundColor: 0x87CEEB // Sky blue
@@ -362,1444 +397,1607 @@
/****
* Game Code
****/
-// Constants
-// Plugins
-// Sounds
-// Assets
+// Initialize the layer manager
/****
-* Layer Management System
+* Layer Constants
****/
var LAYERS = {
BACKGROUND: 0,
- CLOUDS: 1,
- CLOUD1: 1,
- CLOUD2: 2,
- BUILDINGS: 2,
- ENEMIES: 3,
- BIRD1: 3,
- BIRD2: 3,
- UFO: 3,
+ EFFECTS: 1,
+ SUN: 2,
JET: 3,
- PLAYER: 4,
- CAT: 4,
- PROJECTILES: 5,
- LASER: 5,
- EFFECTS: 5,
- UI: 6,
- RETICLE: 6,
- SUN: 1,
- TREE: 2,
- STACK: 2,
- GRASS_BACK: 5,
- GRASS_FRONT: 6
+ CLOUDS: 4,
+ UFO: 5,
+ BIRDS: 6,
+ GRASS_BACK: 7,
+ TREE: 8,
+ STACK: 9,
+ GRASS_FRONT: 10,
+ CAT: 11,
+ RETICLE: 12,
+ LASER: 13,
+ MIRROR: 14,
+ UI: 15
};
-// Initialize game
-function initGame() {
- var background = LK.getAsset('background', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- game.addChild(background);
- game.setChildIndex(background, 0);
- var sun = game.addChild(new Sun());
- sun.x = 530;
- sun.y = 450;
- game.setChildIndex(sun, 1);
- var light1 = game.addChild(new Light1());
- light1.x = 530;
- light1.y = 1500;
- game.setChildIndex(light1, 2);
- for (var i = 0; i < 6; i++) {
- var cloud = new Cloud();
- cloud.x = Math.random() * 2048;
- cloud.y = Math.random() * (2732 / 2);
- game.addChild(cloud);
- game.setChildIndex(cloud, 2);
- clouds.push(cloud);
- }
- var ufoTimer = LK.setTimeout(function () {
- if (!game.ufo && !game.ufoSpawnTimer) {
- var ufo = new UFO();
- ufo.x = Math.random() < 0.5 ? 2048 + ufo.width / 2 : -ufo.width / 2;
- ufo.y = Math.random() * (2732 / 2 - ufo.height);
- game.addChild(ufo);
- game.setChildIndex(ufo, game.getChildIndex(clouds[0]) + 1);
- game.ufo = ufo;
+/****
+* Layer Management System
+****/
+var LayerManager = function LayerManager(gameInstance) {
+ var self = this;
+ self.game = gameInstance;
+ self.layerMap = {}; // Maps objects to their layers
+ /**
+ * Add an object to a specific layer
+ * @param {Object} object - The game object to add
+ * @param {Number} layerId - The layer ID to add the object to
+ */
+ self.addToLayer = function (object, layerId) {
+ if (!object) {
+ return;
}
- game.ufoSpawnTimer = LK.setTimeout(function () {
- game.ufoSpawnTimer = null;
- }, Math.random() * 10000 + 10000);
- }, Math.random() * 10000 + 10000);
- if (!game.jet && !game.jetSpawnTimer) {
- var jet = new Jet1();
- jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2;
- jet.y = Math.random() * (2732 - jet.height);
- game.addChild(jet);
- game.setChildIndex(jet, game.children.length - 1);
- game.jet = jet;
- if (!game.jetSoundPlayed) {
- LK.getSound('jetSound').play();
- game.jetSoundPlayed = true;
+ // Add to game if not already added
+ if (!self.game.children.includes(object)) {
+ self.game.addChild(object);
}
- }
- var tree = game.addChild(new Tree());
- tree.x = 2048 / 2 + 600;
- tree.y = 2500;
- game.setChildIndex(tree, game.getChildIndex(bird1) + 1);
- var stack1 = game.addChild(new Stack1());
- stack1.x = 1700;
- stack1.y = 2250;
- game.setChildIndex(stack1, game.getChildIndex(tree) + 1);
- var grassBack = game.addChild(new GrassBack());
- grassBack.y = 2735;
- game.setChildIndex(grassBack, 5);
- var grassFront = game.addChild(new GrassFront());
- grassFront.y = 2785;
- game.setChildIndex(grassFront, game.getChildIndex(bird2) + 1);
- var mirror = game.addChild(new Mirror());
- mirror.x = 2048 / 2 + 400;
- mirror.y = 2732 - 185 - 80 - 390 + 30; // Position mirror correctly
- game.setChildIndex(mirror, game.getChildIndex(tree) + 1);
- var LayerManager = function LayerManager(gameInstance) {
- var self = this;
- self.game = gameInstance;
- self.layerMap = {}; // Maps objects to their layers
- /**
- * Add an object to a specific layer
- * @param {Object} object - The game object to add
- * @param {Number} layerId - The layer ID to add the object to
- */
- self.addToLayer = function (object, layerId) {
- if (!object) {
- return;
- }
- // Add to game if not already added
- if (!self.game.children.includes(object)) {
- self.game.addChild(object);
- }
- // Set the object's index based on the layer
- self.setLayerIndex(object, layerId);
- // Store the layer information for this object
- self.layerMap[object.id] = layerId;
- return object;
- };
- /**
- * Set the layer index for an object
- * @param {Object} object - The game object
- * @param {Number} layerId - The layer ID
- */
- self.setLayerIndex = function (object, layerId) {
- if (!object || !self.game.children.includes(object)) {
- return;
- }
- // Ensure the layer ID is valid
- layerId = Math.max(0, Math.min(layerId, self.game.children.length - 1));
- // Set the child index
- game.setChildIndex(object, layerId);
- // Update the layer map
- self.layerMap[object.id] = layerId;
- };
- /**
- * Get the current layer of an object
- * @param {Object} object - The game object
- * @returns {Number} The layer ID or -1 if not found
- */
- self.getObjectLayer = function (object) {
- if (!object || !object.id) {
- return -1;
- }
- return self.layerMap[object.id] || -1;
- };
- /**
- * Move an object to a different layer
- * @param {Object} object - The game object
- * @param {Number} newLayerId - The new layer ID
- */
- self.moveToLayer = function (object, newLayerId) {
- if (!object) {
- return;
- }
- self.setLayerIndex(object, newLayerId);
- };
- /**
- * Get all objects in a specific layer
- * @param {Number} layerId - The layer ID
- * @returns {Array} Array of objects in the layer
- */
- self.getObjectsInLayer = function (layerId) {
- var objects = [];
- for (var id in self.layerMap) {
- if (self.layerMap[id] === layerId) {
- // Find the object in the game's children
- for (var i = 0; i < self.game.children.length; i++) {
- var child = self.game.children[i];
- if (child.id === id) {
- objects.push(child);
- break;
- }
+ // Set the object's index based on the layer
+ self.setLayerIndex(object, layerId);
+ // Store the layer information for this object
+ self.layerMap[object.id] = layerId;
+ return object;
+ };
+ /**
+ * Set the layer index for an object
+ * @param {Object} object - The game object
+ * @param {Number} layerId - The layer ID
+ */
+ self.setLayerIndex = function (object, layerId) {
+ if (!object || !self.game.children.includes(object)) {
+ return;
+ }
+ // Ensure the layer ID is valid
+ layerId = Math.max(0, Math.min(layerId, self.game.children.length - 1));
+ // Set the child index
+ self.game.setChildIndex(object, layerId);
+ // Update the layer map
+ self.layerMap[object.id] = layerId;
+ };
+ /**
+ * Get the current layer of an object
+ * @param {Object} object - The game object
+ * @returns {Number} The layer ID or -1 if not found
+ */
+ self.getObjectLayer = function (object) {
+ if (!object || !object.id) {
+ return -1;
+ }
+ return self.layerMap[object.id] || -1;
+ };
+ /**
+ * Move an object to a different layer
+ * @param {Object} object - The game object
+ * @param {Number} newLayerId - The new layer ID
+ */
+ self.moveToLayer = function (object, newLayerId) {
+ if (!object) {
+ return;
+ }
+ self.setLayerIndex(object, newLayerId);
+ };
+ /**
+ * Get all objects in a specific layer
+ * @param {Number} layerId - The layer ID
+ * @returns {Array} Array of objects in the layer
+ */
+ self.getObjectsInLayer = function (layerId) {
+ var objects = [];
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id) {
+ objects.push(child);
+ break;
}
}
}
- return objects;
- };
- /**
- * Remove an object from layer tracking
- * @param {Object} object - The game object to remove
- */
- self.removeFromTracking = function (object) {
- if (!object || !object.id) {
- return;
- }
- delete self.layerMap[object.id];
- };
- /**
- * Bring an object to the front of its layer
- * @param {Object} object - The game object
- */
- self.bringToFrontOfLayer = function (object) {
- if (!object || !object.id) {
- return;
- }
- var layerId = self.getObjectLayer(object);
- if (layerId === -1) {
- return;
- }
- // Find the highest index within this layer
- var highestIndex = layerId;
- for (var id in self.layerMap) {
- if (self.layerMap[id] === layerId) {
- // Find the object in the game's children
- for (var i = 0; i < self.game.children.length; i++) {
- var child = self.game.children[i];
- if (child.id === id && i > highestIndex) {
- highestIndex = i;
- }
+ }
+ return objects;
+ };
+ /**
+ * Remove an object from layer tracking
+ * @param {Object} object - The game object to remove
+ */
+ self.removeFromTracking = function (object) {
+ if (!object || !object.id) {
+ return;
+ }
+ delete self.layerMap[object.id];
+ };
+ /**
+ * Bring an object to the front of its layer
+ * @param {Object} object - The game object
+ */
+ self.bringToFrontOfLayer = function (object) {
+ if (!object || !object.id) {
+ return;
+ }
+ var layerId = self.getObjectLayer(object);
+ if (layerId === -1) {
+ return;
+ }
+ // Find the highest index within this layer
+ var highestIndex = layerId;
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id && i > highestIndex) {
+ highestIndex = i;
}
}
}
- // Set the object to the highest index within its layer
- self.game.setChildIndex(object, highestIndex);
- };
- /**
- * Send an object to the back of its layer
- * @param {Object} object - The game object
- */
- self.sendToBackOfLayer = function (object) {
- if (!object || !object.id) {
- return;
- }
- var layerId = self.getObjectLayer(object);
- if (layerId === -1) {
- return;
- }
- // Find the lowest index within this layer
- var lowestIndex = layerId;
- for (var id in self.layerMap) {
- if (self.layerMap[id] === layerId) {
- // Find the object in the game's children
- for (var i = 0; i < self.game.children.length; i++) {
- var child = self.game.children[i];
- if (child.id === id && i < lowestIndex) {
- lowestIndex = i;
- }
+ }
+ // Set the object to the highest index within its layer
+ self.game.setChildIndex(object, highestIndex);
+ };
+ /**
+ * Send an object to the back of its layer
+ * @param {Object} object - The game object
+ */
+ self.sendToBackOfLayer = function (object) {
+ if (!object || !object.id) {
+ return;
+ }
+ var layerId = self.getObjectLayer(object);
+ if (layerId === -1) {
+ return;
+ }
+ // Find the lowest index within this layer
+ var lowestIndex = layerId;
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id && i < lowestIndex) {
+ lowestIndex = i;
}
}
}
- // Set the object to the lowest index within its layer
- self.game.setChildIndex(object, lowestIndex);
- };
- }; // Add missing closing brace for LayerManager class
- var layerManager = new LayerManager(game);
- // Using UFO sound ID as placeholder
- function flipImageVerticallyBasedOnDirection(graphics, lastX, currentX) {
+ }
+ // Set the object to the lowest index within its layer
+ self.game.setChildIndex(object, lowestIndex);
+ };
+ /**
+ * Helper function to add multiple objects to a layer at once
+ * @param {Array} objects - Array of game objects
+ * @param {Number} layerId - The layer ID
+ */
+ self.addMultipleToLayer = function (objects, layerId) {
+ if (!Array.isArray(objects)) {
+ return;
+ }
+ objects.forEach(function (object) {
+ self.addToLayer(object, layerId);
+ });
+ };
+};
+var layerManager = new LayerManager(game);
+// Using UFO sound ID as placeholder
+function flipImageVerticallyBasedOnDirection(graphics, lastX, currentX) {
+ if (graphics) {
+ if (lastX < currentX && graphics.scaleX < 0) {
+ graphics.scaleX *= -1; // Flip horizontally to face right
+ } else if (lastX > currentX && graphics.scaleX > 0) {
+ graphics.scaleX *= -1; // Flip horizontally to face left
+ }
+ }
+}
+function flipImageBasedOnDirection(graphics, lastX, currentX) {
+ if (graphics) {
if (lastX < currentX) {
- graphics.flipX = false;
+ graphics.scaleY = Math.abs(graphics.scaleY); // Face right when moving right by ensuring scaleY is positive
} else if (lastX > currentX) {
- graphics.flipX = true;
+ graphics.scaleY = -Math.abs(graphics.scaleY); // Face left when moving left by ensuring scaleY is negative
}
}
- function flipImageBasedOnDirection(graphics, lastX, currentX) {
- if (graphics) {
- if (lastX < currentX) {
- graphics.scaleY = Math.abs(graphics.scaleY); // Face right when moving right by ensuring scaleY is positive
- } else if (lastX > currentX) {
- graphics.scaleY = -Math.abs(graphics.scaleY); // Face left when moving left by ensuring scaleY is negative
- }
- }
+}
+// SunPulse class to handle the pulsating effect for Sun
+function AABBIntersect(obj1, obj2) {
+ if (obj1 && obj2) {
+ return obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y;
}
- // SunPulse class to handle the pulsating effect for Sun
- function AABBIntersect(a, b) {
- return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
- }
- var CLOUD_TYPES = ['cloud1', 'cloud2'];
- var SunPulse = function SunPulse(sunGraphics) {
- this.sunGraphics = sunGraphics;
- this.startPulsating = function () {
- function pulsate() {
- tween(this.sunGraphics, {
- scaleX: 1.1,
- scaleY: 1.1
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: function () {
- tween(this.sunGraphics, {
- scaleX: 1.0,
- scaleY: 1.0
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: pulsate.bind(this)
- });
- }.bind(this)
- });
- }
- pulsate.call(this);
- };
+ return false;
+}
+var CLOUD_TYPES = ['cloud1', 'cloud2'];
+var SunPulse = function SunPulse(sunGraphics) {
+ this.sunGraphics = sunGraphics;
+ this.startPulsating = function () {
+ function pulsate() {
+ tween(this.sunGraphics, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: function () {
+ tween(this.sunGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: pulsate.bind(this)
+ });
+ }.bind(this)
+ });
+ }
+ pulsate.call(this);
};
- // Light1Pulse class to handle the pulsating effect for Light1
- var Light1Pulse = function Light1Pulse(lightGraphics) {
- this.lightGraphics = lightGraphics;
- this.startPulsating = function () {
- function pulsate() {
- tween(this.lightGraphics, {
- scaleX: 1.6,
- scaleY: 1.6
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: function () {
- tween(this.lightGraphics, {
- scaleX: 1.0,
- scaleY: 1.0
- }, {
- duration: 1500,
- easing: tween.easeInOut,
- onFinish: pulsate.bind(this)
- });
- }.bind(this)
- });
- }
- pulsate.call(this);
- };
+};
+// Light1Pulse class to handle the pulsating effect for Light1
+var Light1Pulse = function Light1Pulse(lightGraphics) {
+ this.lightGraphics = lightGraphics;
+ this.startPulsating = function () {
+ function pulsate() {
+ tween(this.lightGraphics, {
+ scaleX: 1.6,
+ scaleY: 1.6
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: function () {
+ tween(this.lightGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 1500,
+ easing: tween.easeInOut,
+ onFinish: pulsate.bind(this)
+ });
+ }.bind(this)
+ });
+ }
+ pulsate.call(this);
};
- function startPulsating(target) {
- var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 888;
+};
+var bird2; // Define bird2 variable in the global scope
+// Bird2Effects class to handle effects and animations for Bird2
+// Function to create a pulsating effect
+function startPulsating(target) {
+ var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 888;
+ function pulsate() {
tween(target, {
- scale: 1.1
+ scaleX: 1.0,
+ scaleY: 1.0
}, {
duration: duration,
- easing: tween.easeInOutQuad,
- onComplete: function onComplete() {
+ easing: tween.easeInOut,
+ onFinish: function onFinish() {
tween(target, {
- scale: 1
+ scaleX: 1.0,
+ scaleY: 1.0
}, {
duration: duration,
- easing: tween.easeInOutQuad,
- onComplete: function onComplete() {
- startPulsating(target, duration);
- }
+ easing: tween.easeInOut,
+ onFinish: pulsate
});
}
});
}
- var UFOSound = function UFOSound() {
- var sound = LK.getSound('ufo1');
- this.play = function () {
- if (sound) {
- sound.play();
- }
- };
- this.stop = function () {
- if (sound) {
- sound.stop();
- }
- };
+ pulsate();
+}
+// UFOSound class to manage UFO sound effects
+// UFOMovement class to handle UFO movement
+var UFOMovement = function UFOMovement(ufo, ufoGraphics) {
+ this.ufo = ufo;
+ this.ufoGraphics = ufoGraphics;
+ this.ufo.speed = 3.2; // Decrease speed by 20%
+ this.ufo.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ this.ufo.lastX = this.ufo.x; // Track last X position for future checks
+ this.ufo.update = function () {
+ this.x += this.speed * this.direction;
+ this.y = 100 + Math.sin(this.x / 200) * 250; // Slow down the wave pattern further
+ // Stop playing ufo1 sound after UFO leaves the screen
+ if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2 || this.lastX >= -this.width / 2 && this.x < -this.width / 2) {
+ this.destroy();
+ ufo = null;
+ LK.getSound('ufo1').stop();
+ }
+ this.lastX = this.x; // Update lastX after movement
};
- // BackgroundMusic class to manage background music
- var BackgroundMusic = function BackgroundMusic() {
- var bgm = LK.getSound('bgm1');
- var breeze = LK.getSound('breeze1');
- this.play = function () {
- if (bgm) {
- bgm.play();
- }
- if (breeze) {
- breeze.play();
- }
- };
- this.stop = function () {
- if (bgm) {
- bgm.stop();
- }
- if (breeze) {
- breeze.stop();
- }
- };
+};
+var UFOSound = function UFOSound() {
+ this.play = function () {
+ LK.getSound('ufo1').play();
};
- // LaserSound class to manage laser sound effects
- var LaserSound = function LaserSound() {
- var sound = LK.getSound('laser1');
- this.play = function () {
- if (sound) {
- sound.play();
- }
- };
+};
+// BackgroundMusic class to manage background music
+var BackgroundMusic = function BackgroundMusic() {
+ this.play = function () {
+ LK.playMusic('bgm1', {
+ loop: true,
+ fade: {
+ start: 0,
+ end: 1,
+ duration: 4000
+ },
+ onEnd: onBgm1End
+ });
};
- var Bird1Movement = function Bird1Movement(bird, birdGraphics) {
- this.bird = bird;
- this.birdGraphics = birdGraphics;
- this.bird.lastY = this.bird.y;
- this.bird.lastX = this.bird.x;
- this.update = function () {
- this.x += Math.sin(this.y / 100) * 6.5;
- if (this.x < 0 || this.x > 2048) {
- if (this.x < 0) {
- this.birdGraphics.flipX = false;
- } else {
- this.birdGraphics.flipX = true;
- }
- this.x = Math.random() * 2048;
- }
- this.lastX = this.x;
- this.lastY = this.y;
- };
+};
+// LaserSound class to manage laser sound effects
+var LaserSound = function LaserSound() {
+ this.play = function () {
+ LK.getSound('laser1').play();
};
- var Bird2Movement = function Bird2Movement(bird, birdGraphics) {
- this.bird = bird;
- this.birdGraphics = birdGraphics;
- this.bird.lastY = this.bird.y;
- this.bird.lastX = this.bird.x;
- this.update = function () {
- this.x += Math.sin(this.y / 100) * 6.5;
- if (this.x < 0) {
- this.birdGraphics.flipX = false;
- } else {
- this.birdGraphics.flipX = true;
- }
- this.lastX = this.x;
- if (this.x < 0 || this.x > 2048) {
- this.y = Math.random() * 2732;
- this.x = Math.random() < 0.5 ? 0 : 2048;
- }
- this.lastY = this.y;
- };
+};
+var Bird1Movement = function Bird1Movement(bird, birdGraphics) {
+ this.bird = bird;
+ this.birdGraphics = birdGraphics;
+ this.bird.speed = Math.random() * 1.6 + 1;
+ this.bird.lastY = storage.bird1_lastY || this.bird.y; // Initialize lastY for tracking changes on Y
+ this.bird.lastX = storage.bird1_lastX || this.bird.x; // Initialize lastX for tracking changes on X
+ this.bird.update = function () {
+ this.y += this.speed;
+ this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30%
+ // Check if the bird has moved off-screen
+ if (this.lastY <= 2732 && this.y > 2732) {
+ this.y = -this.height;
+ this.x = Math.random() < 0.33 ? Math.random() * 2048 : Math.random() < 0.5 ? 0 : 2048;
+ }
+ flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ this.lastX = this.x; // Track position for next update
+ this.lastY = this.y; // Update lastY after movement
+ storage.bird1_lastX = this.lastX; // Store lastX using storage plugin
+ storage.bird1_lastY = this.lastY; // Store lastY using storage plugin
};
- var CloudMovement = function CloudMovement(cloud, cloudGraphics) {
- if (!cloud || !cloudGraphics) {
- throw new Error("Cloud and cloudGraphics must be provided");
+};
+var Bird2Movement = function Bird2Movement(bird, birdGraphics) {
+ this.bird = bird;
+ this.birdGraphics = birdGraphics;
+ this.bird.speed = Math.random() * 1.6 + 1;
+ this.bird.lastY = storage.bird2_lastY || this.bird.y; // Initialize lastY for tracking changes on Y
+ this.bird.lastX = storage.bird2_lastX || this.bird.x; // Initialize lastX for tracking changes on X
+ this.bird.update = function () {
+ this.y += this.speed;
+ this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30%
+ flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ // Check if the bird has moved off-screen
+ if (this.lastY <= 2732 && this.y > 2732) {
+ this.y = Math.random() * 2732; // Random initial y position within the screen height
+ this.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side
}
- this.cloud = cloud;
- this.cloudGraphics = cloudGraphics;
- this.speed = Math.random() * 0.55 + 0.25;
- this.hasAccelerated = false;
- this.direction = Math.random() < 0.5 ? 1 : -1;
- this.lastX = this.cloud.x;
- this.lastIntersecting = false;
- this.update = function () {
- try {
- this.cloud.x += this.speed * this.direction;
- if (this.cloud.x <= -this.cloud.width / 2) {
- this.cloud.x = 2048 + this.cloud.width / 2;
- this.direction = -1;
- this.cloudGraphics.flipX = true;
- } else if (this.cloud.x >= 2048 + this.cloud.width / 2) {
- this.cloud.x = -this.cloud.width / 2;
- this.direction = 1;
- this.cloudGraphics.flipX = false;
+ this.lastX = this.x; // Track position for next update
+ this.lastY = this.y; // Update lastY after movement
+ storage.bird2_lastX = this.lastX; // Store lastX using storage plugin
+ storage.bird2_lastY = this.lastY; // Store lastY using storage plugin
+ };
+};
+var CloudMovement = function CloudMovement(cloud, cloudGraphics) {
+ this.cloud = cloud;
+ this.cloudGraphics = cloudGraphics;
+ this.cloud.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8
+ this.cloud.hasAccelerated = false; // Track if the cloud has already accelerated
+ this.cloud.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ this.cloud.lastX = this.cloud.x; // Track last X position for future checks
+ this.cloud.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections
+ this.cloud.update = function () {
+ flipImageVerticallyBasedOnDirection(this.cloudGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ this.x += this.speed * this.direction;
+ // If the cloud moves off-screen, reposition it to the opposite side
+ if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2) {
+ this.x = -this.width / 2;
+ } else if (this.lastX >= -this.width / 2 && this.x < -this.width / 2) {
+ this.x = 2048 + this.width / 2;
+ }
+ // Check for overlap with other clouds
+ for (var i = 0; i < clouds.length; i++) {
+ if (clouds[i] !== this && this.intersects(clouds[i])) {
+ if (!this.hasAccelerated) {
+ this.speed *= 1.5; // Increase speed by 50%
+ this.hasAccelerated = true; // Mark as accelerated
}
- } catch (error) {
- console.error("Error updating cloud movement:", error);
+ break;
+ } else if (this.hasAccelerated && !this.intersects(clouds[i])) {
+ this.speed /= 1.5; // Reset speed to original
+ this.hasAccelerated = false; // Reset acceleration state
}
- };
- };
- var Bird2Effects = function Bird2Effects(bird) {
- if (!bird) {
- throw new Error("Bird must be provided");
}
- this.bird = bird;
- this.applyEffects = function () {
- // Add any specific effects or animations for Bird2 here
- };
+ // Check if the cloud intersects with the sun
+ if (!this.lastIntersecting && this.intersects(sun)) {
+ this.speed *= 2; // Double the speed when touching the sun
+ if (this.cloudGraphics) {
+ tween(this.cloudGraphics, {
+ alpha: 0.5
+ }, {
+ duration: 3000,
+ easing: tween.linear
+ });
+ }
+ } else if (this.lastIntersecting && !this.intersects(sun)) {
+ this.speed /= 2; // Reset speed to original when not touching the sun
+ if (this.cloudGraphics) {
+ tween(this.cloudGraphics, {
+ alpha: 1.0
+ }, {
+ duration: 3000,
+ easing: tween.linear
+ });
+ }
+ }
+ this.lastIntersecting = this.intersects(sun);
+ this.lastX = this.x; // Update lastX after movement
};
- // Add background to the BACKGROUND layer
- var background = new Background();
- background.x = 2048 / 2;
- background.y = 2732 / 2 - 140;
- layerManager.addToLayer(background, LAYERS.BACKGROUND);
- // ScoreManager class to manage score logic
- var ScoreManager = function ScoreManager() {
- var self = this;
- var score = 0;
- self.addScore = function (points) {
- score += points;
- return score;
- };
- self.resetScore = function () {
- score = 0;
- return score;
- };
- self.getScore = function () {
- return score;
- };
+};
+var Bird2Effects = function Bird2Effects(bird) {
+ this.bird = bird;
+ this.applyEffects = function () {
+ // Add any specific effects or animations for Bird2 here
};
- var birds = [];
- game.down = function (x, y, obj) {
- if (!game.reticle) {
- game.reticle = new Reticle();
- layerManager.addToLayer(game.reticle, LAYERS.RETICLE);
- }
- game.reticle.x = x;
- game.reticle.y = y;
- var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point
- layerManager.addToLayer(laser, LAYERS.LASER);
+};
+// Bird1Effects class to handle effects and animations for Bird1
+var Bird1Effects = function Bird1Effects(bird) {
+ this.bird = bird;
+ this.applyEffects = function () {
+ // Add any specific effects or animations for Bird1 here
};
- var VOLUME_BGM1 = 0.02;
- var VOLUME_BREEZE1 = 0.02;
- var VOLUME_CRICKET1 = 0.02;
- var VOLUME_FROG1 = 0.02;
- var VOLUME_SONGBIRD1 = 0.02;
- var VOLUME_UFO1 = 0.02;
- var VOLUME_WINGS1 = 0.02;
- game.move = function (x, y, obj) {
- if (!game.reticle) {
- game.reticle = new Reticle();
- layerManager.addToLayer(game.reticle, LAYERS.RETICLE);
- }
- if (obj.event) {
- var x = obj.event.x;
- var y = obj.event.y;
- }
- game.reticle.x = x;
- game.reticle.y = y;
+};
+// Add background to the BACKGROUND layer
+var background = new Background();
+background.x = 2048 / 2;
+background.y = 2732 / 2 - 140;
+layerManager.addToLayer(background, LAYERS.BACKGROUND);
+// ScoreManager class to manage score logic
+var ScoreManager = function ScoreManager() {
+ var self = this;
+ self.score = 0;
+ self.addScore = function (points) {
+ self.score += points;
+ return self.score;
};
- // Add a sun to the game in the top left corner
- var sun = new Sun();
- sun.x = 480;
- sun.y = 680;
- layerManager.addToLayer(sun, LAYERS.SUN);
- var light1 = new Light1();
- light1.x = 510; // Move light1 right by 50px
- light1.y = 1500; // Move light1 down by 210px
- layerManager.addToLayer(light1, LAYERS.EFFECTS);
- // Function to add a UFO to the game
- function addUFO() {
- if (!ufo) {
- ufo = new UFO();
- layerManager.addToLayer(ufo, LAYERS.UFO);
+ self.resetScore = function () {
+ self.score = 0;
+ };
+ self.getScore = function () {
+ return self.score;
+ };
+};
+var birds = [];
+game.down = function (x, y, obj) {
+ if (!game.reticle) {
+ game.reticle = game.addChild(new Reticle());
+ if (game.children.includes(game.reticle)) {
+ game.setChildIndex(game.reticle, 7); // Set reticle to be rendered after the tree
}
- ufo.x = Math.random() < 0.5 ? 2048 + ufo.width / 2 : -ufo.width / 2; // Start from either the far right or left edge of the screen
- ufo.y = Math.random() * (2732 / 2 - ufo.height); // Random initial y position within the top half of the screen
- ufo.customUpdate = function () {
- ufo.update();
- };
- // Removed ufo sound playing at startup
- return ufo;
}
- // Initialize clouds array
- var clouds = [];
- for (var i = 0; i < 6; i++) {
- var cloud = new Cloud();
- cloud.x = Math.random() * 2048;
- cloud.y = Math.random() * (2732 / 2);
- layerManager.addToLayer(cloud, i < 3 ? LAYERS.CLOUD1 : LAYERS.CLOUD2);
- clouds.push(cloud);
+ game.reticle.x = x;
+ game.reticle.y = y;
+ var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point
+ game.addChild(laser);
+ if (game.children.includes(laser) && game.children.includes(game.reticle)) {
+ game.setChildIndex(laser, game.getChildIndex(game.reticle) + 1); // Ensure laser is rendered after the reticle
}
- var bird1; // Define bird1 variable in the global scope
- var bird; // Define bird variable in the global scope
- // Initialize birds
- spawnBird1();
- spawnBird2();
- spawnBird2();
- // Initialize a timer to add a UFO at a random time between 20 and 30 seconds
- var ufo; // Define the ufo variable in the global scope
- var laser; // Define the laser variable in the global scope
- var ufoTimer = LK.setTimeout(function () {
- addUFO();
- // Reset the timer for the UFO to reappear between 10-20 seconds
- ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000);
- }, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds
- game.update = function () {
- // Update clouds
- for (var i = 0; i < clouds.length; i++) {
- clouds[i].update();
+};
+var VOLUME_BGM1 = 0.02;
+var VOLUME_BREEZE1 = 0.02;
+var VOLUME_CRICKET1 = 0.02;
+var VOLUME_FROG1 = 0.02;
+var VOLUME_SONGBIRD1 = 0.02;
+var VOLUME_UFO1 = 0.02;
+var VOLUME_WINGS1 = 0.02;
+game.move = function (x, y, obj) {
+ if (!game.reticle) {
+ game.reticle = game.addChild(new Reticle());
+ }
+ if (obj.event) {
+ var x = obj.event.x;
+ var y = obj.event.y;
+ }
+ game.reticle.x = x;
+ game.reticle.y = y;
+};
+// Add a sun to the game in the top left corner
+var sun = new Sun();
+sun.x = 480;
+sun.y = 680;
+layerManager.addToLayer(sun, LAYERS.SUN);
+var light1 = new Light1();
+light1.x = 510; // Move light1 right by 50px
+light1.y = 1500; // Move light1 down by 210px
+layerManager.addToLayer(light1, LAYERS.EFFECTS);
+// Function to add a UFO to the game
+function addUFO() {
+ if (!ufo) {
+ ufo = new UFO();
+ game.addChild(ufo);
+ }
+ // Ensure UFO is added to the game before setting its index
+ if (game.children.includes(ufo) && game.children.includes(clouds[0])) {
+ game.setChildIndex(ufo, game.getChildIndex(clouds[0]) + 1); // Set UFO to be rendered after the first cloud
+ }
+ ufo.x = Math.random() < 0.5 ? 2048 + ufo.width / 2 : -ufo.width / 2; // Start from either the far right or left edge of the screen
+ ufo.y = Math.random() * (2732 / 2 - ufo.height); // Random initial y position within the top half of the screen
+ ufo.customUpdate = function () {
+ ufo.update();
+ };
+ // Removed ufo sound playing at startup
+ return ufo;
+}
+// Initialize clouds array
+var clouds = [];
+for (var i = 0; i < 3; i++) {
+ // Add 3 clouds for variety
+ var cloud = new Cloud();
+ cloud.x = Math.random() * 2048;
+ cloud.y = Math.random() * (2732 / 2); // Position clouds in the upper half of the screen
+ layerManager.addToLayer(cloud, LAYERS.CLOUDS);
+ clouds.push(cloud);
+}
+for (var i = 0; i < 3; i++) {
+ // Add 3 cloud2 for variety
+ var cloud2 = new Cloud();
+ cloud2.x = Math.random() * 2048;
+ cloud2.y = Math.random() * (2732 / 2); // Position cloud in the upper half of the screen
+ layerManager.addToLayer(cloud2, LAYERS.CLOUDS);
+ clouds.push(cloud2);
+}
+var bird1; // Define bird1 variable in the global scope
+var bird; // Define bird variable in the global scope
+// Initialize birds
+spawnBird1();
+spawnBird2();
+spawnBird2();
+// Initialize a timer to add a UFO at a random time between 20 and 30 seconds
+var ufo; // Define the ufo variable in the global scope
+var laser; // Define the laser variable in the global scope
+var ufoTimer = LK.setTimeout(function () {
+ addUFO();
+ // Reset the timer for the UFO to reappear between 10-20 seconds
+ ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000);
+}, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds
+game.update = function () {
+ // Update clouds
+ for (var i = 0; i < clouds.length; i++) {
+ clouds[i].update();
+ }
+ // Add a jet to the game if it doesn't exist and no jet spawn timer is active
+ if (!game.jet && !game.jetSpawnTimer) {
+ var jet = new Jet1();
+ jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen
+ jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen
+ game.addChild(jet);
+ if (game.children.includes(jet) && game.children.includes(clouds[0])) {
+ // Set jet to render after cloud1 but before cloud2
+ game.setChildIndex(jet, game.getChildIndex(clouds[0]) + 1);
}
- // Add a jet to the game if it doesn't exist and no jet spawn timer is active
- if (!game.jet && !game.jetSpawnTimer) {
- var jet = new Jet1();
- jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen
- jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen
- layerManager.addToLayer(jet, LAYERS.JET);
- game.jet = jet;
- if (!game.jetSoundPlayed) {
- LK.getSound('jetSound').play(); // Play jet sound when it appears
- game.jetSoundPlayed = true; // Ensure it only plays once
- }
+ game.jet = jet;
+ if (!game.jetSoundPlayed) {
+ LK.getSound('jetSound').play(); // Play jet sound when it appears
+ game.jetSoundPlayed = true; // Ensure it only plays once
}
- // Update jet
- if (game.jet) {
- game.jet.update();
- }
- if (ufo) {
- ufo.customUpdate();
- // Update each laser
- game.children.forEach(function (child) {
- if (child instanceof Laser) {
- child.update();
- // Check for intersections with birds and UFO
- birds.forEach(function (bird) {
- if (!child.lastIntersecting && AABBIntersect(child, bird)) {
- if (bird instanceof Bird1) {
- score += 5;
- } else if (bird instanceof Bird2 || bird instanceof Bird3) {
- score += 1;
- }
- scoreDisplay.updateScore(score); // Update the score display
- bird.destroy(); // Remove the bird when hit by a laser
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- child.destroy();
+ }
+ // Update jet
+ if (game.jet) {
+ game.jet.update();
+ }
+ if (ufo) {
+ ufo.customUpdate();
+ // Update each laser
+ game.children.forEach(function (child) {
+ if (child instanceof Laser) {
+ child.update();
+ // Check for intersections with birds and UFO
+ birds.forEach(function (bird) {
+ if (!child.lastIntersecting && AABBIntersect(child, bird)) {
+ if (bird instanceof Bird1) {
+ score += 5;
+ } else if (bird instanceof Bird2 || bird instanceof Bird3) {
+ score += 1;
}
- child.lastIntersecting = birds.some(function (bird) {
- return child.intersects(bird);
- });
- });
- if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
- score += 25;
- scoreDisplay.updateScore(score);
+ scoreDisplay.updateScore(score); // Update the score display
+ bird.destroy(); // Remove the bird when hit by a laser
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
child.destroy();
}
child.lastIntersecting = birds.some(function (bird) {
return child.intersects(bird);
- }) || ufo && child.intersects(ufo);
- }
- });
- // Check for intersections between UFO and birds
- birds.forEach(function (bird) {
- if (!ufo.lastIntersecting && ufo.intersects(bird)) {
- bird.destroy(); // Remove the bird when touched by the UFO
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- LK.getSound('electro').play(); // Play the 'electro' sound
- }
- ufo.lastIntersecting = birds.some(function (bird) {
- return ufo.intersects(bird);
+ });
});
- });
- if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) {
- // Destroy the UFO and set it to null
- ufo.destroy();
- ufo = null;
- // Start a timer for the UFO to reappear between 30-50 seconds
- ufoTimer = LK.setTimeout(function () {
- ufo = addUFO();
- }, Math.random() * 20000 + 30000);
- } else {}
- ufo.lastX = ufo.x; // Update lastX for the UFO
- }
- // Removed auto-targeting update function
- };
- // Function to spawn a third kind of bird
- function spawnBird1() {
- var bird1 = new Bird1();
- bird1.x = Math.random() * 2048;
- bird1.y = -bird1.height;
- bird1.speed = 1 + Math.random() * 0.6;
- bird1.lastIntersecting = false;
- bird1.type = 'bird1'; // Specific property for Bird1
- bird1.color = 0x746130; // Specific color for Bird1
- LK.setTimeout(function () {
- layerManager.addToLayer(bird1, LAYERS.BIRD1);
- birds.push(bird1);
- }, 2000);
- }
- function spawnBird2() {
- var bird = new Bird2();
- bird.y = Math.random() * (2732 / 2); // Random initial y position in the top half
- bird.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side
- bird.speed = 1 + Math.random() * 0.6;
- bird.lastIntersecting = false;
- bird.type = 'bird2'; // Specific property for Bird2
- bird.color = 0xFFFFFF; // Specific color for Bird2
- LK.setTimeout(function () {
- layerManager.addToLayer(bird, LAYERS.BIRD2);
- birds.push(bird);
- }, 2000);
- }
- // Update the main game update function to include bird updates
- var originalUpdate = game.update;
- game.update = function () {
- // Call the original update function first
- originalUpdate.call(this);
- // Update each bird
+ if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
+ score += 25;
+ scoreDisplay.updateScore(score);
+ child.destroy();
+ }
+ child.lastIntersecting = birds.some(function (bird) {
+ return child.intersects(bird);
+ }) || ufo && child.intersects(ufo);
+ }
+ });
+ // Check for intersections between UFO and birds
birds.forEach(function (bird) {
- bird.update();
- // Check if the bird has moved off-screen
- if (bird.lastY <= 2732 && bird.y > 2732) {
- bird.y = -bird.height; // Respawn the bird at the top
- bird.x = Math.random() * 2048; // Randomize the x position
- bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird
+ if (!ufo.lastIntersecting && ufo.intersects(bird)) {
+ bird.destroy(); // Remove the bird when touched by the UFO
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
+ LK.getSound('electro').play(); // Play the 'electro' sound
}
+ ufo.lastIntersecting = birds.some(function (bird) {
+ return ufo.intersects(bird);
+ });
});
- // Update UFO and check for collisions
- if (ufo) {
- ufo.customUpdate();
- // Update each laser
- game.children.forEach(function (child) {
- if (child instanceof Laser) {
- child.update();
- // Check for intersections with birds and UFO
- birds.forEach(function (bird) {
- if (!child.lastIntersecting && AABBIntersect(child, bird)) {
- if (bird instanceof Bird1) {
- score += 5;
- } else if (bird instanceof Bird2 || bird instanceof Bird3) {
- score += 1;
- }
- scoreDisplay.updateScore(score); // Update the score display
- bird.destroy(); // Remove the bird when hit by a laser
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- child.destroy();
+ if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) {
+ // Destroy the UFO and set it to null
+ ufo.destroy();
+ ufo = null;
+ // Start a timer for the UFO to reappear between 30-50 seconds
+ ufoTimer = LK.setTimeout(function () {
+ ufo = addUFO();
+ }, Math.random() * 20000 + 30000);
+ } else {}
+ ufo.lastX = ufo.x; // Update lastX for the UFO
+ }
+ // Removed auto-targeting update function
+};
+// Function to spawn a third kind of bird
+function spawnBird1() {
+ var bird1 = new Bird1();
+ bird1.x = Math.random() * 2048;
+ bird1.y = -bird1.height;
+ bird1.speed = 1 + Math.random() * 0.6;
+ bird1.lastIntersecting = false;
+ bird1.type = 'bird1'; // Specific property for Bird1
+ bird1.color = 0x746130; // Specific color for Bird1
+ LK.setTimeout(function () {
+ game.addChild(bird1);
+ // Ensure bird1 is added to the game before setting its index
+ if (game.children.includes(bird1)) {
+ if (game.children.includes(bird1) && game.children.includes(tree)) {
+ game.setChildIndex(bird1, game.getChildIndex(tree)); // Set Bird1 to be rendered before tree
+ }
+ }
+ birds.push(bird1);
+ }, 2000);
+}
+function spawnBird2() {
+ var bird = new Bird2();
+ bird.y = Math.random() * (2732 / 2); // Random initial y position in the top half
+ bird.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side
+ bird.speed = 1 + Math.random() * 0.6;
+ bird.lastIntersecting = false;
+ bird.type = 'bird2'; // Specific property for Bird2
+ bird.color = 0xFFFFFF; // Specific color for Bird2
+ LK.setTimeout(function () {
+ game.addChild(bird);
+ // Ensure bird is added to the game before setting its index
+ if (game.children.includes(bird)) {
+ game.setChildIndex(bird, game.getChildIndex(stack1) + 1); // Set Bird2 to be rendered after stack1
+ }
+ birds.push(bird);
+ }, 2000);
+}
+// Update the main game update function to include bird updates
+var originalUpdate = game.update;
+game.update = function () {
+ // Call the original update function first
+ originalUpdate.call(this);
+ // Update each bird
+ birds.forEach(function (bird) {
+ bird.update();
+ // Check if the bird has moved off-screen
+ if (bird.lastY <= 2732 && bird.y > 2732) {
+ bird.y = -bird.height; // Respawn the bird at the top
+ bird.x = Math.random() * 2048; // Randomize the x position
+ bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird
+ }
+ });
+ // Update UFO and check for collisions
+ if (ufo) {
+ ufo.customUpdate();
+ // Update each laser
+ game.children.forEach(function (child) {
+ if (child instanceof Laser) {
+ child.update();
+ // Check for intersections with birds and UFO
+ birds.forEach(function (bird) {
+ if (!child.lastIntersecting && AABBIntersect(child, bird)) {
+ if (bird instanceof Bird1) {
+ score += 5;
+ } else if (bird instanceof Bird2 || bird instanceof Bird3) {
+ score += 1;
}
- child.lastIntersecting = birds.some(function (bird) {
- return child.intersects(bird);
- });
- });
- if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
- score += 25;
- scoreDisplay.updateScore(score);
+ scoreDisplay.updateScore(score); // Update the score display
+ bird.destroy(); // Remove the bird when hit by a laser
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
child.destroy();
}
child.lastIntersecting = birds.some(function (bird) {
return child.intersects(bird);
- }) || ufo && child.intersects(ufo);
+ });
+ });
+ if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
+ score += 25;
+ scoreDisplay.updateScore(score);
+ child.destroy();
}
- });
- }
- };
- // Add a tree to the game
- var tree = new Tree();
- tree.x = 2048 / 2 + 600; // Move the tree 500px to the right
- tree.y = 2500; // Position the tree on the grass
- layerManager.addToLayer(tree, LAYERS.TREE);
- var score = 0;
- // Add stack1 image to the game
- var stack1 = new Stack1();
- stack1.x = 1700; // Move 700px to the right
- stack1.y = 2250; // Move down by 800px
- layerManager.addToLayer(stack1, LAYERS.STACK);
- // Add the grass floor to the game
- var grassBack = new GrassBack();
- grassBack.x = 1020;
- grassBack.y = 2735; // Position grassBack at the bottom
- layerManager.addToLayer(grassBack, LAYERS.GRASS_BACK);
- var grassFront = new GrassFront();
- grassFront.x = 1020;
- grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px
- layerManager.addToLayer(grassFront, LAYERS.GRASS_FRONT);
- var scoreDisplay = new Mirror();
- scoreDisplay.x = 2048 / 2 + 400;
- scoreDisplay.y = 2732 - 185 - 80 - 390 + 30;
- layerManager.addToLayer(scoreDisplay, LAYERS.UI);
- // Function to handle bgm1 end event
- function onBgm1End() {
- // Set a timer to replay bgm1 after 50-80 seconds
- var bgmTimer = LK.setTimeout(function () {
+ child.lastIntersecting = birds.some(function (bird) {
+ return child.intersects(bird);
+ }) || ufo && child.intersects(ufo);
+ }
+ });
+ }
+};
+// Add a tree to the game
+var tree = game.addChild(new Tree());
+tree.x = 2048 / 2 + 600; // Move the tree 500px to the right
+tree.y = 2500; // Position the tree on the grass
+// Ensure tree is added to the game before setting its index
+if (game.children.includes(tree) && game.children.includes(bird1)) {
+ game.setChildIndex(tree, game.getChildIndex(bird1) + 1); // Set tree to be rendered after bird1
+}
+var score = 0;
+// Add stack1 image to the game
+var stack1 = game.addChild(new Stack1());
+stack1.x = 1700; // Move 700px to the right
+stack1.y = 2250; // Move down by 800px
+// Ensure stack1 is added to the game before setting its index
+if (game.children.includes(stack1) && game.children.includes(tree)) {
+ game.setChildIndex(stack1, game.getChildIndex(tree) + 1); // Set stack1 to be rendered after tree
+}
+// Add the grass floor to the game
+var grassBack = new GrassBack();
+grassBack.x = 1020;
+grassBack.y = 2735; // Position grassBack at the bottom
+game.addChild(grassBack);
+// Ensure grassBack is added to the game before setting its index
+if (game.children.includes(grassBack)) {
+ game.setChildIndex(grassBack, 5); // Set grassBack to be rendered after Bird3
+}
+var grassFront = new GrassFront();
+grassFront.x = 1020;
+grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px
+game.addChild(grassFront);
+// Ensure grassFront is added to the game before setting its index
+if (game.children.includes(grassFront) && game.children.includes(bird2)) {
+ game.setChildIndex(grassFront, game.getChildIndex(bird2) + 1); // Set grassFront to be rendered after bird2
+}
+var scoreDisplay = new Mirror();
+scoreDisplay.x = 2048 / 2 + 400; // Move the score display 400px to the right
+scoreDisplay.y = 2732 - 185 - 80 - 390 + 30; // Move the score display up by an additional 80px and down by 30px
+game.addChild(scoreDisplay);
+var mirror = new Mirror();
+mirror.x = 2048 / 2 + 400; // Move the mirror 400px to the right
+mirror.y = 2732 - 185 - 80 - 390 + 30; // Move the mirror up by an additional 80px and down by 30px
+game.addChild(mirror);
+// Ensure mirror is added to the game before setting its index
+if (game.children.includes(mirror) && game.children.includes(stack1)) {
+ game.setChildIndex(mirror, game.getChildIndex(stack1) + 1); // Set mirror to be rendered after stack1
+}
+// Ensure laser is added to the game before setting mirror's index
+if (game.children.includes(laser) && game.children.includes(mirror)) {
+ game.setChildIndex(mirror, game.getChildIndex(laser) + 1); // Set mirror to be rendered after the laser
+}
+// Function to handle bgm1 end event
+function onBgm1End() {
+ // Set a timer to replay bgm1 after 50-80 seconds
+ var bgmTimer = LK.setTimeout(function () {
+ LK.playMusic('bgm1', {
+ loop: true,
+ fade: {
+ start: 0,
+ end: 1,
+ duration: 4000
+ },
+ onEnd: onBgm1End
+ });
+ }, Math.random() * 30000 + 50000);
+}
+;
+// Add the cat to the game
+var cat = game.addChild(new Cat());
+cat.x = 230; // Move the cat 20px to the left
+cat.y = 2732; // Position the cat at the bottom of the screen
+// Ensure cat is added to the game before setting its index
+if (game.children.includes(cat)) {
+ game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value
+}
+// Play bgm1 once on load and set a timer to replay it every 20-50 seconds
+LK.playMusic('bgm1', {
+ loop: false,
+ // Play once
+ fade: {
+ start: 0,
+ end: 0.02,
+ // Set to the lowest volume
+ duration: 4000
+ },
+ onEnd: function onEnd() {
+ // Set a timer to replay bgm1 every 20-50 seconds
+ LK.setTimeout(function () {
LK.playMusic('bgm1', {
- loop: true,
+ loop: false,
+ // Play once
fade: {
start: 0,
- end: 1,
+ end: 0.5,
+ // Set to the lowest volume
duration: 4000
},
- onEnd: onBgm1End
+ onEnd: onEnd // Set the onEnd function to replay bgm1
});
- }, Math.random() * 30000 + 50000);
+ }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds
}
- ;
- // Add the cat to the game
- var cat = new Cat();
- cat.x = 230; // Move the cat 20px to the left
- cat.y = 2732; // Position the cat at the bottom of the screen
- layerManager.addToLayer(cat, LAYERS.CAT);
- game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value
- // Play bgm1 once on load and set a timer to replay it every 20-50 seconds
- LK.playMusic('bgm1', {
- loop: false,
- // Play once
- fade: {
- start: 0,
- end: 0.02,
- // Set to the lowest volume
- duration: 4000
- },
- onEnd: function onEnd() {
- // Set a timer to replay bgm1 every 20-50 seconds
- LK.setTimeout(function () {
- LK.playMusic('bgm1', {
- loop: false,
- // Play once
- fade: {
- start: 0,
- end: 0.5,
- // Set to the lowest volume
- duration: 4000
- },
- onEnd: onEnd // Set the onEnd function to replay bgm1
- });
- }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds
+});
+// Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds
+var wingsTimer = LK.setTimeout(function () {
+ LK.getSound('wings1').play();
+ wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000);
+}, Math.random() * 20000 + 10000);
+// Create an array for all sounds except ufo1, including all birdsong sounds
+var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1'];
+var currentAmbientSound = null;
+// Sound volumes configuration
+// Initialize random timers for each sound to play between 30-50 seconds
+sounds.forEach(function (soundId) {
+ var sound = LK.getSound(soundId);
+ var ambientSoundTimer = LK.setTimeout(function () {
+ if (currentAmbientSound) {
+ currentAmbientSound.stop(); // Stop the currently playing ambient sound
}
- });
- // Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds
- var wingsTimer = LK.setTimeout(function () {
- LK.getSound('wings1').play();
- wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000);
- }, Math.random() * 20000 + 10000);
- // Create an array for all sounds except ufo1, including all birdsong sounds
- var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1'];
- var currentAmbientSound = null;
- // Sound volumes configuration
- // Initialize random timers for each sound to play between 30-50 seconds
- sounds.forEach(function (soundId) {
- var sound = LK.getSound(soundId);
- var ambientSoundTimer = LK.setTimeout(function () {
- if (currentAmbientSound) {
- currentAmbientSound.stop(); // Stop the currently playing ambient sound
- }
- sound.play();
- currentAmbientSound = sound; // Set the current ambient sound
- // Set a timeout to reset the current ambient sound after it finishes playing
- LK.setTimeout(function () {
- currentAmbientSound = null;
- }, sound.duration * 1000); // Convert duration from seconds to milliseconds
- // Reset the timer to play the sound again between 30-50 seconds, plus an additional 5 seconds
- ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000 + 5000);
- }, Math.random() * 20000 + 30000);
- });
- /****
- * Layer Management System
- ****/
- var LayerManager = function LayerManager(gameInstance) {
- var self = this;
- self.game = gameInstance;
- self.layerMap = {};
- self.addToLayer = function (object, layerId) {
- if (!object || !object.id) {
- return;
- }
- self.layerMap[object.id] = layerId;
- self.game.setChildIndex(object, layerId);
- };
- self.getObjectLayer = function (object) {
- if (!object || !object.id) {
- return -1;
- }
- return self.layerMap[object.id] || -1;
- };
- self.removeFromTracking = function (object) {
- if (!object || !object.id) {
- return;
- }
- delete self.layerMap[object.id];
- };
- self.bringToFrontOfLayer = function (object) {
- if (!object || !object.id) {
- return;
- }
- var layerId = self.getObjectLayer(object);
- if (layerId === -1) {
- return;
- }
- var highestIndex = layerId;
- for (var id in self.layerMap) {
- if (self.layerMap[id] === layerId) {
- for (var i = 0; i < self.game.children.length; i++) {
- var child = self.game.children[i];
- if (child.id === id && i > highestIndex) {
- highestIndex = i;
- }
- }
- }
- }
- self.game.setChildIndex(object, highestIndex);
- };
- self.sendToBackOfLayer = function (object) {
- if (!object || !object.id) {
- return;
- }
- var layerId = self.getObjectLayer(object);
- if (layerId === -1) {
- return;
- }
- var lowestIndex = layerId;
- for (var id in self.layerMap) {
- if (self.layerMap[id] === layerId) {
- for (var i = 0; i < self.game.children.length; i++) {
- var child = self.game.children[i];
- if (child.id === id && i < lowestIndex) {
- lowestIndex = i;
- }
- }
- }
- }
- self.game.setChildIndex(object, lowestIndex);
- };
- }; // Added missing closing brace for LayerManager class
- // Initialize the layer manager
- var layerManager = new LayerManager(game);
- // Using UFO sound ID as placeholder
- var CLOUD_TYPES = ['cloud1', 'cloud2'];
- var SunPulse = function SunPulse(sunGraphics) {
- this.sunGraphics = sunGraphics;
- this.startPulsating = function () {
- function pulsate() {
- tween(this.sunGraphics, {
- scaleX: 1.1,
- scaleY: 1.1
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: function () {
- tween(this.sunGraphics, {
- scaleX: 1.0,
- scaleY: 1.0
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: pulsate.bind(this)
- });
- }.bind(this)
- });
- }
- pulsate.call(this);
- };
+ sound.play();
+ currentAmbientSound = sound; // Set the current ambient sound
+ // Set a timeout to reset the current ambient sound after it finishes playing
+ LK.setTimeout(function () {
+ currentAmbientSound = null;
+ }, sound.duration * 1000); // Convert duration from seconds to milliseconds
+ // Reset the timer to play the sound again between 30-50 seconds, plus an additional 5 seconds
+ ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000 + 5000);
+ }, Math.random() * 20000 + 30000);
+});
+/****
+* Layer Constants
+****/
+var LAYERS = {
+ BACKGROUND: 0,
+ CLOUDS: 1,
+ SUN: 2,
+ BIRDS: 3,
+ UFO: 4,
+ TREE: 5,
+ GRASS_BACK: 6,
+ CAT: 7,
+ GRASS_FRONT: 8,
+ STACK: 9,
+ LASER: 10,
+ JET: 11,
+ RETICLE: 12,
+ UI: 13,
+ EFFECTS: 14
+};
+/****
+* Layer Management System
+****/
+var LayerManager = function LayerManager(gameInstance) {
+ var self = this;
+ self.game = gameInstance;
+ self.layerMap = {}; // Maps objects to their layers
+ /**
+ * Add an object to a specific layer
+ * @param {Object} object - The game object to add
+ * @param {Number} layerId - The layer ID to add the object to
+ */
+ self.addToLayer = function (object, layerId) {
+ if (!object) {
+ return;
+ }
+ // Add to game if not already added
+ if (!self.game.children.includes(object)) {
+ self.game.addChild(object);
+ }
+ // Set the object's index based on the layer
+ self.setLayerIndex(object, layerId);
+ // Store the layer information for this object
+ self.layerMap[object.id] = layerId;
+ return object;
};
- // Light1Pulse class to handle the pulsating effect for Light1
- var Light1Pulse = function Light1Pulse(lightGraphics) {
- this.lightGraphics = lightGraphics;
- this.startPulsating = function () {
- function pulsate() {
- tween(this.lightGraphics, {
- scaleX: 1.6,
- scaleY: 1.6
- }, {
- duration: 1000,
- easing: tween.easeInOut,
- onFinish: function () {
- tween(this.lightGraphics, {
- scaleX: 1.0,
- scaleY: 1.0
- }, {
- duration: 1500,
- easing: tween.easeInOut,
- onFinish: pulsate.bind(this)
- });
- }.bind(this)
- });
- }
- pulsate.call(this);
- };
+ /**
+ * Set the layer index for an object
+ * @param {Object} object - The game object
+ * @param {Number} layerId - The layer ID
+ */
+ self.setLayerIndex = function (object, layerId) {
+ if (!object || !self.game.children.includes(object)) {
+ return;
+ }
+ // Ensure the layer ID is valid
+ layerId = Math.max(0, Math.min(layerId, self.game.children.length - 1));
+ // Set the child index
+ self.game.setChildIndex(object, layerId);
+ // Update the layer map
+ self.layerMap[object.id] = layerId;
};
- var UFOSound = function UFOSound() {
- var sound = LK.getSound('ufo1');
- this.play = function () {
- if (sound) {
- sound.play();
- }
- };
- this.stop = function () {
- if (sound) {
- sound.stop();
- }
- };
+ /**
+ * Get the current layer of an object
+ * @param {Object} object - The game object
+ * @returns {Number} The layer ID or -1 if not found
+ */
+ self.getObjectLayer = function (object) {
+ if (!object || !object.id) {
+ return -1;
+ }
+ return self.layerMap[object.id] || -1;
};
- // BackgroundMusic class to manage background music
- var BackgroundMusic = function BackgroundMusic() {
- var bgm = LK.getSound('bgm1');
- var breeze = LK.getSound('breeze1');
- this.play = function () {
- if (bgm) {
- bgm.play();
- }
- if (breeze) {
- breeze.play();
- }
- };
- this.stop = function () {
- if (bgm) {
- bgm.stop();
- }
- if (breeze) {
- breeze.stop();
- }
- };
+ /**
+ * Move an object to a different layer
+ * @param {Object} object - The game object
+ * @param {Number} newLayerId - The new layer ID
+ */
+ self.moveToLayer = function (object, newLayerId) {
+ if (!object) {
+ return;
+ }
+ self.setLayerIndex(object, newLayerId);
};
- // LaserSound class to manage laser sound effects
- var LaserSound = function LaserSound() {
- var sound = LK.getSound('laser1');
- this.play = function () {
- if (sound) {
- sound.play();
- }
- };
- };
- var Bird1Movement = function Bird1Movement(bird, birdGraphics) {
- this.bird = bird;
- this.birdGraphics = birdGraphics;
- this.bird.lastY = this.bird.y;
- this.bird.lastX = this.bird.x;
- this.update = function () {
- this.x += Math.sin(this.y / 100) * 6.5;
- if (this.x < 0 || this.x > 2048) {
- if (this.x < 0) {
- this.birdGraphics.flipX = false;
- } else {
- this.birdGraphics.flipX = true;
+ /**
+ * Get all objects in a specific layer
+ * @param {Number} layerId - The layer ID
+ * @returns {Array} Array of objects in the layer
+ */
+ self.getObjectsInLayer = function (layerId) {
+ var objects = [];
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id) {
+ objects.push(child);
+ break;
+ }
}
- this.x = Math.random() * 2048;
}
- this.lastX = this.x;
- this.lastY = this.y;
- };
+ }
+ return objects;
};
- var Bird2Movement = function Bird2Movement(bird, birdGraphics) {
- this.bird = bird;
- this.birdGraphics = birdGraphics;
- this.bird.lastY = this.bird.y;
- this.bird.lastX = this.bird.x;
- this.update = function () {
- this.x += Math.sin(this.y / 100) * 6.5;
- if (this.x < 0) {
- this.birdGraphics.flipX = false;
- } else {
- this.birdGraphics.flipX = true;
- }
- this.lastX = this.x;
- if (this.x < 0 || this.x > 2048) {
- this.y = Math.random() * 2732;
- this.x = Math.random() < 0.5 ? 0 : 2048;
- }
- this.lastY = this.y;
- };
+ /**
+ * Remove an object from layer tracking
+ * @param {Object} object - The game object to remove
+ */
+ self.removeFromTracking = function (object) {
+ if (!object || !object.id) {
+ return;
+ }
+ delete self.layerMap[object.id];
};
- var CloudMovement = function CloudMovement(cloud, cloudGraphics) {
- if (!cloud || !cloudGraphics) {
- throw new Error("Cloud and cloudGraphics must be provided");
+ /**
+ * Bring an object to the front of its layer
+ * @param {Object} object - The game object
+ */
+ self.bringToFrontOfLayer = function (object) {
+ if (!object || !object.id) {
+ return;
}
- this.cloud = cloud;
- this.cloudGraphics = cloudGraphics;
- this.speed = Math.random() * 0.55 + 0.25;
- this.hasAccelerated = false;
- this.direction = Math.random() < 0.5 ? 1 : -1;
- this.lastX = this.cloud.x;
- this.lastIntersecting = false;
- this.update = function () {
- try {
- this.cloud.x += this.speed * this.direction;
- if (this.cloud.x <= -this.cloud.width / 2) {
- this.cloud.x = 2048 + this.cloud.width / 2;
- this.direction = -1;
- this.cloudGraphics.flipX = true;
- } else if (this.cloud.x >= 2048 + this.cloud.width / 2) {
- this.cloud.x = -this.cloud.width / 2;
- this.direction = 1;
- this.cloudGraphics.flipX = false;
+ var layerId = self.getObjectLayer(object);
+ if (layerId === -1) {
+ return;
+ }
+ // Find the highest index within this layer
+ var highestIndex = layerId;
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id && i > highestIndex) {
+ highestIndex = i;
+ }
}
- } catch (error) {
- console.error("Error updating cloud movement:", error);
}
- };
+ }
+ // Set the object to the highest index within its layer
+ self.game.setChildIndex(object, highestIndex);
};
- var Bird2Effects = function Bird2Effects(bird) {
- if (!bird) {
- throw new Error("Bird must be provided");
+ /**
+ * Send an object to the back of its layer
+ * @param {Object} object - The game object
+ */
+ self.sendToBackOfLayer = function (object) {
+ if (!object || !object.id) {
+ return;
}
- this.bird = bird;
- this.applyEffects = function () {
- // Apply visual effects when bird is hit
- if (this.bird.isHit) {
- this.bird.alpha = 0.7;
- this.bird.speed *= 0.8;
+ var layerId = self.getObjectLayer(object);
+ if (layerId === -1) {
+ return;
+ }
+ // Find the lowest index within this layer
+ var lowestIndex = layerId;
+ for (var id in self.layerMap) {
+ if (self.layerMap[id] === layerId) {
+ // Find the object in the game's children
+ for (var i = 0; i < self.game.children.length; i++) {
+ var child = self.game.children[i];
+ if (child.id === id && i < lowestIndex) {
+ lowestIndex = i;
+ }
+ }
}
- };
+ }
+ // Set the object to the lowest index within its layer
+ self.game.setChildIndex(object, lowestIndex);
};
- // Add background to the BACKGROUND layer
- var background = new Background();
- background.x = 2048 / 2;
- background.y = 2732 / 2 - 140;
- layerManager.addToLayer(background, LAYERS.BACKGROUND);
- // ScoreManager class to manage score logic
- var ScoreManager = function ScoreManager() {
- var self = this;
- var score = 0;
- self.addScore = function (points) {
- score += points;
- return score;
- };
- self.resetScore = function () {
- score = 0;
- return score;
- };
- self.getScore = function () {
- return score;
- };
+ /**
+ * Helper function to add multiple objects to a layer at once
+ * @param {Array} objects - Array of game objects
+ * @param {Number} layerId - The layer ID
+ */
+ self.addMultipleToLayer = function (objects, layerId) {
+ if (!Array.isArray(objects)) {
+ return;
+ }
+ objects.forEach(function (object) {
+ self.addToLayer(object, layerId);
+ });
};
- var birds = [];
- game.down = function (x, y, obj) {
- if (!game.reticle) {
- game.reticle = new Reticle();
- layerManager.addToLayer(game.reticle, LAYERS.RETICLE);
+};
+// Initialize the layer manager
+var layerManager = new LayerManager(game);
+// Using UFO sound ID as placeholder
+var CLOUD_TYPES = ['cloud1', 'cloud2'];
+var SunPulse = function SunPulse(sunGraphics) {
+ this.sunGraphics = sunGraphics;
+ this.startPulsating = function () {
+ function pulsate() {
+ tween(this.sunGraphics, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: function () {
+ tween(this.sunGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: pulsate.bind(this)
+ });
+ }.bind(this)
+ });
}
- game.reticle.x = x;
- game.reticle.y = y;
- var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point
- layerManager.addToLayer(laser, LAYERS.LASER);
+ pulsate.call(this);
};
- var VOLUME_BGM1 = 0.02;
- var VOLUME_BREEZE1 = 0.02;
- var VOLUME_CRICKET1 = 0.02;
- var VOLUME_FROG1 = 0.02;
- var VOLUME_SONGBIRD1 = 0.02;
- var VOLUME_UFO1 = 0.02;
- var VOLUME_WINGS1 = 0.02;
- game.move = function (x, y, obj) {
- if (!game.reticle) {
- game.reticle = new Reticle();
- layerManager.addToLayer(game.reticle, LAYERS.RETICLE);
+};
+// Light1Pulse class to handle the pulsating effect for Light1
+var Light1Pulse = function Light1Pulse(lightGraphics) {
+ this.lightGraphics = lightGraphics;
+ this.startPulsating = function () {
+ function pulsate() {
+ tween(this.lightGraphics, {
+ scaleX: 1.6,
+ scaleY: 1.6
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut,
+ onFinish: function () {
+ tween(this.lightGraphics, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 1500,
+ easing: tween.easeInOut,
+ onFinish: pulsate.bind(this)
+ });
+ }.bind(this)
+ });
}
- if (obj.event) {
- var x = obj.event.x;
- var y = obj.event.y;
+ pulsate.call(this);
+ };
+};
+var bird2; // Define bird2 variable in the global scope
+// Bird2Effects class to handle effects and animations for Bird2
+// Function to create a pulsating effect
+// UFOSound class to manage UFO sound effects
+// UFOMovement class to handle UFO movement
+var UFOMovement = function UFOMovement(ufo, ufoGraphics) {
+ this.ufo = ufo;
+ this.ufoGraphics = ufoGraphics;
+ this.ufo.speed = 3.2; // Decrease speed by 20%
+ this.ufo.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ this.ufo.lastX = this.ufo.x; // Track last X position for future checks
+ this.ufo.update = function () {
+ this.x += this.speed * this.direction;
+ this.y = 100 + Math.sin(this.x / 200) * 250; // Slow down the wave pattern further
+ // Stop playing ufo1 sound after UFO leaves the screen
+ if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2 || this.lastX >= -this.width / 2 && this.x < -this.width / 2) {
+ this.destroy();
+ ufo = null;
+ LK.getSound('ufo1').stop();
}
- game.reticle.x = x;
- game.reticle.y = y;
+ this.lastX = this.x; // Update lastX after movement
};
- // Add a sun to the game in the top left corner
- var sun = new Sun();
- sun.x = 480;
- sun.y = 680;
- layerManager.addToLayer(sun, LAYERS.SUN);
- var light1 = new Light1();
- light1.x = 510; // Move light1 right by 50px
- light1.y = 1500; // Move light1 down by 210px
- layerManager.addToLayer(light1, LAYERS.EFFECTS);
- // Function to add a UFO to the game
- // Initialize clouds array
- var clouds = [];
- for (var i = 0; i < 6; i++) {
- var cloud = new Cloud();
- cloud.x = Math.random() * 2048;
- cloud.y = Math.random() * (2732 / 2);
- layerManager.addToLayer(cloud, i < 3 ? LAYERS.CLOUD1 : LAYERS.CLOUD2);
- clouds.push(cloud);
- }
- var bird1; // Define bird1 variable in the global scope
- var bird; // Define bird variable in the global scope
- // Initialize birds
- spawnBird1();
- spawnBird2();
- spawnBird2();
- // Initialize a timer to add a UFO at a random time between 20 and 30 seconds
- var ufo; // Define the ufo variable in the global scope
- var laser; // Define the laser variable in the global scope
- var ufoTimer = LK.setTimeout(function () {
- addUFO();
- // Reset the timer for the UFO to reappear between 10-20 seconds
- ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000);
- }, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds
- game.update = function () {
- // Update clouds
+};
+var UFOSound = function UFOSound() {
+ this.play = function () {
+ LK.getSound('ufo1').play();
+ };
+};
+// BackgroundMusic class to manage background music
+var BackgroundMusic = function BackgroundMusic() {
+ this.play = function () {
+ LK.playMusic('bgm1', {
+ loop: true,
+ fade: {
+ start: 0,
+ end: 1,
+ duration: 4000
+ },
+ onEnd: onBgm1End
+ });
+ };
+};
+// LaserSound class to manage laser sound effects
+var LaserSound = function LaserSound() {
+ this.play = function () {
+ LK.getSound('laser1').play();
+ };
+};
+var Bird1Movement = function Bird1Movement(bird, birdGraphics) {
+ this.bird = bird;
+ this.birdGraphics = birdGraphics;
+ this.bird.speed = Math.random() * 1.6 + 1;
+ this.bird.lastY = storage.bird1_lastY || this.bird.y; // Initialize lastY for tracking changes on Y
+ this.bird.lastX = storage.bird1_lastX || this.bird.x; // Initialize lastX for tracking changes on X
+ this.bird.update = function () {
+ this.y += this.speed;
+ this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30%
+ // Check if the bird has moved off-screen
+ if (this.lastY <= 2732 && this.y > 2732) {
+ this.y = -this.height;
+ this.x = Math.random() < 0.33 ? Math.random() * 2048 : Math.random() < 0.5 ? 0 : 2048;
+ }
+ flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ this.lastX = this.x; // Track position for next update
+ this.lastY = this.y; // Update lastY after movement
+ storage.bird1_lastX = this.lastX; // Store lastX using storage plugin
+ storage.bird1_lastY = this.lastY; // Store lastY using storage plugin
+ };
+};
+var Bird2Movement = function Bird2Movement(bird, birdGraphics) {
+ this.bird = bird;
+ this.birdGraphics = birdGraphics;
+ this.bird.speed = Math.random() * 1.6 + 1;
+ this.bird.lastY = storage.bird2_lastY || this.bird.y; // Initialize lastY for tracking changes on Y
+ this.bird.lastX = storage.bird2_lastX || this.bird.x; // Initialize lastX for tracking changes on X
+ this.bird.update = function () {
+ this.y += this.speed;
+ this.x += Math.sin(this.y / 100) * 6.5; // Increase the swooping motion of the bird's movement by 30%
+ flipImageVerticallyBasedOnDirection(birdGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ // Check if the bird has moved off-screen
+ if (this.lastY <= 2732 && this.y > 2732) {
+ this.y = Math.random() * 2732; // Random initial y position within the screen height
+ this.x = Math.random() < 0.5 ? 0 : 2048; // Start from either the left or right side
+ }
+ this.lastX = this.x; // Track position for next update
+ this.lastY = this.y; // Update lastY after movement
+ storage.bird2_lastX = this.lastX; // Store lastX using storage plugin
+ storage.bird2_lastY = this.lastY; // Store lastY using storage plugin
+ };
+};
+var CloudMovement = function CloudMovement(cloud, cloudGraphics) {
+ this.cloud = cloud;
+ this.cloudGraphics = cloudGraphics;
+ this.cloud.speed = Math.random() * 0.55 + 0.25; // Adjust speed to range between 0.25 and 0.8
+ this.cloud.hasAccelerated = false; // Track if the cloud has already accelerated
+ this.cloud.direction = Math.random() < 0.5 ? 1 : -1; // Random direction: 1 for right, -1 for left
+ this.cloud.lastX = this.cloud.x; // Track last X position for future checks
+ this.cloud.lastIntersecting = false; // Initialize lastIntersecting for tracking changes on intersections
+ this.cloud.update = function () {
+ flipImageVerticallyBasedOnDirection(this.cloudGraphics, this.lastX, this.x); // Use flipImageVerticallyBasedOnDirection to flip on X axis
+ this.x += this.speed * this.direction;
+ // If the cloud moves off-screen, reposition it to the opposite side
+ if (this.lastX <= 2048 + this.width / 2 && this.x > 2048 + this.width / 2) {
+ this.x = -this.width / 2;
+ } else if (this.lastX >= -this.width / 2 && this.x < -this.width / 2) {
+ this.x = 2048 + this.width / 2;
+ }
+ // Check for overlap with other clouds
for (var i = 0; i < clouds.length; i++) {
- clouds[i].update();
+ if (clouds[i] !== this && this.intersects(clouds[i])) {
+ if (!this.hasAccelerated) {
+ this.speed *= 1.5; // Increase speed by 50%
+ this.hasAccelerated = true; // Mark as accelerated
+ }
+ break;
+ } else if (this.hasAccelerated && !this.intersects(clouds[i])) {
+ this.speed /= 1.5; // Reset speed to original
+ this.hasAccelerated = false; // Reset acceleration state
+ }
}
- // Add a jet to the game if it doesn't exist and no jet spawn timer is active
- if (!game.jet && !game.jetSpawnTimer) {
- var jet = new Jet1();
- jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen
- jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen
- layerManager.addToLayer(jet, LAYERS.JET);
- game.jet = jet;
- if (!game.jetSoundPlayed) {
- LK.getSound('jet1').play(); // Play jet sound when it appears
- game.jetSoundPlayed = true; // Ensure it only plays once
+ // Check if the cloud intersects with the sun
+ if (!this.lastIntersecting && this.intersects(sun)) {
+ this.speed *= 2; // Double the speed when touching the sun
+ if (this.cloudGraphics) {
+ tween(this.cloudGraphics, {
+ alpha: 0.5
+ }, {
+ duration: 3000,
+ easing: tween.linear
+ });
}
+ } else if (this.lastIntersecting && !this.intersects(sun)) {
+ this.speed /= 2; // Reset speed to original when not touching the sun
+ if (this.cloudGraphics) {
+ tween(this.cloudGraphics, {
+ alpha: 1.0
+ }, {
+ duration: 3000,
+ easing: tween.linear
+ });
+ }
}
- // Update jet
- if (game.jet) {
- game.jet.update();
+ this.lastIntersecting = this.intersects(sun);
+ this.lastX = this.x; // Update lastX after movement
+ };
+};
+var Bird2Effects = function Bird2Effects(bird) {
+ this.bird = bird;
+ this.applyEffects = function () {
+ // Add any specific effects or animations for Bird2 here
+ };
+};
+// Bird1Effects class to handle effects and animations for Bird1
+var Bird1Effects = function Bird1Effects(bird) {
+ this.bird = bird;
+ this.applyEffects = function () {
+ // Add any specific effects or animations for Bird1 here
+ };
+};
+// Add background to the BACKGROUND layer
+var background = new Background();
+background.x = 2048 / 2;
+background.y = 2732 / 2 - 140;
+layerManager.addToLayer(background, LAYERS.BACKGROUND);
+// ScoreManager class to manage score logic
+var ScoreManager = function ScoreManager() {
+ var self = this;
+ self.score = 0;
+ self.addScore = function (points) {
+ self.score += points;
+ return self.score;
+ };
+ self.resetScore = function () {
+ self.score = 0;
+ };
+ self.getScore = function () {
+ return self.score;
+ };
+};
+var birds = [];
+game.down = function (x, y, obj) {
+ if (!game.reticle) {
+ game.reticle = game.addChild(new Reticle());
+ if (game.children.includes(game.reticle)) {
+ game.setChildIndex(game.reticle, 7); // Set reticle to be rendered after the tree
}
- if (ufo) {
- ufo.customUpdate();
- // Update each laser
- game.children.forEach(function (child) {
- if (child instanceof Laser) {
- child.update();
- // Check for intersections with birds and UFO
- birds.forEach(function (bird) {
- if (!child.lastIntersecting && AABBIntersect(child, bird)) {
- if (bird instanceof Bird1) {
- score += 5;
- } else if (bird instanceof Bird2 || bird instanceof Bird3) {
- score += 1;
- }
- scoreDisplay.updateScore(score); // Update the score display
- LK.getSound('dead1').play(); // Play the 'dead1' sound when a bird is hit
- bird.destroy(); // Remove the bird when hit by a laser
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- child.destroy();
+ }
+ game.reticle.x = x;
+ game.reticle.y = y;
+ var laser = new Laser(cat.x - 140, cat.y - 440, x, y); // laser starting point
+ game.addChild(laser);
+ if (game.children.includes(laser) && game.children.includes(game.reticle)) {
+ game.setChildIndex(laser, game.getChildIndex(game.reticle) + 1); // Ensure laser is rendered after the reticle
+ }
+};
+var VOLUME_BGM1 = 0.02;
+var VOLUME_BREEZE1 = 0.02;
+var VOLUME_CRICKET1 = 0.02;
+var VOLUME_FROG1 = 0.02;
+var VOLUME_SONGBIRD1 = 0.02;
+var VOLUME_UFO1 = 0.02;
+var VOLUME_WINGS1 = 0.02;
+game.move = function (x, y, obj) {
+ if (!game.reticle) {
+ game.reticle = game.addChild(new Reticle());
+ }
+ if (obj.event) {
+ var x = obj.event.x;
+ var y = obj.event.y;
+ }
+ game.reticle.x = x;
+ game.reticle.y = y;
+};
+// Add a sun to the game in the top left corner
+var sun = new Sun();
+sun.x = 480;
+sun.y = 680;
+layerManager.addToLayer(sun, LAYERS.SUN);
+var light1 = new Light1();
+light1.x = 510; // Move light1 right by 50px
+light1.y = 1500; // Move light1 down by 210px
+layerManager.addToLayer(light1, LAYERS.EFFECTS);
+// Function to add a UFO to the game
+// Initialize clouds array
+var clouds = [];
+for (var i = 0; i < 3; i++) {
+ // Add 3 clouds for variety
+ var cloud = new Cloud();
+ cloud.x = Math.random() * 2048;
+ cloud.y = Math.random() * (2732 / 2); // Position clouds in the upper half of the screen
+ layerManager.addToLayer(cloud, LAYERS.CLOUDS);
+ clouds.push(cloud);
+}
+for (var i = 0; i < 3; i++) {
+ // Add 3 cloud2 for variety
+ var cloud2 = new Cloud();
+ cloud2.x = Math.random() * 2048;
+ cloud2.y = Math.random() * (2732 / 2); // Position cloud in the upper half of the screen
+ layerManager.addToLayer(cloud2, LAYERS.CLOUDS);
+ clouds.push(cloud2);
+}
+var bird1; // Define bird1 variable in the global scope
+var bird; // Define bird variable in the global scope
+// Initialize birds
+spawnBird1();
+spawnBird2();
+spawnBird2();
+// Initialize a timer to add a UFO at a random time between 20 and 30 seconds
+var ufo; // Define the ufo variable in the global scope
+var laser; // Define the laser variable in the global scope
+var ufoTimer = LK.setTimeout(function () {
+ addUFO();
+ // Reset the timer for the UFO to reappear between 10-20 seconds
+ ufoTimer = LK.setTimeout(arguments.callee, Math.random() * 10000 + 10000);
+}, Math.random() * 10000 + 10000); // Initial interval for UFO appearances between 10 to 20 seconds
+game.update = function () {
+ // Update clouds
+ for (var i = 0; i < clouds.length; i++) {
+ clouds[i].update();
+ }
+ // Add a jet to the game if it doesn't exist and no jet spawn timer is active
+ if (!game.jet && !game.jetSpawnTimer) {
+ var jet = new Jet1();
+ jet.x = Math.random() < 0.5 ? 2048 + jet.width / 2 : -jet.width / 2; // Start from either the far right or left edge of the screen
+ jet.y = Math.random() * (2732 / 2); // Random initial y position in the top half of the screen
+ game.addChild(jet);
+ if (game.children.includes(jet) && game.children.includes(clouds[0])) {
+ // Set jet to render after cloud1 but before cloud2
+ game.setChildIndex(jet, game.getChildIndex(clouds[0]) + 1);
+ }
+ game.jet = jet;
+ if (!game.jetSoundPlayed) {
+ LK.getSound('jet1').play(); // Play jet sound when it appears
+ game.jetSoundPlayed = true; // Ensure it only plays once
+ }
+ }
+ // Update jet
+ if (game.jet) {
+ game.jet.update();
+ }
+ if (ufo) {
+ ufo.customUpdate();
+ // Update each laser
+ game.children.forEach(function (child) {
+ if (child instanceof Laser) {
+ child.update();
+ // Check for intersections with birds and UFO
+ birds.forEach(function (bird) {
+ if (!child.lastIntersecting && AABBIntersect(child, bird)) {
+ if (bird instanceof Bird1) {
+ score += 5;
+ } else if (bird instanceof Bird2 || bird instanceof Bird3) {
+ score += 1;
}
- child.lastIntersecting = birds.some(function (bird) {
- return child.intersects(bird);
- });
- });
- if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
- score += 25;
- scoreDisplay.updateScore(score);
+ scoreDisplay.updateScore(score); // Update the score display
+ LK.getSound('dead1').play(); // Play the 'dead1' sound when a bird is hit
+ bird.destroy(); // Remove the bird when hit by a laser
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
child.destroy();
}
child.lastIntersecting = birds.some(function (bird) {
return child.intersects(bird);
- }) || ufo && child.intersects(ufo);
- }
- });
- // Check for intersections between UFO and birds
- birds.forEach(function (bird) {
- if (!ufo.lastIntersecting && ufo.intersects(bird)) {
- bird.destroy(); // Remove the bird when touched by the UFO
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- LK.getSound('electro').play(); // Play the 'electro' sound
- }
- ufo.lastIntersecting = birds.some(function (bird) {
- return ufo.intersects(bird);
+ });
});
- });
- if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) {
- // Destroy the UFO and set it to null
- ufo.destroy();
- ufo = null;
- // Start a timer for the UFO to reappear between 30-50 seconds
- ufoTimer = LK.setTimeout(function () {
- ufo = addUFO();
- }, Math.random() * 20000 + 30000);
- } else {}
- ufo.lastX = ufo.x; // Update lastX for the UFO
- }
- // Removed auto-targeting update function
- };
- // Function to spawn a third kind of bird
- // Update the main game update function to include bird updates
- var originalUpdate = game.update;
- game.update = function () {
- // Call the original update function first
- originalUpdate.call(this);
- // Update each bird
+ if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
+ score += 25;
+ scoreDisplay.updateScore(score);
+ child.destroy();
+ }
+ child.lastIntersecting = birds.some(function (bird) {
+ return child.intersects(bird);
+ }) || ufo && child.intersects(ufo);
+ }
+ });
+ // Check for intersections between UFO and birds
birds.forEach(function (bird) {
- bird.update();
- // Check if the bird has moved off-screen
- if (bird.lastY <= 2732 && bird.y > 2732) {
- bird.y = -bird.height; // Respawn the bird at the top
- bird.x = Math.random() * 2048; // Randomize the x position
- bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird
+ if (!ufo.lastIntersecting && ufo.intersects(bird)) {
+ bird.destroy(); // Remove the bird when touched by the UFO
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
+ LK.getSound('electro').play(); // Play the 'electro' sound
}
+ ufo.lastIntersecting = birds.some(function (bird) {
+ return ufo.intersects(bird);
+ });
});
- // Update UFO and check for collisions
- if (ufo) {
- ufo.customUpdate();
- // Update each laser
- game.children.forEach(function (child) {
- if (child instanceof Laser) {
- child.update();
- // Check for intersections with birds and UFO
- birds.forEach(function (bird) {
- if (!child.lastIntersecting && AABBIntersect(child, bird)) {
- if (bird instanceof Bird1) {
- score += 5;
- } else if (bird instanceof Bird2 || bird instanceof Bird3) {
- score += 1;
- }
- scoreDisplay.updateScore(score); // Update the score display
- bird.destroy(); // Remove the bird when hit by a laser
- birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
- child.destroy();
+ if (ufo.lastX <= 2048 + ufo.width / 2 && ufo.x > 2048 + ufo.width / 2 || ufo.lastX >= -ufo.width / 2 && ufo.x < -ufo.width / 2) {
+ // Destroy the UFO and set it to null
+ ufo.destroy();
+ ufo = null;
+ // Start a timer for the UFO to reappear between 30-50 seconds
+ ufoTimer = LK.setTimeout(function () {
+ ufo = addUFO();
+ }, Math.random() * 20000 + 30000);
+ } else {}
+ ufo.lastX = ufo.x; // Update lastX for the UFO
+ }
+ // Removed auto-targeting update function
+};
+// Function to spawn a third kind of bird
+// Update the main game update function to include bird updates
+var originalUpdate = game.update;
+game.update = function () {
+ // Call the original update function first
+ originalUpdate.call(this);
+ // Update each bird
+ birds.forEach(function (bird) {
+ bird.update();
+ // Check if the bird has moved off-screen
+ if (bird.lastY <= 2732 && bird.y > 2732) {
+ bird.y = -bird.height; // Respawn the bird at the top
+ bird.x = Math.random() * 2048; // Randomize the x position
+ bird.speed = 1 + Math.random() * 0.6; // Reset speed for new bird
+ }
+ });
+ // Update UFO and check for collisions
+ if (ufo) {
+ ufo.customUpdate();
+ // Update each laser
+ game.children.forEach(function (child) {
+ if (child instanceof Laser) {
+ child.update();
+ // Check for intersections with birds and UFO
+ birds.forEach(function (bird) {
+ if (!child.lastIntersecting && AABBIntersect(child, bird)) {
+ if (bird instanceof Bird1) {
+ score += 5;
+ } else if (bird instanceof Bird2 || bird instanceof Bird3) {
+ score += 1;
}
- child.lastIntersecting = birds.some(function (bird) {
- return child.intersects(bird);
- });
- });
- if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
- score += 25;
- scoreDisplay.updateScore(score);
+ scoreDisplay.updateScore(score); // Update the score display
+ bird.destroy(); // Remove the bird when hit by a laser
+ birds.splice(birds.indexOf(bird), 1); // Remove bird from the array
child.destroy();
}
child.lastIntersecting = birds.some(function (bird) {
return child.intersects(bird);
- }) || ufo && child.intersects(ufo);
- }
- });
- }
- };
- // Add a tree to the game
- var tree = new Tree();
- tree.x = 2048 / 2 + 600; // Move the tree 500px to the right
- tree.y = 2500; // Position the tree on the grass
- layerManager.addToLayer(tree, LAYERS.TREE);
- var score = 0;
- // Add stack1 image to the game
- var stack1 = new Stack1();
- stack1.x = 1700; // Move 700px to the right
- stack1.y = 2250; // Move down by 800px
- layerManager.addToLayer(stack1, LAYERS.STACK);
- // Add the grass floor to the game
- var grassBack = new GrassBack();
- grassBack.x = 1020;
- grassBack.y = 2735; // Position grassBack at the bottom
- layerManager.addToLayer(grassBack, LAYERS.GRASS_BACK);
- var grassFront = new GrassFront();
- grassFront.x = 1020;
- grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px
- layerManager.addToLayer(grassFront, LAYERS.GRASS_FRONT);
- var scoreDisplay = new Mirror();
- scoreDisplay.x = 2048 / 2 + 400;
- scoreDisplay.y = 2732 - 185 - 80 - 390 + 30;
- layerManager.addToLayer(scoreDisplay, LAYERS.UI);
- // Function to handle bgm1 end event
- ;
- // Add the cat to the game
- var cat = new Cat();
- cat.x = 230; // Move the cat 20px to the left
- cat.y = 2732; // Position the cat at the bottom of the screen
- layerManager.addToLayer(cat, LAYERS.CAT);
- game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value
- // Play bgm1 once on load and set a timer to replay it every 20-50 seconds
- LK.playMusic('bgm1', {
- loop: false,
- // Play once
- fade: {
- start: 0,
- end: 0.02,
- // Set to the lowest volume
- duration: 4000
- },
- onEnd: function onEnd() {
- // Set a timer to replay bgm1 every 20-50 seconds
- LK.setTimeout(function () {
- LK.playMusic('bgm1', {
- loop: false,
- // Play once
- fade: {
- start: 0,
- end: 0.5,
- // Set to the lowest volume
- duration: 4000
- },
- onEnd: onEnd // Set the onEnd function to replay bgm1
+ });
});
- }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds
- }
- });
- // Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds
- var wingsTimer = LK.setTimeout(function () {
- LK.getSound('wings1').play();
- wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000);
- }, Math.random() * 20000 + 10000);
- var CLOUD_TYPES = ['cloud1', 'cloud2'];
- var VOLUME_BGM1 = 0.02;
- var VOLUME_BREEZE1 = 0.02;
- var VOLUME_CRICKET1 = 0.02;
- var VOLUME_FROG1 = 0.02;
- var VOLUME_SONGBIRD1 = 0.02;
- var VOLUME_UFO1 = 0.02;
- var VOLUME_WINGS1 = 0.02;
- function clearGameTimers() {
- if (ufoTimer) {
- LK.clearTimeout(ufoTimer);
- }
- if (game.ufoSpawnTimer) {
- LK.clearTimeout(game.ufoSpawnTimer);
- }
- if (game.jetSpawnTimer) {
- LK.clearTimeout(game.jetSpawnTimer);
- }
- if (bgmTimer) {
- LK.clearTimeout(bgmTimer);
- }
- if (wingsTimer) {
- LK.clearTimeout(wingsTimer);
- }
- if (ambientSoundTimer) {
- LK.clearTimeout(ambientSoundTimer);
- }
- // Clean up UFO properly
- if (game.ufo) {
- game.removeChild(game.ufo);
- game.ufo = null;
- }
- // Clean up Jet properly
- if (game.jet) {
- game.removeChild(game.jet);
- game.jet = null;
- }
- }
- function clearGameTimers() {
- if (ufoTimer) {
- LK.clearTimeout(ufoTimer);
- }
- if (game.ufoSpawnTimer) {
- LK.clearTimeout(game.ufoSpawnTimer);
- }
- if (game.jetSpawnTimer) {
- LK.clearTimeout(game.jetSpawnTimer);
- }
- if (bgmTimer) {
- LK.clearTimeout(bgmTimer);
- }
- if (wingsTimer) {
- LK.clearTimeout(wingsTimer);
- }
- if (ambientSoundTimer) {
- LK.clearTimeout(ambientSoundTimer);
- }
- }
- function initTimers() {
- clearGameTimers();
- ufoTimer = LK.setTimeout(function () {
- if (!game.ufo && !game.ufoSpawnTimer) {
- var ufo = new UFO();
- ufo.x = Math.random() < 0.5 ? 2048 + ufo.width / 2 : -ufo.width / 2;
- ufo.y = Math.random() * (2732 / 2 - ufo.height);
- game.addChild(ufo);
- layerManager.addToLayer(ufo, LAYERS.UFO);
- game.ufo = ufo;
- }
- game.ufoSpawnTimer = LK.setTimeout(function () {
- game.ufoSpawnTimer = null;
- }, Math.random() * 10000 + 10000);
- }, Math.random() * 10000 + 10000);
- bgmTimer = LK.setTimeout(function () {
- var bgm = new BackgroundMusic();
- bgm.play();
- }, 1000);
- wingsTimer = LK.setTimeout(function () {
- LK.getSound('wings1').play();
- wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000);
- }, Math.random() * 20000 + 10000);
- var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1'];
- var currentAmbientSound = null;
- ambientSoundTimer = LK.setTimeout(function () {
- var soundId = sounds[Math.floor(Math.random() * sounds.length)];
- var sound = LK.getSound(soundId);
- if (sound) {
- if (currentAmbientSound) {
- currentAmbientSound.stop();
+ if (ufo && !child.lastIntersecting && AABBIntersect(child, ufo)) {
+ score += 25;
+ scoreDisplay.updateScore(score);
+ child.destroy();
}
- sound.play();
- currentAmbientSound = sound;
+ child.lastIntersecting = birds.some(function (bird) {
+ return child.intersects(bird);
+ }) || ufo && child.intersects(ufo);
}
- ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000);
- }, Math.random() * 20000 + 30000);
+ });
}
- var currentAmbientSound = null;
- var clouds = [];
- var birds = [];
- initGame();
- initTimers();
-}
\ No newline at end of file
+};
+// Add a tree to the game
+var tree = game.addChild(new Tree());
+tree.x = 2048 / 2 + 600; // Move the tree 500px to the right
+tree.y = 2500; // Position the tree on the grass
+// Ensure tree is added to the game before setting its index
+if (game.children.includes(tree) && game.children.includes(bird1)) {
+ game.setChildIndex(tree, game.getChildIndex(bird1) + 1); // Set tree to be rendered after bird1
+}
+var score = 0;
+// Add stack1 image to the game
+var stack1 = game.addChild(new Stack1());
+stack1.x = 1700; // Move 700px to the right
+stack1.y = 2250; // Move down by 800px
+// Ensure stack1 is added to the game before setting its index
+if (game.children.includes(stack1) && game.children.includes(tree)) {
+ game.setChildIndex(stack1, game.getChildIndex(tree) + 1); // Set stack1 to be rendered after tree
+}
+// Add the grass floor to the game
+var grassBack = new GrassBack();
+grassBack.x = 1020;
+grassBack.y = 2735; // Position grassBack at the bottom
+game.addChild(grassBack);
+// Ensure grassBack is added to the game before setting its index
+if (game.children.includes(grassBack)) {
+ game.setChildIndex(grassBack, 5); // Set grassBack to be rendered after Bird3
+}
+var grassFront = new GrassFront();
+grassFront.x = 1020;
+grassFront.y = 2785; // Position grassFront at the bottom, moved down by 50px
+game.addChild(grassFront);
+// Ensure grassFront is added to the game before setting its index
+if (game.children.includes(grassFront) && game.children.includes(bird2)) {
+ game.setChildIndex(grassFront, game.getChildIndex(bird2) + 1); // Set grassFront to be rendered after bird2
+}
+var scoreDisplay = new Mirror();
+scoreDisplay.x = 2048 / 2 + 400; // Move the score display 400px to the right
+scoreDisplay.y = 2732 - 185 - 80 - 390 + 30; // Move the score display up by an additional 80px and down by 30px
+game.addChild(scoreDisplay);
+var mirror = new Mirror();
+mirror.x = 2048 / 2 + 400; // Move the mirror 400px to the right
+mirror.y = 2732 - 185 - 80 - 390 + 30; // Move the mirror up by an additional 80px and down by 30px
+game.addChild(mirror);
+layerManager.addToLayer(mirror, LAYERS.MIRROR);
+// Ensure mirror is added to the game before setting its index
+if (game.children.includes(mirror) && game.children.includes(stack1)) {
+ game.setChildIndex(mirror, game.getChildIndex(stack1) + 1); // Set mirror to be rendered after stack1
+}
+// Ensure laser is added to the game before setting mirror's index
+if (game.children.includes(laser) && game.children.includes(mirror)) {
+ game.setChildIndex(mirror, game.getChildIndex(laser) + 1); // Set mirror to be rendered after the laser
+}
+// Function to handle bgm1 end event
+;
+// Add the cat to the game
+var cat = game.addChild(new Cat());
+cat.x = 230; // Move the cat 20px to the left
+cat.y = 2732; // Position the cat at the bottom of the screen
+// Ensure cat is added to the game before setting its index
+if (game.children.includes(cat)) {
+ game.setChildIndex(cat, game.children.length - 1); // Bring the cat to the front by setting its index to the highest value
+}
+// Play bgm1 once on load and set a timer to replay it every 20-50 seconds
+LK.playMusic('bgm1', {
+ loop: false,
+ // Play once
+ fade: {
+ start: 0,
+ end: 0.02,
+ // Set to the lowest volume
+ duration: 4000
+ },
+ onEnd: function onEnd() {
+ // Set a timer to replay bgm1 every 20-50 seconds
+ LK.setTimeout(function () {
+ LK.playMusic('bgm1', {
+ loop: false,
+ // Play once
+ fade: {
+ start: 0,
+ end: 0.5,
+ // Set to the lowest volume
+ duration: 4000
+ },
+ onEnd: onEnd // Set the onEnd function to replay bgm1
+ });
+ }, Math.random() * 30000 + 20000); // Random time between 20-50 seconds
+ }
+});
+// Initialize a timer to play the wings1 sound at a random time between 10 and 30 seconds
+var wingsTimer = LK.setTimeout(function () {
+ LK.getSound('wings1').play();
+ wingsTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 10000);
+}, Math.random() * 20000 + 10000);
+// Create an array for all sounds except ufo1, including all birdsong sounds
+var sounds = ['cricket1', 'frog1', 'wings1', 'songbird1'];
+var currentAmbientSound = null;
+// Display volumes of all sounds in the console
+// Sound volumes configuration
+// Initialize random timers for each sound to play between 30-50 seconds
+sounds.forEach(function (soundId) {
+ var sound = LK.getSound(soundId);
+ var ambientSoundTimer = LK.setTimeout(function () {
+ if (currentAmbientSound) {
+ currentAmbientSound.stop(); // Stop the currently playing ambient sound
+ }
+ sound.play();
+ currentAmbientSound = sound; // Set the current ambient sound
+ // Set a timeout to reset the current ambient sound after it finishes playing
+ LK.setTimeout(function () {
+ currentAmbientSound = null;
+ }, sound.duration * 1000); // Convert duration from seconds to milliseconds
+ // Reset the timer to play the sound again between 30-50 seconds, plus an additional 5 seconds
+ ambientSoundTimer = LK.setTimeout(arguments.callee, Math.random() * 20000 + 30000 + 5000);
+ }, Math.random() * 20000 + 30000);
+});
\ No newline at end of file
an orange and white cat facing away from the camera. the cat is sitting straight up and looking up, ready to pounce. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
remove black box
fluffy translucent cloud. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
bright sun with wincing cartoon face and a black eye. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a goofy ufo. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
red gaming reticle. Minimal. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
sunny day, hilly landscape. there is an alien invasion taking place in the distance. cities burning.
large AUTUMN SHADES tree with sparse bunches of leaves. branches are exposed, but the tree is tough and old.. true-color, realistic, Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
glowing orange sphere. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
sideway view of a fighter jet. . . In-Game 2d asset. transparent background. horizontal. No shadows.
shiny purple and black attack ufo.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows