# \[Adventurer\] Run for Gold

**URL:** https://discourse.codecombat.com/t/adventurer-run-for-gold/12649
**Category:** Adventurer
**Created:** [October 10, 2017, 12:35pm UTC](https://discourse.codecombat.com/t/adventurer-run-for-gold/12649 "2017-10-10T12:35:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)
#### Post date: [October 10, 2017, 12:35pm UTC](https://discourse.codecombat.com/t/adventurer-run-for-gold/12649/1 "2017-10-10T12:35:03Z")

</div>

![rungold](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/5/50d5f7db850cdc733535af0a7fdaf51203166c83.png)

Create, collect, destroy. Control treasures in the new gamedev level [Run for Gold](https://codecombat.com/play/level/run-for-gold)

It’s a practice level for “game time” mechanic. Control several timers for unit and items.

---

<div class="post-metadata">

### Author: ![Jack\_Gitto](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@Jack\_Gitto](https://discourse.codecombat.com/u/Jack_Gitto)
#### Post date: [April 23, 2018, 3:20pm UTC](https://discourse.codecombat.com/t/adventurer-run-for-gold/12649/2 "2018-04-23T15:20:01Z")

</div>

what was your code please???

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 23, 2018, 4:11pm UTC](https://discourse.codecombat.com/t/adventurer-run-for-gold/12649/3 "2018-04-23T16:11:52Z")

</div>

No one provides working solutions in this forum. If you’re having trouble solving the level, please post your code and there are many here who are more than happy to help you. This is a learning forum and simply providing solutions is not helpful and in fact, counter productive.

---

<div class="post-metadata">

### Author: ![aYo11](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ayo11/32/12012_2.png) [@aYo11](https://discourse.codecombat.com/u/aYo11)
#### Post date: [June 9, 2020, 5:58pm UTC](https://discourse.codecombat.com/t/adventurer-run-for-gold/12649/4 "2020-06-09T17:58:40Z")

</div>

I came across a strange behavior that reproduces once in a while. As my player stays at a location where a valuable item was spawn, the game score goes up infinitely without any further actions. It stops as I move my character away from the spot.

[![](https://img.youtube.com/vi/-gput-TfbS0/maxresdefault.jpg "Codecombat Run For Gold Bug") ](https://www.youtube.com/watch?v=-gput-TfbS0)

Watch the game score at 0:14.

> **Below is the code I used. Not sure where it went wrong...**
>
> ```python
> // Generate randomly placed treasures and destroy them later.
> 
> // This spawns an item with lifetime at a random point.
> function spawnValuable(itemType, lifetime) {
> var x = game.randomInteger(12, 68);
> var y = game.randomInteger(12, 56);
> var item = game.spawnXY(itemType, x, y);
> item.destroyTime = game.time + lifetime;
> }
> 
> // This spawns the treasure set.
> function spawnTreasures() {
> spawnValuable("bronze-coin", 6);
> spawnValuable("silver-coin", 5);
> spawnValuable("gold-coin", 4);
> spawnValuable("gem", 2);
> }
> 
> // The event handler for items.
> function onSpawn(event) {
> var item = event.target;
> while(true) {
> // If game time is greater than item destroyTime: ;
> if (item.destroyTime < game.time) {
> // Call item's destroy() method:
> item.destroy();
> }
> }
> }
> 
> game.setActionFor("bronze-coin", "spawn", onSpawn);
> game.setActionFor("silver-coin", "spawn", onSpawn);
> game.setActionFor("gold-coin", "spawn", onSpawn);
> game.setActionFor("gem", "spawn", onSpawn);
> 
> // Game settings, goals and UI.
> game.spawnTime = 0;
> game.spawnInterval = 3;
> game.score = 0;
> if (!db.get("topScore")) {
> db.set("topScore", 0);
> }
> ui.track(game, "time");
> ui.track(game, "score");
> ui.track(db, "topScore");
> var goal = game.addManualGoal("Collect at least 50 gold in 30 seconds.");
> 
> // The player setup.
> var player = game.spawnPlayerXY("captain", 40, 34);
> player.maxSpeed = 25;
> 
> function onCollect(event) {
> var item = event.other;
> // If the item has a "value" property:
> if (item.value){
> // Increase the game score by item's value:
> game.score+=item.value;
> }
> }
> 
> player.on("collect", onCollect);
> 
> // This checks timers for treasure spawning.
> function checkSpawns() {
> // If game.time is greater than game.spawnTime:
> if (game.time>game.spawnTime){
> // Call spawnTreasures to create more items.
> spawnTreasures();
> // Increase game.spawnTime by game.spawnInterval:
> game.spawnTime+=game.spawnInterval;
> }
> }
> 
> // This checks the goal state.
> function checkGoal() {
> if (game.score >= 50) {
> game.setGoalState(goal, true);
> }
> else {
> game.setGoalState(goal, false);
> }
> }
> 
> // This checks the game state.
> function checkGameOver() {
> if (game.time > 30) {
> checkGoal();
> db.set("topScore", game.score);
> }
> }
> 
> while (true) {
> checkSpawns();
> checkGameOver();
> }
> 
> ```
