User prompt
4. **Unnecessary Distance Calculation**: In the `Magic` class's `move` function, the distance is calculated twice using different methods. The first calculation is used for movement, and the second one is used for collision detection. The second calculation could use the already computed `distance` variable to check against the `hitThreshold`.
User prompt
move santa graphic down by 10
User prompt
1. **Calculate Collision**: Within the `Magic` class's `move` method, after moving the magic object towards Santa, check if the magic object's position intersects with Santa's position. This is typically done by comparing the bounding boxes of the two objects, which involves checking if the positions and dimensions of the two objects overlap. 2. **Trigger Game Over**: If a collision is detected (meaning the magic object intersects with Santa), you would call a method within the `Game` class that handles the game over logic. This method would perform any necessary cleanup and display the game over screen. 3. **Update Game State**: The game over method would set the `isGameOver` property of the `Game` class to `true` and could use the provided `LK.showGameOver()` function to display the game over screen. 4. **Stop Game Logic**: Once the game over state is triggered, ensure that no further game logic is processed by checking the `isGameOver` state in your game's tick function or other relevant places.
User prompt
// Inside the Magic class's move method self.move = function () { // ... existing movement logic ... // Check for collision with Santa if (self.intersects(self.gameInstance.santa)) { // If there is a collision, trigger the game over logic self.gameInstance.triggerGameOver(); } }; // Inside the Game class this.triggerGameOver = function() { // Set the game over state this.isGameOver = true; // Display the game over screen LK.showGameOver(); // Perform any additional cleanup if necessary };
User prompt
fix it
User prompt
The collision detection uses `magic.width` which may not be the correct way to reference the width of the Magic object's graphic. It should likely be `magicGraphics.width` if `magicGraphics` is the actual PIXI graphics object.
User prompt
make sure the magic object is on the same layer or z-index as Santa
User prompt
make sure the coordinates or anchor points for santa and Magic graphics are set correctly, the visual representation of the objects should match their actual positions in the game world, leading to proper collisions.
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'santaGraphics')' in this line: 'var santaX = this.santa.santaGraphics.x;' Line Number: 263
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'santaGraphics')' in this line: 'var santaX = this.santa.santaGraphics.x;' Line Number: 263
User prompt
the collision detection logic is placed within the `move` function of the `Magic` class. If the `move` function is not being called correctly within the `Game` class's tick event, the collision detection will not occur.
User prompt
Implement the missing checkMagicCollisionWithSanta method in the Game class to handle the collision between magic and Santa graphic
User prompt
The method `checkMagicCollisionWithSanta` is called when a collision is detected, but it's not clear if this method exists or is implemented correctly within the `Game` class. If the method is missing or not implemented correctly, the collision will not have the intended effect.
User prompt
The hit threshold is set to 100, which may not be the correct value for detecting collisions based on the actual sizes of the `magic` and `santaGraphics` sprites. If the sprites are smaller or the magic is moving too fast, it might skip over the area where the collision would be detected.
User prompt
The collision detection logic uses `self.gameInstance.santa.x` and `self.gameInstance.santa.y` for Santa's position, which might not be the correct coordinates if `santa` is a container that has `santaGraphics` as a child. The actual position of the graphic might be offset within the `santa` container. The collision logic should use the position of `santaGraphics` instead if that's the actual sprite that should be checked for collision.
User prompt
magic class should check collision with santa's graphic not santa himself
User prompt
The `Magic` class checks for collision with Santa and sets `isGameOver` to true directly. It should check with santa'sg raphic
User prompt
**Magic Class - Collision Detection**: The `Magic` class checks for collision with Santa and sets `isGameOver` to true directly. This logic should ideally be handled within the `Game` class to centralize game state management.
User prompt
fix the reindeer not flipping it's X when bouncing on edge of playspace
User prompt
fix any logical errors or redundancy you can find: var Shield = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.x = x; self.y = y; self.on('down', function () { self.destroy(); }); }); var ObstacleBox = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var obstacleBoxGraphics = self.createAsset('obstacleBox', 'Obstacle Box', 0.5, 0.5); self.x = x; self.y = y; }); var Magic = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.moveEnabled = true; self.rotation = 0; var magicGraphics = self.createAsset('magic', 'Magic effect', 0.5, 0.5); self.x = x; self.y = y; self.move = function () { if (self.moveEnabled) { var targetX = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.x : 2048; var targetY = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.y : 2732; var dx = targetX - self.x; var dy = targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); var speed = 10; if (distance > 0) { var normX = dx / distance; var normY = dy / distance; self.x += normX * speed; self.y += normY * speed; } if (self.gameInstance.santa && self.gameInstance.santa.santaGraphics) { var santaX = self.gameInstance.santa.santaGraphics.x; var santaY = self.gameInstance.santa.santaGraphics.y; var distance = Math.sqrt(Math.pow(self.x - santaX, 2) + Math.pow(self.y - santaY, 2)); var hitThreshold = 100; if (distance <= hitThreshold) { console.log("Magic position:", self.x, self.y, "Santa position:", santaX, santaY, "Distance:", distance, "Collision detected:", distance <= hitThreshold); self.gameInstance.isGameOver = true; } } else if ((self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) && self.parent) { if (self.gameInstance && typeof self.gameInstance.activeMagicCount !== 'undefined') { self.gameInstance.activeMagicCount--; } self.destroy(); } } }; self.on('down', function () { LK.setScore(LK.getScore() + 2); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var tingText = new Text2('Ting!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); tingText.x = self.x; tingText.y = self.y; tingText.anchor.set(0.5, 0.5); self.parent.addChild(tingText); LK.setTimeout(function () { tingText.destroy(); }, 1000); self.gameInstance.activeMagicCount--; self.destroy(); }); }); var Reindeer = Container.expand(function (gameInstance) { var self = Container.call(this); self.moveInArc = function () { self.arcAngle += self.arcSpeed * self.arcDirection; self.x = self.gameInstance.x + Math.cos(self.arcAngle) * self.arcRadius; self.y = self.gameInstance.y + Math.sin(self.arcAngle) * self.arcRadius; }; self.moveHorizontally = function () { self.x += self.speed * self.direction; if (self.x > 2048 || self.x < 0) { self.direction *= -1; self.x = Math.max(0, Math.min(self.x, 2048)); } }; self.fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.01; } else if (self.gameInstance) { self.gameInstance.respawnReindeer(self); } }; self.gameInstance = gameInstance; var reindeerGraphics = self.createAsset('reindeer', 'Reindeer', 0.5, 0.5); self.addChild(reindeerGraphics); self.speed = 10; self.direction = Math.random() < 0.5 ? 1 : -1; self.x = self.direction === 1 ? 0 : 2048; self.scale.x = self.direction; self.on('down', function (obj) { LK.setScore(LK.getScore() + 25); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var greatText = new Text2('Great!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = self.x; greatText.y = self.y; greatText.anchor.set(0.5, 0.5); self.parent.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); self.parent.respawnReindeer(); self.destroy(); }); }); var RandomText = Container.expand(function () { var self = Container.call(this); self.gameInstance = null; self.display = function (x, y, messages) { var message = messages[Math.floor(Math.random() * messages.length)]; var text = new Text2(message, { size: 48, fill: '#ff0000', font: 'Arial Black' }); text.x = x - 400; text.y = y + 175; text.anchor.set(0.5, 0.5); self.addChild(text); LK.setTimeout(function () { text.destroy(); self.parent.randomTextActive = false; }, 3000); }; }); var BadElf = Container.expand(function (spawnX, spawnY, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.spawnedMagicCount = 1; self.magicShootingInterval = LK.setInterval(function () { if (self.visible && self.gameInstance.activeMagicCount < self.gameInstance.maxMagicAllowed) { var magic = new Magic(self.x + 125, self.y, self.gameInstance); self.gameInstance.addChild(magic); self.gameInstance.activeMagicCount++; self.spawnedMagicCount++; if (self.spawnedMagicCount >= 2) { self.alpha = 1; var fadeOutInterval; var fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.05; } else { LK.clearInterval(fadeOutInterval); self.gameInstance.activeBadElves--; self.destroy(); LK.setTimeout(function () { self.gameInstance.spawnBadElf(); }, 2000); } }; LK.clearInterval(self.magicShootingInterval); fadeOutInterval = LK.setInterval(fadeOut, 60); self.magicShootingInterval = null; } } }, Math.random() * (6000 - 2000) + 2000); var badElfGraphics = self.createAsset('badElf', 'Mischievous Elf', 0.5, 0.5); self.x = spawnX; self.y = spawnY; self.on('down', function (obj) { LK.setScore(LK.getScore() + 3); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); if (self.gameInstance) { self.gameInstance.activeBadElves--; } var obstacleBox = new ObstacleBox(self.x, self.y, self.gameInstance); obstacleBox.x = self.x; obstacleBox.y = self.y; self.parent.addChild(obstacleBox); LK.setTimeout(function () { obstacleBox.destroy(); }, 1000); LK.clearInterval(self.magicShootingInterval); self.destroy(); }); }); var Mistletoe = Container.expand(function (gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var mistletoeGraphics = self.createAsset('mistletoe', 'Mistletoe', 0.5, 0.5); var scale = Math.random() * 0.15 + 0.85; mistletoeGraphics.scale.set(scale); var bobbingRange = 10; var bobbingSpeed = 0.2; var bobbingPosition = 0; self.bob = function () { bobbingPosition += bobbingSpeed; self.y += Math.sin(bobbingPosition) * bobbingRange; }; self.on('down', function (obj) { LK.setScore(LK.getScore() + 10); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); self.parent.respawnMistletoe(self); }); }); var Snowflake = Container.expand(function () { var self = Container.call(this); var sizeFactor = Math.random() * 0.5 + 0.5; var snowflakeGraphics = self.createAsset('snowflake', 'Snowflake', 0.5, 0.5); snowflakeGraphics.scale.set(sizeFactor); self.speed = -3 * sizeFactor; self.move = function () { self.x += self.speed; self.y += 0.5; self.scale.x -= 0.003; self.scale.y -= 0.003; if (self.scale.x <= 0 || self.scale.y <= 0) { self.destroy(); } }; }); var Heart = Container.expand(function () { var self = Container.call(this); var heartGraphics = self.createAsset('heart', 'Player life', 0.5, 0.5); self.x = heartGraphics.width / 2; self.y = heartGraphics.height / 2; }); var MuzzleFlash = Container.expand(function () { var self = Container.call(this); self.visible = false; var flashGraphics = self.createAsset('muzzleFlash', 'Muzzle flash effect', .5, .5); self.show = function () { var randomScale = Math.random() * 0.125 + 0.375; self.scale.set(randomScale); self.visible = true; LK.setTimeout(function () { self.visible = false; self.scale.set(1); }, 100); }; }); var Santa = Container.expand(function () { var self = Container.call(this); self.santaGraphics = self.createAsset('santa', 'Santa character', .5, .5); self.move = function () {}; }); var Crosshair = Container.expand(function () { var self = Container.call(this); self.move = function (event) { var pos = event.getLocalPosition(self.parent); self.x = pos.x; self.y = pos.y; }; var crosshairGraphics = self.createAsset('crosshair', 'Crosshair', .5, .5); self.y = -crosshairGraphics.height / 2; }); var Game = Container.expand(function () { var self = Container.call(this); this.isGameOver = false; this.activeMagicCount = 0; this.magicSpawnX = 1024; this.magicSpawnY = 1366; self.maxMagicAllowed = 3; if (self.activeMagicCount < self.maxMagicAllowed) { var magicInstance = new Magic(this.magicSpawnX, this.magicSpawnY, self); self.addChild(magicInstance); self.activeMagicCount++; } self.respawnReindeer = function (reindeer) { LK.setTimeout(function () { var newReindeer = new Reindeer(self); var spawnLeftSide = Math.random() < 0.5; newReindeer.x = spawnLeftSide ? 0 : 2048; newReindeer.y = Math.random() * (2732 / 4 - newReindeer.height) + newReindeer.height; newReindeer.arcDirection = spawnLeftSide ? 1 : -1; newReindeer.arcRadius = 2048 / 2; newReindeer.arcAngle = spawnLeftSide ? Math.PI : 0; var scale = Math.random() * 0.25 + 0.75; newReindeer.scale.set(scale); self.addChild(newReindeer); LK.on('tick', function () { newReindeer.moveHorizontally(); }); }, Math.random() < 0.5 ? 8000 : 12000); }; self.showGreatText = function (x, y) { var greatText = new Text2('Nice!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = x; greatText.y = y; greatText.anchor.set(0.5, 0.5); self.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); }; var silentNightText = new Text2('Silent Night', { size: 150, fill: "#ffffff" }); silentNightText.anchor.set(0.5, 0); silentNightText.x = 5 / 2; silentNightText.y = 150; LK.gui.topCenter.addChild(silentNightText); self.generateSnowflakes = function () { var snowflakeInterval = LK.setInterval(function () { var snowflake = new Snowflake(); snowflake.x = Math.random() * 2048; snowflake.y = Math.random() * 2732; self.addChild(snowflake); }, 1000); }; LK.setTimeout(function () { var fadeOutDuration = 2000; var fadeOutStep = 0.05; var fadeOutInterval = LK.setInterval(function () { silentNightText.alpha -= fadeOutStep; if (silentNightText.alpha <= 0) { LK.clearInterval(fadeOutInterval); silentNightText.destroy(); self.score = 0; self.scoreText = new Text2(self.score.toString(), { size: 150, fill: "#ffffff" }); self.scoreText.anchor.set(0.5, 0); self.scoreText.x = 5 / 2; self.scoreText.y = 150; LK.gui.topCenter.addChild(self.scoreText); self.updateScore = function (score) { self.score += score; self.scoreText.setText(self.score.toString()); LK.setScore(self.score); }; } }, fadeOutDuration * fadeOutStep); self.spawnBadElf(); LK.setInterval(self.spawnBadElf, Math.random() * (6000 - 3000) + 3000); self.generateMistletoes = function () { LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 500) + boundaryOffset + 200; self.addChild(newMistletoe); }, 8000); }; LK.setTimeout(function () { var reindeer = new Reindeer(self); var boundaryOffset = 200; reindeer.x = Math.random() < 0.5 ? boundaryOffset : 2048 - boundaryOffset - reindeer.width; reindeer.y = Math.random() * (2732 / 4 - boundaryOffset) + boundaryOffset; reindeer.arcDirection = Math.random() < 0.5 ? 1 : -1; reindeer.arcRadius = 2048 / 2; reindeer.arcAngle = Math.random() < 0.5 ? Math.PI : 0; var initialScale = Math.random() * 0.25 + 0.75; reindeer.scale.set(initialScale); self.addChild(reindeer); LK.on('tick', function () { if (reindeer) reindeer.moveHorizontally(); }); }, 12000); LK.setTimeout(self.generateMistletoes, 8000); }, 2000); stage.on('move', function (obj) { crosshair.move(obj.event); }); stage.on('down', function (obj) { if (hearts.length > 0) { muzzleFlash.show(); leftClickPressed = true; lastClickTime = LK.ticks; if (!barnMovedDown) { self.children.forEach(function (child) { if (child instanceof BadElf) { child.y += 100; } }); barnBackground.y += 460; room1Background.y += 100; boxBackground.y += 100; crateBackground.y += 100; missBackground.y += 460; tireBackground.y += 100; barnMovedDown = true; if (Math.random() < 0.10 && santa && santa.santaGraphics) { var messages = ["Santa's special delivery!", "This is for the naughty list!", "Ho, ho, ho-hold this!"]; if (!self.randomTextActive) { self.randomTextActive = true; var randomText = new RandomText(); randomText.display(santa.x, santa.y - santa.santaGraphics.height / 2, messages); self.addChild(randomText); } } } var heart = hearts.pop(); heart.destroy(); } self.children.forEach(function (child) { if (child instanceof BadElf && leftClickPressed && LK.ticks - lastClickTime > 30) { child.y -= 100; } }); }); var room1Background = self.createAsset('Room1', 'Room1 background', 0, 0); room1Background.anchor.set(0.5, 0.5); room1Background.x = 2048 / 2; room1Background.y = 2732 / 2; self.addChild(room1Background); var tireBackground = self.createAsset('Tire', 'Tire background', 0.5, 0.5); tireBackground.anchor.set(0.5, 0.5); tireBackground.x = 700; tireBackground.y = 1825; self.addChild(tireBackground); self.respawnMistletoe = function (mistletoe) { this.showGreatText(mistletoe.x, mistletoe.y); mistletoe.destroy(); LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 100 - 500) + boundaryOffset + 300; self.addChild(newMistletoe); }, Math.random() * (10000 - 5000) + 5000); }; self.generateSnowflakes(); var missBackground = self.createAsset('Miss', 'Miss background', 0, 0); missBackground.anchor.set(0.5, 0.5); missBackground.x = 700; missBackground.y = 2520; var crateBackground = self.createAsset('Crate', 'Crate background', 0, 0); crateBackground.anchor.set(0, 1); crateBackground.x = 205; crateBackground.y = 2645; self.addChild(crateBackground); var boxBackground = self.createAsset('Box', 'Box background', 0, 0); boxBackground.anchor.set(0, 1); boxBackground.x = 1350; boxBackground.y = 2200; self.addChild(boxBackground); var barnBackground = self.createAsset('Barn', 'Barn background', 0, 0); barnBackground.anchor.set(0.5, 0.5); barnBackground.x = 2048 / 2; barnBackground.y = 2732 - barnBackground.height / 4 + 900; self.addChild(barnBackground); self.addChild(missBackground); var santa = self.addChild(new Santa()); santa.x = 2048 / 2 + 650; santa.y = 2732 - santa.height / 2; var shield = new Shield(santa.x, santa.y - 100, self); self.addChild(shield); self.addChild(santa); var spawnLocations = [{ x: tireBackground.x + 175, y: tireBackground.y - 65 }, { x: boxBackground.x, y: boxBackground.y - 350 }, { x: crateBackground.x + 250, y: crateBackground.y - 600 }]; var usedSpawnLocations = []; var spawnLocationCounter = 0; self.activeBadElves = 0; self.spawnBadElf = function () { if (self.scoreText && self.activeBadElves < 2) { if (usedSpawnLocations.length === spawnLocations.length) { usedSpawnLocations = []; spawnLocationCounter = 0; } var availableLocations = spawnLocations.filter(function (location, index) { return usedSpawnLocations.indexOf(index) === -1; }); var locationIndex = Math.floor(Math.random() * availableLocations.length); var location = availableLocations[locationIndex]; usedSpawnLocations.push(spawnLocations.indexOf(location)); spawnLocationCounter++; var badElf = new BadElf(location.x, location.y, self); var scaleModifier = Math.random() * 0.25 + 0.75; badElf.scale.set(scaleModifier, scaleModifier); self.addChildAt(badElf, self.getChildIndex(room1Background) + 1); var magic = new Magic(badElf.x + badElf.width / 2, badElf.y, self); self.addChild(magic); self.activeBadElves++; var bulletStartY = badElf.y - badElf.height; } }; var muzzleFlash = new MuzzleFlash(); muzzleFlash.x = santa.x - santa.santaGraphics.width + 400; muzzleFlash.y = santa.y - 90; self.addChild(muzzleFlash); self.addChild(santa); var crosshair = self.addChild(new Crosshair()); crosshair.x = 2048 / 2; crosshair.y = 2732 / 2; var isGameOver = false; var tickOffset = 0; var leftClickPressed = false; var barnMovedDown = false; var lastClickTime = 0; var playerLives = 6; var hearts = []; for (var i = 0; i < playerLives; i++) { var heart = new Heart(); heart.x = 1125 + i * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } LK.on('tick', function () { if (santa) santa.move(); for (var i = 0; i < self.children.length; i++) { var child = self.children[i]; if (child instanceof Snowflake) { child.move(); } else if (child instanceof Mistletoe) { child.bob(); } else if (child instanceof Magic) { child.move(); } } if (self.isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); self.isGameOver = false; } if (leftClickPressed && LK.ticks - lastClickTime > 30) { barnBackground.y -= 460; room1Background.y -= 100; boxBackground.y -= 100; crateBackground.y -= 100; missBackground.y -= 460; tireBackground.y -= 100; self.children.forEach(function (child) { if (child instanceof BadElf && barnMovedDown) { child.y -= 100; } }); leftClickPressed = false; barnMovedDown = false; while (hearts.length < playerLives) { var heart = new Heart(); heart.x = 1125 + hearts.length * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } } }); });
User prompt
fix any logical errors, var Shield = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.x = x; self.y = y; self.on('down', function () { self.destroy(); }); }); var ObstacleBox = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var obstacleBoxGraphics = self.createAsset('obstacleBox', 'Obstacle Box', 0.5, 0.5); self.x = x; self.y = y; }); var Magic = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.moveEnabled = true; self.rotation = 0; var magicGraphics = self.createAsset('magic', 'Magic effect', 0.5, 0.5); self.x = x; self.y = y; self.move = function () { if (self.moveEnabled) { var targetX = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.x : 2048; var targetY = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.y : 2732; var dx = targetX - self.x; var dy = targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); var speed = 10; if (distance > 0) { var normX = dx / distance; var normY = dy / distance; self.x += normX * speed; self.y += normY * speed; } if (self.gameInstance.santa) { var santaX = self.gameInstance.santa.santaGraphics.x; var santaY = self.gameInstance.santa.santaGraphics.y; var distance = Math.sqrt(Math.pow(self.x - santaX, 2) + Math.pow(self.y - santaY, 2)); var hitThreshold = 100; if (distance <= hitThreshold) { console.log("Magic position:", self.x, self.y, "Santa position:", santaX, santaY, "Distance:", distance, "Collision detected:", distance <= hitThreshold); self.gameInstance.isGameOver = true; } } else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { if (self.parent) { if (self.gameInstance) { if (self.gameInstance && typeof self.gameInstance.activeMagicCount !== 'undefined') { self.gameInstance.activeMagicCount--; } } self.destroy(); } } } }; self.on('down', function () { LK.setScore(LK.getScore() + 2); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var tingText = new Text2('Ting!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); tingText.x = self.x; tingText.y = self.y; tingText.anchor.set(0.5, 0.5); self.parent.addChild(tingText); LK.setTimeout(function () { tingText.destroy(); }, 1000); self.gameInstance.activeMagicCount--; self.destroy(); }); }); var Reindeer = Container.expand(function (gameInstance) { var self = Container.call(this); self.moveInArc = function () { self.arcAngle += self.arcSpeed * self.arcDirection; self.x = self.gameInstance.x + Math.cos(self.arcAngle) * self.arcRadius; self.y = self.gameInstance.y + Math.sin(self.arcAngle) * self.arcRadius; }; self.moveHorizontally = function () { self.x += self.speed * self.direction; if (self.x > 2048 || self.x < 0) { self.direction *= -1; self.x = Math.max(0, Math.min(self.x, 2048)); } }; self.fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.01; } else { self.gameInstance.respawnReindeer(self); } }; self.gameInstance = gameInstance; var reindeerGraphics = self.createAsset('reindeer', 'Reindeer', 0.5, 0.5); self.addChild(reindeerGraphics); self.speed = 10; self.direction = Math.random() < 0.5 ? 1 : -1; self.x = self.direction === 1 ? 0 : 2048; self.scale.x = self.direction; self.on('down', function (obj) { LK.setScore(LK.getScore() + 25); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var greatText = new Text2('Great!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = self.x; greatText.y = self.y; greatText.anchor.set(0.5, 0.5); self.parent.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); self.parent.respawnReindeer(); self.destroy(); }); }); var RandomText = Container.expand(function () { var self = Container.call(this); self.gameInstance = null; self.display = function (x, y, messages) { var message = messages[Math.floor(Math.random() * messages.length)]; var text = new Text2(message, { size: 48, fill: '#ff0000', font: 'Arial Black' }); text.x = x - 400; text.y = y + 175; text.anchor.set(0.5, 0.5); self.addChild(text); LK.setTimeout(function () { text.destroy(); self.parent.randomTextActive = false; }, 3000); }; }); var BadElf = Container.expand(function (spawnX, spawnY, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.spawnedMagicCount = 1; self.magicShootingInterval = LK.setInterval(function () { if (self.visible && self.gameInstance.activeMagicCount < self.gameInstance.maxMagicAllowed) { var magic = new Magic(self.x + 125, self.y, self.gameInstance); self.gameInstance.addChild(magic); self.gameInstance.activeMagicCount++; self.spawnedMagicCount++; if (self.spawnedMagicCount >= 2) { self.alpha = 1; var fadeOutInterval; var fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.05; } else { LK.clearInterval(fadeOutInterval); self.gameInstance.activeBadElves--; self.destroy(); LK.setTimeout(function () { self.gameInstance.spawnBadElf(); }, 2000); } }; LK.clearInterval(self.magicShootingInterval); fadeOutInterval = LK.setInterval(fadeOut, 60); self.magicShootingInterval = null; } } }, Math.random() * (6000 - 2000) + 2000); var badElfGraphics = self.createAsset('badElf', 'Mischievous Elf', 0.5, 0.5); self.x = spawnX; self.y = spawnY; self.on('down', function (obj) { LK.setScore(LK.getScore() + 3); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); self.parent.activeBadElves--; var obstacleBox = new ObstacleBox(self.x, self.y, self.gameInstance); obstacleBox.x = self.x; obstacleBox.y = self.y; self.parent.addChild(obstacleBox); LK.setTimeout(function () { obstacleBox.destroy(); }, 1000); LK.clearInterval(self.magicShootingInterval); self.destroy(); }); }); var Mistletoe = Container.expand(function (gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var mistletoeGraphics = self.createAsset('mistletoe', 'Mistletoe', 0.5, 0.5); var scale = Math.random() * 0.15 + 0.85; mistletoeGraphics.scale.set(scale); var bobbingRange = 10; var bobbingSpeed = 0.2; var bobbingPosition = 0; self.bob = function () { bobbingPosition += bobbingSpeed; self.y += Math.sin(bobbingPosition) * bobbingRange; }; self.on('down', function (obj) { LK.setScore(LK.getScore() + 10); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); self.parent.respawnMistletoe(self); }); }); var Snowflake = Container.expand(function () { var self = Container.call(this); var sizeFactor = Math.random() * 0.5 + 0.5; var snowflakeGraphics = self.createAsset('snowflake', 'Snowflake', 0.5, 0.5); snowflakeGraphics.scale.set(sizeFactor); self.speed = -3 * sizeFactor; self.move = function () { self.x += self.speed; self.y += 0.5; self.scale.x -= 0.003; self.scale.y -= 0.003; if (self.scale.x <= 0 || self.scale.y <= 0) { self.destroy(); } }; }); var Heart = Container.expand(function () { var self = Container.call(this); var heartGraphics = self.createAsset('heart', 'Player life', 0.5, 0.5); self.x = heartGraphics.width / 2; self.y = heartGraphics.height / 2; }); var MuzzleFlash = Container.expand(function () { var self = Container.call(this); self.visible = false; var flashGraphics = self.createAsset('muzzleFlash', 'Muzzle flash effect', .5, .5); self.show = function () { var randomScale = Math.random() * 0.125 + 0.375; self.scale.set(randomScale); self.visible = true; LK.setTimeout(function () { self.visible = false; self.scale.set(1); }, 100); }; }); var Santa = Container.expand(function () { var self = Container.call(this); self.santaGraphics = self.createAsset('santa', 'Santa character', .5, .5); self.move = function () {}; }); var Crosshair = Container.expand(function () { var self = Container.call(this); self.move = function (event) { var pos = event.getLocalPosition(self.parent); self.x = pos.x; self.y = pos.y; }; var crosshairGraphics = self.createAsset('crosshair', 'Crosshair', .5, .5); self.y = -crosshairGraphics.height / 2; }); var Game = Container.expand(function () { var self = Container.call(this); this.isGameOver = false; this.activeMagicCount = 0; this.magicSpawnX = 1024; this.magicSpawnY = 1366; self.maxMagicAllowed = 3; if (self.activeMagicCount < self.maxMagicAllowed) { var magicInstance = new Magic(this.magicSpawnX, this.magicSpawnY, self); self.addChild(magicInstance); self.activeMagicCount++; } self.respawnReindeer = function (reindeer) { LK.setTimeout(function () { var newReindeer = new Reindeer(self); var spawnLeftSide = Math.random() < 0.5; newReindeer.x = spawnLeftSide ? 0 : 2048; newReindeer.y = Math.random() * (2732 / 4 - newReindeer.height) + newReindeer.height; newReindeer.arcDirection = spawnLeftSide ? 1 : -1; newReindeer.arcRadius = 2048 / 2; newReindeer.arcAngle = spawnLeftSide ? Math.PI : 0; var scale = Math.random() * 0.25 + 0.75; newReindeer.scale.set(scale); self.addChild(newReindeer); LK.on('tick', function () { newReindeer.moveHorizontally(); }); }, Math.random() < 0.5 ? 8000 : 12000); }; self.showGreatText = function (x, y) { var greatText = new Text2('Nice!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = x; greatText.y = y; greatText.anchor.set(0.5, 0.5); self.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); }; var silentNightText = new Text2('Silent Night', { size: 150, fill: "#ffffff" }); silentNightText.anchor.set(0.5, 0); silentNightText.x = 5 / 2; silentNightText.y = 150; LK.gui.topCenter.addChild(silentNightText); self.generateSnowflakes = function () { var snowflakeInterval = LK.setInterval(function () { var snowflake = new Snowflake(); snowflake.x = Math.random() * 2048; snowflake.y = Math.random() * 2732; self.addChild(snowflake); }, 1000); }; LK.setTimeout(function () { var fadeOutDuration = 2000; var fadeOutStep = 0.05; var fadeOutInterval = LK.setInterval(function () { silentNightText.alpha -= fadeOutStep; if (silentNightText.alpha <= 0) { LK.clearInterval(fadeOutInterval); silentNightText.destroy(); self.score = 0; self.scoreText = new Text2(self.score.toString(), { size: 150, fill: "#ffffff" }); self.scoreText.anchor.set(0.5, 0); self.scoreText.x = 5 / 2; self.scoreText.y = 150; LK.gui.topCenter.addChild(self.scoreText); self.updateScore = function (score) { self.score += score; self.scoreText.setText(self.score.toString()); LK.setScore(self.score); }; } }, fadeOutDuration * fadeOutStep); self.spawnBadElf(); LK.setInterval(self.spawnBadElf, Math.random() * (6000 - 3000) + 3000); self.generateMistletoes = function () { LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 500) + boundaryOffset + 200; self.addChild(newMistletoe); }, 8000); }; LK.setTimeout(function () { var reindeer = new Reindeer(self); var boundaryOffset = 200; reindeer.x = Math.random() < 0.5 ? boundaryOffset : 2048 - boundaryOffset - reindeer.width; reindeer.y = Math.random() * (2732 / 4 - boundaryOffset) + boundaryOffset; reindeer.arcDirection = Math.random() < 0.5 ? 1 : -1; reindeer.arcRadius = 2048 / 2; reindeer.arcAngle = Math.random() < 0.5 ? Math.PI : 0; var initialScale = Math.random() * 0.25 + 0.75; reindeer.scale.set(initialScale); self.addChild(reindeer); LK.on('tick', function () { if (reindeer) reindeer.moveHorizontally(); }); }, 12000); LK.setTimeout(self.generateMistletoes, 8000); }, 2000); stage.on('move', function (obj) { crosshair.move(obj.event); }); stage.on('down', function (obj) { if (hearts.length > 0) { muzzleFlash.show(); leftClickPressed = true; lastClickTime = LK.ticks; if (!barnMovedDown) { self.children.forEach(function (child) { if (child instanceof BadElf) { child.y += 100; } }); barnBackground.y += 460; room1Background.y += 100; boxBackground.y += 100; crateBackground.y += 100; missBackground.y += 460; tireBackground.y += 100; barnMovedDown = true; if (Math.random() < 0.10) { var messages = ["Santa's special delivery!", "This is for the naughty list!", "Ho, ho, ho-hold this!"]; if (!self.randomTextActive) { self.randomTextActive = true; var randomText = new RandomText(); randomText.display(santa.x, santa.y - santa.santaGraphics.height / 2, messages); self.addChild(randomText); } } } var heart = hearts.pop(); heart.destroy(); } self.children.forEach(function (child) { if (child instanceof BadElf && leftClickPressed && LK.ticks - lastClickTime > 30) { child.y -= 100; } }); }); var room1Background = self.createAsset('Room1', 'Room1 background', 0, 0); room1Background.anchor.set(0.5, 0.5); room1Background.x = 2048 / 2; room1Background.y = 2732 / 2; self.addChild(room1Background); var tireBackground = self.createAsset('Tire', 'Tire background', 0.5, 0.5); tireBackground.anchor.set(0.5, 0.5); tireBackground.x = 700; tireBackground.y = 1825; self.addChild(tireBackground); self.respawnMistletoe = function (mistletoe) { this.showGreatText(mistletoe.x, mistletoe.y); mistletoe.destroy(); LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 100 - 500) + boundaryOffset + 300; self.addChild(newMistletoe); }, Math.random() * (10000 - 5000) + 5000); }; self.generateSnowflakes(); var missBackground = self.createAsset('Miss', 'Miss background', 0, 0); missBackground.anchor.set(0.5, 0.5); missBackground.x = 700; missBackground.y = 2520; var crateBackground = self.createAsset('Crate', 'Crate background', 0, 0); crateBackground.anchor.set(0, 1); crateBackground.x = 205; crateBackground.y = 2645; self.addChild(crateBackground); var boxBackground = self.createAsset('Box', 'Box background', 0, 0); boxBackground.anchor.set(0, 1); boxBackground.x = 1350; boxBackground.y = 2200; self.addChild(boxBackground); var barnBackground = self.createAsset('Barn', 'Barn background', 0, 0); barnBackground.anchor.set(0.5, 0.5); barnBackground.x = 2048 / 2; barnBackground.y = 2732 - barnBackground.height / 4 + 900; self.addChild(barnBackground); self.addChild(missBackground); var santa = self.addChild(new Santa()); santa.x = 2048 / 2 + 650; santa.y = 2732 - santa.height / 2; var shield = new Shield(santa.x, santa.y - 100, self); self.addChild(shield); self.addChild(santa); var spawnLocations = [{ x: tireBackground.x + 175, y: tireBackground.y - 65 }, { x: boxBackground.x, y: boxBackground.y - 350 }, { x: crateBackground.x + 250, y: crateBackground.y - 600 }]; var usedSpawnLocations = []; var spawnLocationCounter = 0; self.activeBadElves = 0; self.spawnBadElf = function () { if (self.scoreText && self.activeBadElves < 2) { if (usedSpawnLocations.length === spawnLocations.length) { usedSpawnLocations = []; spawnLocationCounter = 0; } var availableLocations = spawnLocations.filter(function (location, index) { return usedSpawnLocations.indexOf(index) === -1; }); var locationIndex = Math.floor(Math.random() * availableLocations.length); var location = availableLocations[locationIndex]; usedSpawnLocations.push(spawnLocations.indexOf(location)); spawnLocationCounter++; var badElf = new BadElf(location.x, location.y, self); var scaleModifier = Math.random() * 0.25 + 0.75; badElf.scale.set(scaleModifier, scaleModifier); self.addChildAt(badElf, self.getChildIndex(room1Background) + 1); var magic = new Magic(badElf.x + badElf.width / 2, badElf.y, self); self.addChild(magic); self.activeBadElves++; var bulletStartY = badElf.y - badElf.height; } }; var muzzleFlash = new MuzzleFlash(); muzzleFlash.x = santa.x - santa.santaGraphics.width + 400; muzzleFlash.y = santa.y - 90; self.addChild(muzzleFlash); self.addChild(santa); var crosshair = self.addChild(new Crosshair()); crosshair.x = 2048 / 2; crosshair.y = 2732 / 2; var isGameOver = false; var tickOffset = 0; var leftClickPressed = false; var barnMovedDown = false; var lastClickTime = 0; var playerLives = 6; var hearts = []; for (var i = 0; i < playerLives; i++) { var heart = new Heart(); heart.x = 1125 + i * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } LK.on('tick', function () { if (santa) santa.move(); for (var i = 0; i < self.children.length; i++) { var child = self.children[i]; if (child instanceof Snowflake) { child.move(); } else if (child instanceof Mistletoe) { child.bob(); } else if (child instanceof Magic) { child.move(); } } if (self.isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); self.isGameOver = false; } if (leftClickPressed && LK.ticks - lastClickTime > 30) { barnBackground.y -= 460; room1Background.y -= 100; boxBackground.y -= 100; crateBackground.y -= 100; missBackground.y -= 460; tireBackground.y -= 100; self.children.forEach(function (child) { if (child instanceof BadElf && barnMovedDown) { child.y -= 100; } }); leftClickPressed = false; barnMovedDown = false; while (hearts.length < playerLives) { var heart = new Heart(); heart.x = 1125 + hearts.length * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } } }); });
User prompt
Fix Bug: 'TypeError: santa.move is not a function' in this line: 'if (santa) santa.move();' Line Number: 522
User prompt
Fix Bug: 'TypeError: santa.move is not a function' in this line: 'if (santa) santa.move();' Line Number: 522
User prompt
any redundant and/or code that should be cleaned up? Perhaps even errors? var Shield = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.x = x; self.y = y; self.on('down', function () { self.destroy(); }); }); var ObstacleBox = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var obstacleBoxGraphics = self.createAsset('obstacleBox', 'Obstacle Box', 0.5, 0.5); self.x = x; self.y = y; }); var Magic = Container.expand(function (x, y, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.moveEnabled = true; self.rotation = 0; var magicGraphics = self.createAsset('magic', 'Magic effect', 0.5, 0.5); self.x = x; self.y = y; self.move = function () { if (self.moveEnabled) { var targetX = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.x : 2048; var targetY = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.y : 2732; var dx = targetX - self.x; var dy = targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); var speed = 10; if (distance > 0) { var normX = dx / distance; var normY = dy / distance; self.x += normX * speed; self.y += normY * speed; } var hitThreshold = 100; if (self.gameInstance.santa) { var santaX = self.gameInstance.santa.santaGraphics.x; var santaY = self.gameInstance.santa.santaGraphics.y; var distance = Math.sqrt(Math.pow(self.x - santaX, 2) + Math.pow(self.y - santaY, 2)); var hitThreshold = 100; if (distance <= hitThreshold) { console.log("Magic position:", self.x, self.y, "Santa position:", santaX, santaY, "Distance:", distance, "Collision detected:", distance <= hitThreshold); self.gameInstance.isGameOver = true; } } else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { if (self.parent) { if (self.gameInstance) { if (self.gameInstance && typeof self.gameInstance.activeMagicCount !== 'undefined') { self.gameInstance.activeMagicCount--; } } self.destroy(); } } } }; self.on('down', function () { LK.setScore(LK.getScore() + 2); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var tingText = new Text2('Ting!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); tingText.x = self.x; tingText.y = self.y; tingText.anchor.set(0.5, 0.5); self.parent.addChild(tingText); LK.setTimeout(function () { tingText.destroy(); }, 1000); self.gameInstance.activeMagicCount--; self.destroy(); }); }); var Reindeer = Container.expand(function (gameInstance) { var self = Container.call(this); self.moveInArc = function () { self.arcAngle += self.arcSpeed * self.arcDirection; self.x = self.gameInstance.x + Math.cos(self.arcAngle) * self.arcRadius; self.y = self.gameInstance.y + Math.sin(self.arcAngle) * self.arcRadius; }; self.speed = 10; self.direction = Math.random() < 0.5 ? 1 : -1; self.x = self.direction === 1 ? 0 : 2048; self.moveHorizontally = function () { self.x += self.speed * self.direction; if (self.x > 2048 || self.x < 0) { self.direction *= -1; self.scale.x = self.direction; self.x = Math.max(0, Math.min(self.x, 2048)); } }; self.fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.01; } else { self.gameInstance.respawnReindeer(self); } }; self.gameInstance = gameInstance; var reindeerGraphics = self.createAsset('reindeer', 'Reindeer', 0.5, 0.5); self.addChild(reindeerGraphics); self.speed = 10; self.direction = Math.random() < 0.5 ? 1 : -1; self.x = self.direction === 1 ? 0 : 2048; self.scale.x = self.direction; self.on('down', function (obj) { LK.setScore(LK.getScore() + 25); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); var greatText = new Text2('Great!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = self.x; greatText.y = self.y; greatText.anchor.set(0.5, 0.5); self.parent.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); self.parent.respawnReindeer(); self.destroy(); }); }); var RandomText = Container.expand(function () { var self = Container.call(this); self.gameInstance = null; self.display = function (x, y, messages) { var message = messages[Math.floor(Math.random() * messages.length)]; var text = new Text2(message, { size: 48, fill: '#ff0000', font: 'Arial Black' }); text.x = x - 400; text.y = y + 175; text.anchor.set(0.5, 0.5); self.addChild(text); LK.setTimeout(function () { text.destroy(); self.parent.randomTextActive = false; }, 3000); }; }); var BadElf = Container.expand(function (spawnX, spawnY, gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; self.spawnedMagicCount = 1; self.shoot = function () {}; self.magicShootingInterval = LK.setInterval(function () { if (self.visible && self.gameInstance.activeMagicCount < self.gameInstance.maxMagicAllowed) { var magic = new Magic(self.x + 125, self.y, self.gameInstance); self.gameInstance.addChild(magic); self.gameInstance.activeMagicCount++; self.spawnedMagicCount++; if (self.spawnedMagicCount >= 2) { self.alpha = 1; var fadeOutInterval; var fadeOut = function () { if (self.alpha > 0) { self.alpha -= 0.05; } else { LK.clearInterval(fadeOutInterval); self.gameInstance.activeBadElves--; self.destroy(); LK.setTimeout(function () { self.gameInstance.spawnBadElf(); }, 2000); } }; LK.clearInterval(self.magicShootingInterval); fadeOutInterval = LK.setInterval(fadeOut, 60); self.magicShootingInterval = null; } } }, Math.random() * (6000 - 2000) + 2000); var badElfGraphics = self.createAsset('badElf', 'Mischievous Elf', 0.5, 0.5); self.x = spawnX; self.y = spawnY; var bulletStartY = self.y - badElfGraphics.height; self.on('down', function (obj) { LK.setScore(LK.getScore() + 3); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); self.parent.activeBadElves--; var obstacleBox = new ObstacleBox(self.x, self.y, self.gameInstance); obstacleBox.x = self.x; obstacleBox.y = self.y; self.parent.addChild(obstacleBox); LK.setTimeout(function () { obstacleBox.destroy(); }, 1000); LK.clearInterval(self.magicShootingInterval); self.destroy(); }); }); var Mistletoe = Container.expand(function (gameInstance) { var self = Container.call(this); self.gameInstance = gameInstance; var mistletoeGraphics = self.createAsset('mistletoe', 'Mistletoe', 0.5, 0.5); var scale = Math.random() * 0.15 + 0.85; mistletoeGraphics.scale.set(scale); var bobbingRange = 10; var bobbingSpeed = 0.2; var bobbingPosition = 0; self.bob = function () { bobbingPosition += bobbingSpeed; self.y += Math.sin(bobbingPosition) * bobbingRange; }; self.on('down', function (obj) { LK.setScore(LK.getScore() + 10); LK.gui.topCenter.children[0].setText(LK.getScore().toString()); self.parent.respawnMistletoe(self); }); }); var Snowflake = Container.expand(function () { var self = Container.call(this); var sizeFactor = Math.random() * 0.5 + 0.5; var snowflakeGraphics = self.createAsset('snowflake', 'Snowflake', 0.5, 0.5); snowflakeGraphics.scale.set(sizeFactor); self.speed = -3 * sizeFactor; self.move = function () { self.x += self.speed; self.y += 0.5; self.scale.x -= 0.003; self.scale.y -= 0.003; if (self.scale.x <= 0 || self.scale.y <= 0) { self.destroy(); } }; }); var Heart = Container.expand(function () { var self = Container.call(this); var heartGraphics = self.createAsset('heart', 'Player life', 0.5, 0.5); self.x = heartGraphics.width / 2; self.y = heartGraphics.height / 2; }); var MuzzleFlash = Container.expand(function () { var self = Container.call(this); self.visible = false; var flashGraphics = self.createAsset('muzzleFlash', 'Muzzle flash effect', .5, .5); self.show = function () { var randomScale = Math.random() * 0.125 + 0.375; self.scale.set(randomScale); self.visible = true; LK.setTimeout(function () { self.visible = false; self.scale.set(1); }, 100); }; }); var Santa = Container.expand(function () { var self = Container.call(this); self.santaGraphics = self.createAsset('santa', 'Santa character', .5, .5); self.move = function () {}; self.shoot = function () {}; }); var Crosshair = Container.expand(function () { var self = Container.call(this); self.move = function (event) { var pos = event.getLocalPosition(self.parent); self.x = pos.x; self.y = pos.y; }; var crosshairGraphics = self.createAsset('crosshair', 'Crosshair', .5, .5); self.y = -crosshairGraphics.height / 2; self.destroy = function () {}; }); var Game = Container.expand(function () { var self = Container.call(this); this.isGameOver = false; this.activeMagicCount = 0; this.magicSpawnX = 1024; this.magicSpawnY = 1366; self.maxMagicAllowed = 3; if (self.activeMagicCount < self.maxMagicAllowed) { var magicInstance = new Magic(this.magicSpawnX, this.magicSpawnY, self); self.addChild(magicInstance); self.activeMagicCount++; } self.respawnReindeer = function (reindeer) { LK.setTimeout(function () { var newReindeer = new Reindeer(self); var spawnLeftSide = Math.random() < 0.5; newReindeer.x = spawnLeftSide ? 0 : 2048; newReindeer.y = Math.random() * (2732 / 4 - newReindeer.height) + newReindeer.height; newReindeer.arcDirection = spawnLeftSide ? 1 : -1; newReindeer.arcRadius = 2048 / 2; newReindeer.arcAngle = spawnLeftSide ? Math.PI : 0; var scale = Math.random() * 0.25 + 0.75; newReindeer.scale.set(scale); self.addChild(newReindeer); LK.on('tick', function () { newReindeer.moveHorizontally(); }); }, Math.random() < 0.5 ? 8000 : 12000); }; self.showGreatText = function (x, y) { var greatText = new Text2('Nice!', { size: 40, fill: '#ffffff', font: 'Arial Black' }); greatText.x = x; greatText.y = y; greatText.anchor.set(0.5, 0.5); self.addChild(greatText); LK.setTimeout(function () { greatText.destroy(); }, 1000); }; var silentNightText = new Text2('Silent Night', { size: 150, fill: "#ffffff" }); silentNightText.anchor.set(0.5, 0); silentNightText.x = 5 / 2; silentNightText.y = 150; LK.gui.topCenter.addChild(silentNightText); self.generateSnowflakes = function () { var snowflakeInterval = LK.setInterval(function () { var snowflake = new Snowflake(); snowflake.x = Math.random() * 2048; snowflake.y = Math.random() * 2732; self.addChild(snowflake); }, 1000); }; LK.setTimeout(function () { var fadeOutDuration = 2000; var fadeOutStep = 0.05; var fadeOutInterval = LK.setInterval(function () { silentNightText.alpha -= fadeOutStep; if (silentNightText.alpha <= 0) { LK.clearInterval(fadeOutInterval); silentNightText.destroy(); self.score = 0; self.scoreText = new Text2(self.score.toString(), { size: 150, fill: "#ffffff" }); self.scoreText.anchor.set(0.5, 0); self.scoreText.x = 5 / 2; self.scoreText.y = 150; LK.gui.topCenter.addChild(self.scoreText); self.updateScore = function (score) { self.score += score; self.scoreText.setText(self.score.toString()); LK.setScore(self.score); }; } }, fadeOutDuration * fadeOutStep); self.spawnBadElf(); LK.setInterval(self.spawnBadElf, Math.random() * (6000 - 3000) + 3000); self.generateMistletoes = function () { LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 500) + boundaryOffset + 200; self.addChild(newMistletoe); }, 8000); }; LK.setTimeout(function () { var reindeer = new Reindeer(self); var boundaryOffset = 200; reindeer.x = Math.random() < 0.5 ? boundaryOffset : 2048 - boundaryOffset - reindeer.width; reindeer.y = Math.random() * (2732 / 4 - boundaryOffset) + boundaryOffset; reindeer.arcDirection = Math.random() < 0.5 ? 1 : -1; reindeer.arcRadius = 2048 / 2; reindeer.arcAngle = Math.random() < 0.5 ? Math.PI : 0; var initialScale = Math.random() * 0.25 + 0.75; reindeer.scale.set(initialScale); self.addChild(reindeer); LK.on('tick', function () { if (reindeer) reindeer.moveHorizontally(); }); }, 12000); LK.setTimeout(self.generateMistletoes, 8000); }, 2000); stage.on('move', function (obj) { crosshair.move(obj.event); }); stage.on('down', function (obj) { if (hearts.length > 0) { muzzleFlash.show(); leftClickPressed = true; lastClickTime = LK.ticks; if (!barnMovedDown) { self.children.forEach(function (child) { if (child instanceof BadElf) { child.y += 100; } }); barnBackground.y += 460; room1Background.y += 100; boxBackground.y += 100; crateBackground.y += 100; missBackground.y += 460; tireBackground.y += 100; barnMovedDown = true; if (Math.random() < 0.10) { var messages = ["Santa's special delivery!", "This is for the naughty list!", "Ho, ho, ho-hold this!"]; if (!self.randomTextActive) { self.randomTextActive = true; var randomText = new RandomText(); randomText.display(santa.x, santa.y - santa.santaGraphics.height / 2, messages); self.addChild(randomText); } } } var heart = hearts.pop(); heart.destroy(); } self.children.forEach(function (child) { if (child instanceof BadElf && leftClickPressed && LK.ticks - lastClickTime > 30) { child.y -= 100; } }); }); var room1Background = self.createAsset('Room1', 'Room1 background', 0, 0); room1Background.anchor.set(0.5, 0.5); room1Background.x = 2048 / 2; room1Background.y = 2732 / 2; self.addChild(room1Background); var tireBackground = self.createAsset('Tire', 'Tire background', 0.5, 0.5); tireBackground.anchor.set(0.5, 0.5); tireBackground.x = 700; tireBackground.y = 1825; self.addChild(tireBackground); self.respawnMistletoe = function (mistletoe) { this.showGreatText(mistletoe.x, mistletoe.y); mistletoe.destroy(); LK.setTimeout(function () { var newMistletoe = new Mistletoe(); var boundaryOffset = 100; newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset; newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 100 - 500) + boundaryOffset + 300; self.addChild(newMistletoe); }, Math.random() * (10000 - 5000) + 5000); }; self.generateSnowflakes(); var missBackground = self.createAsset('Miss', 'Miss background', 0, 0); missBackground.anchor.set(0.5, 0.5); missBackground.x = 700; missBackground.y = 2520; var crateBackground = self.createAsset('Crate', 'Crate background', 0, 0); crateBackground.anchor.set(0, 1); crateBackground.x = 205; crateBackground.y = 2645; self.addChild(crateBackground); var boxBackground = self.createAsset('Box', 'Box background', 0, 0); boxBackground.anchor.set(0, 1); boxBackground.x = 1350; boxBackground.y = 2200; self.addChild(boxBackground); var barnBackground = self.createAsset('Barn', 'Barn background', 0, 0); barnBackground.anchor.set(0.5, 0.5); barnBackground.x = 2048 / 2; barnBackground.y = 2732 - barnBackground.height / 4 + 900; self.addChild(barnBackground); self.addChild(missBackground); var santa = self.addChild(new Santa()); santa.x = 2048 / 2 + 650; santa.y = 2732 - santa.height / 2; var shield = new Shield(santa.x, santa.y - 100, self); self.addChild(shield); self.addChild(santa); var spawnLocations = [{ x: tireBackground.x + 175, y: tireBackground.y - 65 }, { x: boxBackground.x, y: boxBackground.y - 350 }, { x: crateBackground.x + 250, y: crateBackground.y - 600 }]; var usedSpawnLocations = []; var spawnLocationCounter = 0; self.activeBadElves = 0; self.spawnBadElf = function () { if (self.scoreText && self.activeBadElves < 2) { if (usedSpawnLocations.length === spawnLocations.length) { usedSpawnLocations = []; spawnLocationCounter = 0; } var availableLocations = spawnLocations.filter(function (location, index) { return usedSpawnLocations.indexOf(index) === -1; }); var locationIndex = Math.floor(Math.random() * availableLocations.length); var location = availableLocations[locationIndex]; usedSpawnLocations.push(spawnLocations.indexOf(location)); spawnLocationCounter++; var badElf = new BadElf(location.x, location.y, self); var scaleModifier = Math.random() * 0.25 + 0.75; badElf.scale.set(scaleModifier, scaleModifier); self.addChildAt(badElf, self.getChildIndex(room1Background) + 1); var magic = new Magic(badElf.x + badElf.width / 2, badElf.y, self); self.addChild(magic); self.activeBadElves++; var bulletStartY = badElf.y - badElf.height; } }; var muzzleFlash = new MuzzleFlash(); muzzleFlash.x = santa.x - santa.santaGraphics.width + 400; muzzleFlash.y = santa.y - 90; self.addChild(muzzleFlash); self.addChild(santa); var crosshair = self.addChild(new Crosshair()); crosshair.x = 2048 / 2; crosshair.y = 2732 / 2; var isGameOver = false; var tickOffset = 0; var leftClickPressed = false; var barnMovedDown = false; var lastClickTime = 0; var playerLives = 6; var hearts = []; for (var i = 0; i < playerLives; i++) { var heart = new Heart(); heart.x = 1125 + i * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } LK.on('tick', function () { if (santa) santa.move(); for (var i = 0; i < self.children.length; i++) { var child = self.children[i]; if (child instanceof Snowflake) { child.move(); } else if (child instanceof Mistletoe) { child.bob(); } else if (child instanceof Magic) { child.move(); } } if (self.isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); self.isGameOver = false; } if (leftClickPressed && LK.ticks - lastClickTime > 30) { barnBackground.y -= 460; room1Background.y -= 100; boxBackground.y -= 100; crateBackground.y -= 100; missBackground.y -= 460; tireBackground.y -= 100; self.children.forEach(function (child) { if (child instanceof BadElf && barnMovedDown) { child.y -= 100; } }); leftClickPressed = false; barnMovedDown = false; while (hearts.length < playerLives) { var heart = new Heart(); heart.x = 1125 + hearts.length * (heart.width + 5); heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350; hearts.push(heart); LK.gui.topLeft.addChild(heart); } } }); });
User prompt
the distance check should also check self.gameInstance.santa.santaGraphics
===================================================================
--- original.js
+++ change.js
@@ -36,11 +36,8 @@
self.x += normX * speed;
self.y += normY * speed;
}
if (self.gameInstance.santa) {
- var santaX = self.gameInstance.santa.santaGraphics.x;
- var santaY = self.gameInstance.santa.santaGraphics.y;
- var distance = Math.sqrt(Math.pow(self.x - santaX, 2) + Math.pow(self.y - santaY, 2));
var hitThreshold = (self.gameInstance.santa.santaGraphics.width + magicGraphics.width) / 4;
if (distance <= hitThreshold) {
self.gameInstance.triggerGameOver();
}
over the shoulder santa firing a revolver Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d transparent christmas crosshair Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d 3rd person front view of a christmas town square with a starry sky Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Christmas sparkles png Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
circular christmas golden star pattern transparent png Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d christmas brick wall Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d opened christmas crate Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d diagonal christmas car or truck in snow Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas poster showcasing miss santa clause Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a single white snowflake Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d stacked christmas winter tire Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d stacked christmas winter tire Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d christmas magical mistletoe Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d christmas 357 Magnum bullets Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d silhouette of a flying reindeer with a red glowy nose Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d christmas dark sparkles Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d christmas evil robot elf with a gun 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.
2d pile of gray and red nuts and bolts Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
transparent snow sphere. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
snd_pistol
Sound effect
snd_enemyshot
Sound effect
snd_obstacle
Sound effect
snd_messages
Sound effect
snd_ricochet
Sound effect
snd_reindeer
Sound effect
snd_mistletoe
Sound effect
snd_reindeershot
Sound effect
snd_mistletoeshot
Sound effect
snd_christmasmusic
Music
snd_reload
Sound effect
snd_blastwave
Sound effect