[SOLVED] Help with hot gems

when i collect gold, it heals me, but i don’t lose health when i take gems, can somebody tell why?
here’s the code (javascript):
var player = game.spawnPlayerXY(“captain”, 40, 34);
player.maxSpeed = 20;
// We’re using relative health value for gems/coins.
player.attackDamage = 30;
var healthValue = player.maxHealth / 4;

// This function spawns an item/unit in a random place.
function spawnRandomPlaced(type) {
var x = game.randomInteger(12, 68);
var y = game.randomInteger(12, 56);
game.spawnXY(type, x, y);
}

// Auxiliary variables, goals and UI.
var itemInterval = 2;
var itemSpawnTime = 0;
game.collected = 0;
ui.track(game, “time”);
ui.track(game, “collected”);
ui.track(player, “health”);
game.addCollectGoal(10);
game.addSurviveGoal();

// Let’s make ogres aggressive.
function onSpawn(event) {
var unit = event.target;
unit.behavior = “AttacksNearest”;
}

game.setActionFor(“munchkin”, “spawn”, onSpawn);

// This defines the result of collecting different items.
function onCollect(event) {
// event.target contains the collector.
var collector = event.target;
// event.other contains the collected item.
var item = event.other;
// Increase the game.collected by 1.
game.collected += 1;
// If item’s type is “gold-coin”:
if (item.type == “gold-coin”)
// Increase collector.health by healthValue:
player.say (“yay!”);
collector.health += healthValue;
// If item’s type is “gem”:
else () {
// Increase collector.health by healthValue:
player.say (“ouch!”);
collector.health -= healthValue;

}

// Assign onCollect handler for player on “collect” event.
player.on(“collect”, onCollect);

function checkSpawnTimer() {
if (game.time >= itemSpawnTime) {
spawnRandomPlaced(“gem”);
spawnRandomPlaced(“gold-coin”);
spawnRandomPlaced(“munchkin”);
itemSpawnTime += itemInterval;
}
}

while(true) {
checkSpawnTimer();
}

1 Like

Hi @foxfire.
If you want someone to help you learn how to post/format your code correctly

1 Like
var player = game.spawnPlayerXY(“captain”, 40, 34);
player.maxSpeed = 20;
// We’re using relative health value for gems/coins.
player.attackDamage = 30;
var healthValue = player.maxHealth / 4;

// This function spawns an item/unit in a random place.
function spawnRandomPlaced(type) {
var x = game.randomInteger(12, 68);
var y = game.randomInteger(12, 56);
game.spawnXY(type, x, y);
}

// Auxiliary variables, goals and UI.
var itemInterval = 2;
var itemSpawnTime = 0;
game.collected = 0;
ui.track(game, “time”);
ui.track(game, “collected”);
ui.track(player, “health”);
game.addCollectGoal(10);
game.addSurviveGoal();

// Let’s make ogres aggressive.
function onSpawn(event) {
var unit = event.target;
unit.behavior = “AttacksNearest”;
}

game.setActionFor(“munchkin”, “spawn”, onSpawn);

// This defines the result of collecting different items.
function onCollect(event) {
// event.target contains the collector.
var collector = event.target;
// event.other contains the collected item.
var item = event.other;
// Increase the game.collected by 1.
game.collected += 1;
// If item’s type is “gold-coin”:
if (item.type == “gold-coin”)
// Increase collector.health by healthValue:
player.say (“yay!”);
collector.health += healthValue;
// If item’s type is “gem”:
else () {
// Increase collector.health by healthValue:
player.say (“ouch!”);
collector.health -= healthValue;

}

// Assign onCollect handler for player on “collect” event.
player.on(“collect”, onCollect);

function checkSpawnTimer() {
if (game.time >= itemSpawnTime) {
spawnRandomPlaced(“gem”);
spawnRandomPlaced(“gold-coin”);
spawnRandomPlaced(“munchkin”);
itemSpawnTime += itemInterval;
}
}

while(true) {
checkSpawnTimer();
}
2 Likes

In else () { you don’t need “()”.

if (item.type == “gold-coin”)
// Increase collector.health by healthValue:
player.say (“yay!”);
collector.health += healthValue;
// If item’s type is “gem”:

In “if” you always need “{” and “}” at the end of the “if”. Also you need “{” and “}” in onCollect function. You have only “{”.
Hope it helps.
Dima

2 Likes

@DimaP, thank you for helping me solve :two: of my problems!
that’s very nice of you :heavy_heart_exclamation:

3 Likes

You’re welcome, and congratulations with completing the level! :partying_face:
Dima

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.