I am having trouble making my scavenger hunt with a timer. I have created a simple code which I thought would work, but didn’t (I am a rookie at this). The Basics of this gam is where you have 60 seconds to find 20 items randomly placed everywhere. Attached is a photo/video of my Code, and please reply to any mistakes I have made, or any suggestions. Thanks you.
Kind Regards, Sebastian Bailey
“set an event listener for item clicks and remove it from the list once clicked” == “set an event listener for item clicks” + “remove item from list once clicked”.
1. Set an event listener for item clicks
To set an event listener for each item in the list, we need to use a for each element in list CoBlock (under Control), add a when item is clicked block inside that loop, then drop the oblong element variable (in the Data category) into the oblong “item” space in when item is clicked:
2. remove item from list once clicked
Inside the yellow click event listener block, we need to add a delete item 0 from Items block (in the Data > List category). We then need to replace the 0 (zero) with the “index of item in Items” block (in the same place).
An index is the number place of the item in the list (1st, 2nd, 3rd etc), but the count starts at 0, so zero/0 is the 1st item, 1 is the 2nd item etc.
Finally, we are going to swap the oblong “item” in the index of item in Items block, with the oblong (value of variable) “element” block, so that the 3 blocks kind of look like this:
Your code is perfect. The 3 blocks basically says, “delete this item from the list of Items”.
Normally the delete block deletes an item at a particular index/position in the list, so in this code we’re feeding in the position of the item we’re currently using in the loop.
Next we need a way to check how many items are left in the list and check this somewhere in the timer code. We just need an if-then block (under Control), a logic = block (under Operators) and a length of Items block (in Data category) to see if the number of items in the list = 0 (zero).
If the length of the Items list is zero, then all items have been clicked.
Have a go at coding that and let me know when you’re done. Don’t worry too much about where you put the code - we’ll look at that next.
@techleapnz, I have already finished the timer, in which when it reaches 0, you go to a different scene. The code isn’t working, for as soon as I spawn in, it has the WooHoo sign thing. How do I fix this?