Help with the level canyon of storms


#1

in this level you have to collect coins while yaks are there, then hide when they are gone.
the problem is that I won’t stop collecting even after the yaks are gone.
here is the code :

// A desert storm is coming!
// Yaks can detect when a storm is coming.

// This variable will be used as a condition.
var yak = hero.findNearest( hero.findItems());

// While there is a sand yak:
while (yak) {
// Collect a coin.
var coin = hero.findNearest( hero.findItems());
if (coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}

// Update the variable "yak".

}
// The yaks have hidden.
// Move to the hideout.
hero.moveXY(38, 58);

how do I update the variable? what does it mean?
please help


#2

Because
// Update the variable "yak".

If you don’t update it for each cycle, then yak contains a value from the first frame.


#3

and how do I update it?
I am new to coding


#4

It’s an adventurer level. Have you completed levels before?

yak = findNearestEnemy();

#5

I have completed the dungeon and forest worlds, I just never encountered an update instruction.
thanks a lot for your help


#6

Hm. Thanks. It’s a useful feedback. I think I should rewrite this comment-instruction.


#7

Can you explain : then yak contains a value from the first frame. ?
In another way : what is the difference between first variable yak and the updated one?
I’m newbie and thanks for helping :slight_smile:


#8

On the first frame (first second, start the game etc) we definitely see yaks here. So yak variable contains the result of the findNearestEnemy.

But then the level state is changing and if you don’t update yak then it contains the result from the first call. So we need to recheck the state and write the new results of findNearestEnemy in the yak variable. It’s updated value.


#9

Thank you so much.:blush: