Game development 2 Level 19 Accounts Department Help

For this Level, I completed all the goals except “Change Game Score based On value of items”. I looked at hints and it didn’t help… at all. I still don’t get how to do it. could someone help me? I’ll copy paste my code below >:)
// Count collected item values and use them for scoring.

// Setup characters.
var player = game.spawnPlayerXY(“captain”, 40, 34);
player.maxSpeed = 25;

// Chests of gems are the most valuable items.
game.spawnXY(“chest”, 68, 56);
game.spawnXY(“chest”, 14, 14);

// This function spawns a random item in a random place.
function spawnRandomItem() {
var itemNumber = game.randomInteger(1, 3);
var x = game.randomInteger(12, 68);
var y = game.randomInteger(12, 56);
if (itemNumber == 1) {
game.spawnXY(“bronze-coin”, x, y);
}
else if (itemNumber == 2) {
game.spawnXY(“gold-coin”, x, y);
}
else if (itemNumber == 3) {
game.spawnXY(“gem”, x, y);
}
}

var itemInterval = 3;
var itemSpawnTime = 0;
function checkSpawnTimer() {
if (game.time >= itemSpawnTime) {
// Call the spawnRandomItem function.
spawnRandomItem();
itemSpawnTime += itemInterval;
}
}

game.score = 220;

// The “collect” event is triggered by collecting something.
function onCollect(event) {
// event.target contains the collector.
var collector = event.target;
// event.other contains the collected item.
var item = event.other;
if (item.value) {
// Adding health power for valuable items.
collector.health += item.value;
// Increase the game score by the item’s value :

}

}

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

// The score the player needs to win.
var requiredScore = 220;
var goldGoal = game.addManualGoal(‘Collect at least 220 gold in 20 seconds.’);
ui.track(game, “score”);

function checkGoals() {
if (game.score >= requiredScore) {
game.setGoalState(goldGoal, true);
}
}

while(true) {
checkSpawnTimer();
checkGoals();
}

Thank you!Preformatted text

1 Like

Hello and welcome to the forum @Amira_Saadi :partying_face:

To format your code properly, please press the </> button
image

after you format press the button and put your code inside, it should look like this:

// This is an example

could you send a link to the level please @Amira_Saadi?

I tried doing that, it didn’t seen ti do anything.

I can do it for you this time but first you should click the button then paste your code

// Count collected item values and use them for scoring.

// Setup characters.
var player = game.spawnPlayerXY(“captain”, 40, 34);
player.maxSpeed = 25;

// Chests of gems are the most valuable items.
game.spawnXY(“chest”, 68, 56);
game.spawnXY(“chest”, 14, 14);

// This function spawns a random item in a random place.
function spawnRandomItem() {
var itemNumber = game.randomInteger(1, 3);
var x = game.randomInteger(12, 68);
var y = game.randomInteger(12, 56);
if (itemNumber == 1) {
game.spawnXY(“bronze-coin”, x, y);
}
else if (itemNumber == 2) {
game.spawnXY(“gold-coin”, x, y);
}
else if (itemNumber == 3) {
game.spawnXY(“gem”, x, y);
}
}

var itemInterval = 3;
var itemSpawnTime = 0;
function checkSpawnTimer() {
if (game.time >= itemSpawnTime) {
// Call the spawnRandomItem function.
spawnRandomItem();
itemSpawnTime += itemInterval;
}
}

game.score = 220;

// The “collect” event is triggered by collecting something.
function onCollect(event) {
// event.target contains the collector.
var collector = event.target;
// event.other contains the collected item.
var item = event.other;
if (item.value) {
// Adding health power for valuable items.
collector.health += item.value;
// Increase the game score by the item’s value :

}
}

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

// The score the player needs to win.
var requiredScore = 220;
var goldGoal = game.addManualGoal(‘Collect at least 220 gold in 20 seconds.’);
ui.track(game, “score”);

function checkGoals() {
if (game.score >= requiredScore) {
game.setGoalState(goldGoal, true);
}
}

while(true) {
checkSpawnTimer();
checkGoals();
}

Yes as @Falcons118 and @Monsty told you please format your code properly.

When it is unformatted it looks like this

hero.findNearestEnemy()

however when you format it using this Preformatted text which are two of these:

``

your code will look like:

hero.findNearestEnemy()

I’ll go ahead and let the two other users take it from here though

:wolf::wolf::wolf:

1 Like

tysm! this is my very 2nd time on this sight, it took me really long to even figure out how to make a new topic. thanks, it’s a big help :>

No problem ik you weren’t saying that abt me but ill take it lol

2 Likes