Creating color sensors

Question from my students:

How do we create a code so that when the player touches a red or green colored space, the “earnings” (text box) increase or decrease?

This CoSpaces is created like a board game, so when the player touches any green space, the text that says “earnings” would increase by 10. Alternatively, if the player touches any red space, the text would decrease by 10.

I think it may have something to do with creating Parent/ Child of all green spaces/ red spaces but I’m not totally sure.

Linked is the game!


Hi @Gabriela_Porras,

There are 2 questions you’re asking here:

  1. How can we make something happen when the player (Camera) touches (collides) a type of square?
  2. How can we make the earnings increase or decrease?

The 1st problem can be solved using the advanced collision CoBlock, and through checking the name of the Item collided to see if it is a red or green square (make sure all red squares have the same name - same for green).

The 2nd is fairly easy; see the following remixable space - it should give some clues as to how to update a variable & text:

I think there is another issue which your student will encounter, as they realise that they need to cross several red/green squares to get to their destination square - but cross that bridge when you get to it!

Hope that helps! Let me know if you have any further questions about this.

Geoff @ TechLeap

1 Like

Hi @techleapnz ,

Thanks so much for your help! Yes, agree that an advanced collision block will need to be used. I was trying to find a way for students to avoid adding a collision for EACH green space/ red space-- maybe more concise code? :slight_smile:

I tried renaming multiple green spaces with the same naming convention, but that didn’t help.

Here’s what I was playing with so far, (the Earnings text would change by 10 as the camera collides with a Greensquare):

Any ideas?

1 Like

Your variable/text updating code is fine.

Here’s an example of how you can use the advanced collision CoBlock. You’ll need to enable Advanced CoBlocks first in Settings:

Note that I’ve renamed all copies of items to have the same name.

See the following remixable CoSpace for details:

1 Like

Ah, I see now! Thank you so much!!!