User prompt
on tick check if backround is playing if not, play
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: LK.getSound(...).isPlaying is not a function' in or related to this line: 'if (!LK.getSound('Background').isPlaying()) {' Line Number: 300
User prompt
Ok tick check of background sound is playing, if not, then play
User prompt
Om game start play background sound
User prompt
When player hits obstacle play gameover sound
User prompt
If condition is set for game over, play gameover sound for 1 second before game over occura
User prompt
Make sure gameover sound is played before the game is over
User prompt
When game over play gameover sound
User prompt
When bullet hits obstacle play hit sound
User prompt
Play scream when bullet hits any enemy
User prompt
When bullet is shot use plop sound
User prompt
Migrate to the latest version of LK
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: hero.distanceTo is not a function' in this line: 'if (hero.intersects(obstacles[i]) && hero.distanceTo(obstacles[i]) < intersectionThreshold) {' Line Number: 173
User prompt
add a thershold for intersects bewteen hero and obstacle
Code edit (1 edits merged)
Please save this source code
User prompt
Update var threshold to 100
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.createAsset('cloud', 'Cloud asset', 0.5, 0.5);
cloudGraphics.alpha = 0.7;
self.speed = -0.2;
self.move = function () {
self.x += self.speed;
if (self.x <= -self.width) {
self.x = 2048 + self.width;
}
};
});
var SecondBackground = Container.expand(function () {
var self = Container.call(this);
var bg1 = self.createAsset('second_background', 'Game Second Background', 0, 0);
var bg2 = self.createAsset('second_background', 'Game Second Background', 0, 0);
bg1.width = bg2.width = 2048;
bg1.height = bg2.height = 2732;
bg2.x = 2048;
self.addChild(bg1);
self.addChild(bg2);
self.move = function () {
bg1.x += 0.5;
bg2.x += 0.5;
if (bg1.x >= 2048) bg1.x = bg2.x - 2048;
if (bg2.x >= 2048) bg2.x = bg1.x - 2048;
};
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', .5, .5);
self.baseSpeed = -5;
self.speedIncrease = 0;
self.move = function () {
self.x += self.baseSpeed - self.speedIncrease;
self.y += Math.sin(LK.ticks / 60) * 1;
};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('bullet', 'Hero Bullet', .5, .5);
self.speed = 5;
self.move = function () {
self.y += self.speed;
};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero character', .5, .5);
var wingGraphics = self.createAsset('wing', 'Hero Wing', .7, .8);
wingGraphics.direction = 1;
self.speed = 0;
self.bullets = [];
self.gravity = 0.2;
self.jump = function () {
self.speed = -10;
};
self.update = function () {
self.speed += self.gravity;
self.y += self.speed;
self.rotation = self.speed * 0.05;
wingGraphics.y += wingGraphics.direction * 2 || -2;
if (wingGraphics.y > 10 || wingGraphics.y < -10) {
wingGraphics.direction = -wingGraphics.direction;
}
for (var i = self.bullets.length - 1; i >= 0; i--) {
self.bullets[i].move();
if (self.bullets[i].y > 2732) {
self.bullets[i].destroy();
self.bullets.splice(i, 1);
}
}
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
self.isHit = false;
var enemyAssetIds = ['enemy1', 'enemy2', 'enemy3'];
var selectedAssetId = enemyAssetIds[Math.floor(Math.random() * enemyAssetIds.length)];
var enemyGraphics = self.createAsset(selectedAssetId, 'Enemy', .5, .5);
self.speedX = -1.8 * (1 + Math.random() * 0.5) * 1.5;
self.speedY = 0;
self.verticalMovementRange = 50;
self.verticalSpeed = self.verticalMovementRange / (5 * 60);
self.verticalDirection = 1;
self.initialY = 0;
self.move = function () {
self.x += self.isHit ? self.speedX * 4 : self.speedX;
if (self.initialY === 0) self.initialY = self.y + 20;
self.y += self.verticalSpeed * self.verticalDirection;
if (Math.abs(self.y - self.initialY) >= self.verticalMovementRange) {
self.verticalDirection *= -1;
}
};
});
var Background = Container.expand(function () {
var self = Container.call(this);
var bg1 = self.createAsset('background', 'Game Background 1', 0, 0);
var bg2 = self.createAsset('background', 'Game Background 2', 0, 0);
var dimOverlay = new Container();
var dimGraphics = dimOverlay.createAsset('dimOverlay', 'Dim Overlay', 0, 0);
dimGraphics.width = 2048;
dimGraphics.height = 2732;
dimGraphics.alpha = 0.3;
bg1.width = bg2.width = 2048;
bg1.height = bg2.height = 2732;
bg2.x = 2048;
self.addChild(bg1);
self.addChild(bg2);
self.addChild(dimOverlay);
self.move = function () {
bg1.x -= 1;
bg2.x -= 1;
if (bg1.x <= -2048) bg1.x = bg2.x + 2048;
if (bg2.x <= -2048) bg2.x = bg1.x + 2048;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var secondBackground = self.addChild(new SecondBackground());
var clouds = [];
for (var i = 0; i < 5; i++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = Math.random() * 2732 * 0.3;
var randomScale = 0.5 + Math.random() * 0.5;
cloud.scale.x = randomScale;
cloud.scale.y = randomScale;
clouds.push(cloud);
self.addChild(cloud);
}
var background = self.addChild(new Background());
self.setChildIndex(secondBackground, 0);
var hero = self.addChild(new Hero());
hero.x = 2048 / 2 - 200;
hero.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
font: "Gamer",
dropShadow: true,
dropShadowColor: "#000000",
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6
});
LK.gui.topCenter.addChild(scoreTxt);
scoreTxt.anchor.set(.5, 0);
var isGameOver = false;
var tickOffset = 0;
var speedIncreaseFactor = 0;
var obstacleSpawnInterval = 36 * 4;
LK.on('tick', function () {
secondBackground.move();
clouds.forEach(function (cloud) {
cloud.move();
});
background.move();
hero.update();
if (hero.y > 2732 || hero.y < 0) {
isGameOver = true;
}
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (obstacles[i].x < -50) {
obstacles[i].destroy();
obstacles.splice(i, 1);
scoreTxt.setText(score);
}
var threshold = 100;
if (hero.intersects(obstacles[i], threshold)) {
isGameOver = true;
} else {
for (var j = hero.bullets.length - 1; j >= 0; j--) {
if (hero.bullets[j].intersects(obstacles[i])) {
if (obstacles[i] instanceof Enemy) {
obstacles[i].isHit = true;
obstacles[i].addChild(hero.bullets[j]);
hero.bullets[j].x = 0;
hero.bullets[j].y = -250;
hero.bullets[j].width = 100;
hero.bullets[j].height = 100;
if (!obstacles[i].scoreAwarded) {
score++;
obstacles[i].scoreAwarded = true;
}
scoreTxt.setText(score);
} else {
hero.bullets[j].destroy();
hero.bullets.splice(j, 1);
}
}
}
}
}
if (tickOffset++ % obstacleSpawnInterval == 0) {
var newObstacle = new Obstacle();
newObstacle.x = 2048;
newObstacle.y = Math.random() * (2732 - 500 - newObstacle.height) + newObstacle.height;
newObstacle.speedIncrease = speedIncreaseFactor;
obstacles.push(newObstacle);
self.addChild(newObstacle);
}
if (tickOffset % 600 == 0) {
speedIncreaseFactor += 0.5;
obstacleSpawnInterval = Math.max(30, obstacleSpawnInterval - 15);
}
var enemySpawnVariance = Math.random() > 0.5 ? 1.5 : 0.5;
if (tickOffset % Math.floor(360 * enemySpawnVariance) == 0) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 - enemy.height / 2;
self.addChild(enemy);
obstacles.push(enemy);
}
if (isGameOver) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
stage.on('down', function (obj) {
hero.jump();
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
self.addChild(bullet);
hero.bullets.push(bullet);
});
});