Melaka Defense

Learning guide

These snippets mirror what you can express in the Visual tab or in generated code. Drop patterns into your cannon functions or adapt them in the Code (generated) view after building blocks.

Simple Command Call

Call one API method to make one cannon action.

artillery.fireRentaka(ship.id);
command.log("Rentaka fired.");

For Loop

Repeat an action. Rentaka allows 3 bullets per cannon per tick.

for (let bullet = 0; bullet < 3; bullet++) {
  artillery.fireRentaka(ship.id);
}

If Else

Choose different weapons based on ship type or HP.

if (ship.type === "Colonizer Galleon") {
  artillery.fireMeriam(ship.id);
} else {
  artillery.fireRentaka(ship.id);
}

Recursion

A function can call itself, but always include a base case.

const countShips = (ships, index = 0) => {
  if (index >= ships.length) return 0;
  return 1 + countShips(ships, index + 1);
};

Best practices

  • - Use early returns when there is no target.
  • - Keep repeated logic in helper functions.
  • - Avoid infinite loops like while (true).
  • - Prefer clear names such as nearestShip and weakestShip.

Return to Melaka Defense