It looks like the error might be due to trying to access properties of the app object before it has been fully instantiated or added to the DOM. Let's ensure that the app is fully created and attached before we proceed with loading the assets.
Updated Game.ts
Make sure the Pixi application is correctly appended to the DOM and the loading procedure only starts after the window has loaded.
import * as PIXI from 'pixi.js';
import { Loader } from './Loader';
import { SplashScreen } from './SplashScreen';
import { GameScreen } from './GameScreen';
class ClickerGame {
app: PIXI.Application;
loader: Loader;
splashScreen: SplashScreen;
gameScreen: GameScreen;
constructor() {
this.app = new PIXI.Application({ width: 800, height: 600, backgroundColor: 0x1099bb });
document.body.appendChild(this.app.view);
this.loader = new Loader(this.app, this.onAssetsLoaded.bind(this));
this.loader.loadAssets();
}
onAssetsLoaded() {
this.splashScreen = new SplashScreen(this.app, this.startGame.bind(this));
this.splashScreen.show();
}
startGame() {
this.splashScreen.hide();
this.gameScreen = new GameScreen(this.app);
this.gameScreen.setup();
}
}
// Ensure the DOM is ready before creating the game instance
window.onload = () => {
new ClickerGame();
};
Improved Error Handling in Loader.ts
Ensure that the Loader handles errors and all assets are correctly loaded.
import * as PIXI from 'pixi.js';
export class Loader {
app: PIXI.Application;
onComplete: () => void;
constructor(app: PIXI.Application, onComplete: () => void) {
this.app = app;
this.onComplete = onComplete;
}
loadAssets() {
this.showLoadingBar();
this.app.loader.add('heroImage', 'path_to_hero_image.png');
this.app.loader.add('enemyImage', 'path_to_enemy_image.png');
this.app.loader.add('enemyPainImage', 'path_to_enemy_pain_image.png');
this.app.loader.add('enemyDeathImage', 'path_to_enemy_death_image.png');
this.app.loader.add('coinImage', 'path_to_coin_image.png');
this.app.loader.load((loader, resources) => {
if (loader.progress === 100) {
this.onComplete();
}
});
this.app.loader.on('progress', this.onLoadingProgress.bind(this));
}
showLoadingBar() {
const loaderBar = new PIXI.Graphics();
loaderBar.beginFill(0x000000);
loaderBar.drawRect(200, 300, 400, 50);
loaderBar.endFill();
this.app.stage.addChild(loaderBar);
const progressBar = new PIXI.Graphics();
progressBar.beginFill(0xFF700B);
progressBar.drawRect(200, 300, 0, 50);
progressBar.endFill();
this.app.stage.addChild(progressBar);
this.app.loader.on('progress', (loader: PIXI.Loader) => {
progressBar.width = 400 * (loader.progress / 100);
});
}
onLoadingProgress(loader: PIXI.Loader, resource: PIXI.LoaderResource) {
// Handle loading progress
}
}
Full code for all other classes
To ensure the full game setup works, here is the combined code for all classes, with minor fixes and improvements:
Constants.ts
export const HEROES_LIST = [
{ name: 'Hero 1', damage: 5, cost: 50 },
{ name: 'Hero 2', damage: 10, cost: 100 },
{ name: 'Hero 3', damage: 20, cost: 200 },
// Add more heroes as needed
];
export const COIN_VALUE = 10;
export const BOSS_MULTIPLIER = 10;
export const DROP_COIN_AMOUNT = 5; // Number of coins to drop per enemy
Hero.ts
export class Hero {
name: string;
damage: number;
level: number;
baseCost: number;
currentCost: number;
constructor(name: string, damage: number, cost: number) {
this.name = name;
this.damage = damage;
this.level = 1;
this.baseCost = cost;
this.currentCost = cost;
}
upgrade() {
this.level += 1;
this.damage += 10; // Example upgrade logic
this.currentCost = this.baseCost * Math.pow(1.15, this.level); // Example cost scaling
}
}
SoundManager.ts
import { Howl } from 'howler';
export class SoundManager {
clickSound: Howl;
constructor() {
this.clickSound = new Howl({
src: ['path_to_sound.mp3']
});
}
playClickSound() {
this.clickSound.play();
}
}
Player.ts
import { Hero } from './Hero';
import { COIN_VALUE } from './Constants';
export class Player {
coins: number;
heroes: Hero[];
constructor() {
this.coins = 0;
this.heroes = [];
}
addHero(hero: Hero) {
this.heroes.push(hero);
}
buyHero(hero: Hero) {
if (this.coins >= hero.currentCost) {
this.coins -= hero.currentCost;
this.addHero(hero);
} else {
console.log('Not enough coins');
}
}
upgradeHero(hero: Hero) {
if (this.coins >= hero.currentCost) {
this.coins -= hero.currentCost;
hero.upgrade();
} else {
console.log('Not enough coins to upgrade');
}
}
collectCoin() {
this.coins += COIN_VALUE;
}
calculateTotalDamage(): number {
return this.heroes.reduce((total, hero) => total + hero.damage, 0);
}
calculateDPS(): number {
return this.calculateTotalDamage() / 10; // DPS example
}
}
Enemy.ts
import * as PIXI from 'pixi.js';
export class Enemy {
sprite: PIXI.AnimatedSprite;
health: number;
maxHealth: number;
hpBar: PIXI.Graphics;
level: number;
normalTextures: PIXI.Texture[];
painTextures: PIXI.Texture[];
deathTextures: PIXI.Texture[];
constructor(normalTextures: PIXI.Texture[], painTextures: PIXI.Texture[], deathTextures: PIXI.Texture[], health: number, level: number) {
this.normalTextures = normalTextures;
this.painTextures = painTextures;
this.deathTextures = deathTextures;
this.sprite = new PIXI.AnimatedSprite(normalTextures);
this.sprite.animationSpeed = 0.1;
this.sprite.play();
this.health = health;
this.maxHealth = health;
this.level = level;
this.hpBar = new PIXI.Graphics();
this.updateHpBar();
}
updateHpBar() {
this.hpBar.clear();
this.hpBar.beginFill(0xff0000);
this.hpBar.drawRect(this.sprite.x - 50, this.sprite.y - 80, 100 * (this.health / this.maxHealth), 10);
this.hpBar.endFill();
}
takeDamage(damage: number) {
this.health -= damage;
this.playPainAnimation();
this.updateHpBar();
if (this.health <= 0) {
this.defeat();
}
}
playPainAnimation() {
this.sprite.textures = this.painTextures;
this.sprite.loop = false;
this.sprite.onComplete = () => {
this.sprite.textures = this.normalTextures;
this.sprite.loop = true;
this.sprite.play();
};
this.sprite.play();
}
playDeathAnimation() {
this.sprite.textures = this.deathTextures;
this.sprite.loop = false;
this.sprite.onComplete = () => {
this.sprite.visible = false; // Hide sprite after death animation
};
this.sprite.play();
}
defeat() {
console.log('Enemy defeated!');
this.health = 0;
this.playDeathAnimation();
}
nextLevel() {
this.level += 1;
this.maxHealth = 100 * Math.pow(1.1, this.level);
if (this.level % 10 === 0) {
this.maxHealth *= 10; // Every 10th level is a boss
}
this.health = this.maxHealth;
this.sprite.visible = true;
this.sprite.textures = this.normalTextures;
this.sprite.play();
this.updateHpBar();
}
}
Coin.ts
import * as PIXI from 'pixi.js';
export class Coin {
sprite: PIXI.Sprite;
value: number;
constructor(texture: PIXI.Texture, x: number, y: number, value: number) {
this.sprite = new PIXI.Sprite(texture);
this.sprite.x = x;
this.sprite.y = y;
this.value = value;
this.sprite.interactive = true;
this.sprite.buttonMode = true;
this.sprite.on('pointerover', this.collect.bind(this));
this.startBounceAnimation();
}
startBounceAnimation() {
PIXI.Ticker.shared.add(() => {
this.sprite.y = this.sprite.y + Math.sin(PIXI.Ticker.shared.lastTime / 200) * 0.5;
});
}
collect() {
this.sprite.visible = false;
this.sprite.destroy();
}
}
GameScreen.ts
import * as PIXI from 'pixi.js';
import { SoundManager } from './SoundManager';
import { Player } from './Player';
import { Enemy } from './Enemy';
import { Hero } from './Hero';
import { Coin } from './Coin';
import { HEROES_LIST, DROP_COIN_AMOUNT, COIN_VALUE } from './Constants';
export class GameScreen {
app: PIXI.Application;
container: PIXI.Container;
soundManager: SoundManager;
player: Player;
enemy: Enemy;
coins: Coin[];
constructor(app: PIXI.Application) {
this.app = app;
this.container = new PIXI.Container();
this.soundManager = new SoundManager();
this.player = new Player();
this.coins = [];
this.gameLoop = this.gameLoop.bind(this);
}
setup() {
this.app.ticker.add(this.gameLoop);
this.app.stage.addChild(this.container);
// Set up enemy with animations
const normalTextures = [PIXI.Texture.from('enemyImage')];
const painTextures = [PIXI.Texture.from('enemyPainImage')];
const deathTextures = [PIXI.Texture.from('enemyDeathImage')];
this.enemy = new Enemy(normalTextures, painTextures, deathTextures, 100, 1);
this.enemy.sprite.position.set(this.app.view.width / 2, this.app.view.height / 2);
this.enemy.sprite.interactive = true;
this.enemy.sprite.buttonMode = true;
this.enemy.sprite.on('pointerdown', this.onEnemyClick.bind(this));
this.container.addChild(this.enemy.sprite);
this.container.addChild(this.enemy.hpBar);
// Add the list of heroes
this.setupHeroList();
}
setupHeroList() {
const heroContainer = new PIXI.Container();
heroContainer.position.set(10, 10);
this.container.addChild(heroContainer);
HEROES_LIST.forEach((heroData, index) => {
const heroText = new PIXI.Text(`${heroData.name} - Cost: ${heroData.cost}`, { fontSize: 18, fill: 0xffffff });
heroText.position.set(0, index * 30);
heroText.interactive = true;
heroText.buttonMode = true;
heroText.on('pointerdown', () => this.onBuyHero(new Hero(heroData.name, heroData.damage, heroData.cost)));
heroContainer.addChild(heroText);
});
}
onBuyHero(hero: Hero) {
this.player.buyHero(hero);
}
onEnemyClick() {
console.log('Enemy Clicked!');
this.soundManager.playClickSound();
this.enemy.takeDamage(this.player.calculateTotalDamage());
if (this.enemy.health <= 0) {
this.handleEnemyDefeat();
}
}
handleEnemyDefeat() {
// Drop multiple coins on defeat
for (let i = 0; i < DROP_COIN_AMOUNT; i++) {
const x = this.enemy.sprite.x + (Math.random() - 0.5) * 100;
const y = this.enemy.sprite.y + (Math.random() - 0.5) * 100;
this.dropCoin(x, y);
}
this.enemy.nextLevel();
}
dropCoin(x: number, y: number) {
const coin = new Coin(PIXI.Texture.from('coinImage'), x, y, COIN_VALUE);
coin.sprite.on('pointerover', () => {
coin.collect();
this.player.collectCoin();
});
this.coins.push(coin);
this.container.addChild(coin.sprite);
}
gameLoop(delta: number) {
// Apply DPS damage to the enemy
this.enemy.takeDamage(this.player.calculateDPS() * delta / 60);
// Check if enemy is defeated and handle accordingly
if (this.enemy.health <= 0) {
this.handleEnemyDefeat();
}
// Keep coins on the screen if not collected
this.coins = this.coins.filter(coin => coin.sprite.visible);
}
}
Updated Loader.ts
import * as PIXI from 'pixi.js';
export class Loader {
app: PIXI.Application;
onComplete: () => void;
constructor(app: PIXI.Application, onComplete: () => void) {
this.app = app;
this.onComplete = onComplete;
}
loadAssets() {
this.showLoadingBar();
this.app.loader.add('heroImage', 'path_to_hero_image.png');
this.app.loader.add('enemyImage', 'path_to_enemy_image.png');
this.app.loader.add('enemyPainImage', 'path_to_enemy_pain_image.png');
this.app.loader.add('enemyDeathImage', 'path_to_enemy_death_image.png');
this.app.loader.add('coinImage', 'path_to_coin_image.png');
this.app.loader.once('complete', this.onComplete);
this.app.loader.on('progress', this.onLoadingProgress.bind(this));
this.app.loader.load();
}
showLoadingBar() {
const loaderBar = new PIXI.Graphics();
loaderBar.beginFill(0x000000);
loaderBar.drawRect(200, 300, 400, 50);
loaderBar.endFill();
this.app.stage.addChild(loaderBar);
const progressBar = new PIXI.Graphics();
progressBar.beginFill(0xFF700B);
progressBar.drawRect(200, 300, 0, 50);
progressBar.endFill();
this.app.stage.addChild(progressBar);
this.app.loader.on('progress', (loader: PIXI.Loader) => {
progressBar.width = 400 * (loader.progress / 100);
});
}
onLoadingProgress(loader: PIXI.Loader, resource: PIXI.LoaderResource) {
// Handle loading progress
}
}
SplashScreen.ts
import * as PIXI from 'pixi.js';
export class SplashScreen {
app: PIXI.Application;
container: PIXI.Container;
onStart: () => void;
constructor(app: PIXI.Application, onStart: () => void) {
this.app = app;
this.onStart = onStart;
this.container = new PIXI.Container();
}
show() {
const splashText = new PIXI.Text('Tap to Start', { fontSize: 36, fill: 0xffffff });
splashText.anchor.set(0.5);
splashText.position.set(this.app.view.width / 2, this.app.view.height / 2);
this.container.addChild(splashText);
this.container.interactive = true;
this.container.buttonMode = true;
this.container.on('pointerdown', this.onStart);
this.app.stage.addChild(this.container);
}
hide() {
this.container.visible = false;
}
}
Notes:
- Assets: Ensure the paths specified for your images and sound files (
path_to_hero_image.png, path_to_enemy_image.png, etc.) are correct and accessible.
- Textures: More textures can be added for smoother enemy animations.
- Fixes: Ensure
Loader.ts and Game.ts correctly initialize and handle errors.
This setup should correct the error and provide a foundation for your clicker game with animations and persistent coins.