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]) < 50) {' Line Number: 164
User prompt
Reduce collision threshold to 10 pixels
User prompt
Fix Bug: 'TypeError: hero.distanceTo is not a function' in this line: 'if (hero.intersects(obstacles[i]) && hero.distanceTo(obstacles[i]) < 50) {' Line Number: 164
User prompt
Add 50 pixels collisio threshold between hero and obstavles
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'width')' in this line: 'var enemyGraphics = self.createAsset(selectedAssetId, 'Enemy', enemyGraphics.width / 2, enemyGraphics.height / 2);' Line Number: 81
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'width')' in this line: 'var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', obstacleGraphics.width / 2, obstacleGraphics.height / 2);' Line Number: 30
User prompt
Fix Bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'width')' in this line: 'var heroGraphics = self.createAsset('hero', 'Hero character', heroGraphics.width / 2, heroGraphics.height / 2);' Line Number: 48
User prompt
Update anchor points of hero and obstacles to correclty detct collision. Anchor points should be defined by the asset graphic not the shape
User prompt
Add collision detection on the cemter of the assets
User prompt
Fix Bug: 'TypeError: hero.distanceTo is not a function' in this line: 'if (hero.intersects(obstacles[i]) && hero.distanceTo(obstacles[i]) < 20) {' Line Number: 164
User prompt
Hero and obstacle can collide for 20 pixels and its not game over
User prompt
Fix Bug: 'TypeError: hero.getBounds(...).intersects is not a function' in this line: 'if (hero.getBounds().intersects(obstacles[i].getBounds())) {' Line Number: 164
User prompt
Veryfy that hero and obstacles actually colide and not juat the area of the asset
User prompt
Add 20 pixels collision threshold berween hero and obstavles
User prompt
Add a 10 pixels collision threshold betweem hero and obstacles
User prompt
Fix Bug: 'TypeError: this.getPixel is not a function' in this line: 'var pixel = this.getPixel(x, y);' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: pixel is not defined' in this line: 'if (pixel.alpha > 0) {' Line Number: 127
User prompt
Fix Bug: 'TypeError: this.getPixel is not a function' in this line: 'var pixel = this.getPixel(x, y);' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: pixel is not defined' in this line: 'if (pixel.alpha > 0) {' Line Number: 127
User prompt
Fix Bug: 'TypeError: this.getPixel is not a function' in this line: 'var pixel = this.getPixel(x, y);' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: pixel is not defined' in this line: 'if (pixel.alpha > 0) {' Line Number: 127
User prompt
Fix Bug: 'TypeError: this.getPixel is not a function' in this line: 'var pixel = this.getPixel(x, y);' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: pixel is not defined' in this line: 'if (pixel.alpha > 0) {' Line Number: 127
User prompt
Fix Bug: 'TypeError: this.getPixel is not a function' in this line: 'var pixel = this.getPixel(x, y);' Line Number: 127
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.createAsset('cloud', 'Cloud asset', 0.5, 0.5);
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);
self.pixelIntersects = function (obstacle) {
var heroPixels = heroGraphics.getPixels();
var obstaclePixels = obstacle.getPixels();
for (var i = 0; i < heroPixels.length; i++) {
for (var j = 0; j < obstaclePixels.length; j++) {
if (heroPixels[i].x === obstaclePixels[j].x && heroPixels[i].y === obstaclePixels[j].y) {
return true;
}
}
}
return false;
};
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.3;
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);
bg1.width = bg2.width = 2048;
bg1.height = bg2.height = 2732;
bg2.x = 2048;
self.addChild(bg1);
self.addChild(bg2);
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;
};
});
Container.prototype.getPixels = function () {
var pixels = [];
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
if (pixel.alpha > 0) {
pixels.push({
x: x + this.x,
y: y + this.y
});
}
}
}
return pixels;
};
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;
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);
}
if (hero.intersects(obstacles[i])) {
if (hero.pixelIntersects(obstacles[i])) {
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++ % 90 == 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;
}
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);
});
});