Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Pocket platformer

Code free tool for creating platformer games 路 By The l0bster

Creating the Enemy Chaser (Step by Step)

A topic by deater07 created 94 days ago Views: 146 Replies: 3
Viewing posts 1 to 3
(1 edit) (+1)

### Creating the Enemy Chaser (Step by Step)

**Overview:** Add a heart-shaped Enemy Chaser that patrols, bounces on walls, and defeats the player on touch. Created with Copilot and shared as a chapter of memory and code.

---

#### 1) Create the object class

- **File:** objects/EnemyChaser.js

```js

class EnemyChaser extends InteractiveLevelObject {

  constructor(x, y, tileSize, type, tileMapHandler, extraAttributes = {}) {

    super(x, y, tileSize, type, tileMapHandler, extraAttributes);

    this.tileMapHandler = tileMapHandler;

    this.speed = extraAttributes.speed || 1;

    this.direction = 1; // 1 = right, -1 = left

    this.collidesWithWalls = extraAttributes.collidesWithWalls ?? true;

    this.animation = SpritePixelArrays.ENEMY_CHASER.animation[0];

  }

  draw(spriteCanvas) {

    const newX = this.x + this.speed * this.direction;

    // Next tile check

    const tileX = this.tileMapHandler.getTileValueForPosition(

      this.direction > 0 ? newX + this.tileSize - 1 : newX

    );

    const tileY = this.tileMapHandler.getTileValueForPosition(this.y);

    const tileType = this.tileMapHandler.getTileLayerValueByIndex(tileY, tileX);

    // Wall collision bounce

    if (this.collidesWithWalls && tileType !== 0) {

      this.direction *= -1;

    } else {

      this.x = newX;

    }

    // Player collision

    const player = this.tileMapHandler?.player;

    if (

      player &&

      Math.abs(this.x - player.x) < this.tileSize / 2 &&

      Math.abs(this.y - player.y) < this.tileSize / 2

    ) {

      PlayMode.playerDeath();

    }

    super.draw(spriteCanvas);

  }

}

```

---

#### 2) Register the script

- **File:** index.html

```html

<script src="objects/EnemyChaser.js"></script>

```

---

#### 3) Add the type

- **File:** utils/ObjectTypes.js

```js

static get ENEMY_CHASER() {

  return 'enemyChaser';

}

```

- **Map at the end:**

```js

[this.ENEMY_CHASER]: EnemyChaser

```

---

#### 4) Add sprite and attributes

- **File:** utils/SpritePixelArray.js

```js

this.ENEMY_CHASER = {

  name: ObjectTypes.ENEMY_CHASER,

  type: this.SPRITE_TYPES.object,

  descriptiveName: "Enemy Chaser",

  changeableAttributes: [

    { name: this.changeableAttributeTypes.speed, defaultValue: 1, minValue: 1, maxValue: 5 },

    {

      name: this.changeableAttributeTypes.collidesWithWalls,

      defaultValue: true,

      formElement: this.changeableAttributeFormElements.checkbox,

      checkboxDescription: "Chaser collides with walls"

    }

  ],

  squishAble: false,

  rotateable: false,

  description: "Un enemigo feroz con forma de coraz贸n que patrulla y mata al jugador al tocarlo.",

  animation: [{

    sprite: [

      ["transp","AA0000","AA0000","transp","transp","AA0000","AA0000","transp"],

      ["AA0000","FF0000","FF0000","AA0000","AA0000","FF0000","FF0000","AA0000"],

      ["AA0000","FF0000","FFFFFF","FF0000","FF0000","FFFFFF","FF0000","AA0000"], // 馃憖 white eyes

      ["AA0000","FF0000","FF0000","FF0000","FF0000","FF0000","FF0000","AA0000"],

      ["transp","AA0000","FF0000","FF0000","FF0000","FF0000","AA0000","transp"],

      ["transp","transp","AA0000","AA0000","AA0000","AA0000","transp","transp"],

      ["transp","transp","transp","AA0000","AA0000","transp","transp","transp"],

      ["transp","transp","transp","transp","transp","transp","transp","transp"]

    ]

  }]

};

```

---

#### Closing note

With Copilot the Enemy Chaser was created.  

This heart-shaped foe patrols the level, collides with walls, and defeats the player on touch.  

A chapter of memory and code, now shared with you.

Developer

thanks 馃檪

Honestly if this type of enemy is Important to you, you could download pocket platformer repo from github, add this code and create a game with this enemy. I don't want to add such special types of enemies to the game, i would rather work on some generic enemy creator in the future.

(1 edit)

Thank you so much for your reply 馃檹. I completely understand your vision of keeping Pocket Platformer clean and generic, and I really appreciate the clarity. For me this enemy is part of a personal project, so your suggestion to download the repo and add the code myself is perfect. I鈥檒l follow that path and integrate this enemy into my own project built on Pocket Platformer, while respecting the foundation you created. I鈥檓 also excited to see your future work on a generic enemy creator — that aligns beautifully with what I鈥檓 exploring. Thanks again for sharing Pocket Platformer with the community, it鈥檚 a wonderful base to create from 馃専.

Developer

thank you :)