Collision handlers in CoBlocks

Great thanks so much for that, as well I had a quick question about “when item collides with” block in this scene https://cospac.es/dyA1 only one of those works, code is Coblocks(3). When the player walks over trigger0 nothing happens but trigger1 works?

HI @Outpost_31,

unfortunately I couldn’t find your when item collides - blocks in your shared scene. But I believe I know where the problem lies:

Keep in mind that you can not place multiple event handlers of the same type on one item. This means if you add two when item collides on the same item, only the last event will be triggered. Let’s demonstrate with an example:

Show example

We add two events of the same type on an item like this:
11

This is what happens:
collision

The latest event-handler always overwrites all others of the same type on an item.

The different types of event handlers are:
-When item collides
-When item is clicked
-When item is hovered

Hope that makes using event handlers more clear to you!

I see thanks, so let’s say if i wanted the camera to trigger let’s say 6 seperate events, each event activated when you walk over something, i need to create six different events and placecthrm in separate windows?

Hi @Outpost_31,

here’s an example on how you can set up collision events for one item against many different ones.

Solution a)


In the scene: a Boy-item walks forward into 5 different cubes.

We create the when item collides with [anotherItem] - block. This block will trigger whenever the boy collides with any other item in the scene.

When you drag this block into the workspace it creates a variable called "anotherItem". This variable can be found in the Data -> Variables category and represents the latest item the boy has collided against.

To create different behaviour for any of the other cubes, we use if-statements to check what kind of cube has been collided against like this:


Here we create if-statements to check which cube was collided, then let the boy say something different depending on the cubes.

With this script in place, let’s move our item forward and see what happens! Make sure that event-handlers are created before you animate items, otherwise the handlers are set after the item is done moving.

Click to see final code & result

Solution b)

We add event handlers on the cubes instead, not the boy. This way we make sure that only one collision handler is on any given item.

Show final code


etc…

Wrapup

Solution a) is more elegant as you can manage collisions from one event-handler. Solution b) is more suitable for quick collision testing between a few single items.

Hope this helped! Click here to make your own copy of the examples by clicking the remix button!

4 Likes

Awesome, this has helped tons on getting me past this hurdle :slight_smile:

1 Like

Thanks a lot. Your example was of great help.

However, I wasn’t able to generate the “anotheritem” variable myself. I did as you said and pulled it into the workspace but I only get in German “keine Variablen” which means “no variables”.

I wanted to try the following thing: A shooting star is fired. If the star hits the correct brick it vanishes. If it hits the wrong brick, nothing happens (except a taunting comment).

For that I tried your code and copied it. However, I cannot re-create it with the given block code. Here is an example what I mean:
The active code is the code I copied from you. The greyed out, inactive, code is mine. I have no means to re-create the yellow block #2 (your picture above) with the collides with item [anotheritem]. I have to add a red variable…and then it doesn’t work.!

Thank you very much for this great post @Stefan , it helped me so much!

I was stuck with the CoBlock ‘when … collides with …’ as I didn’t know that we can’t place multiple event handlers of the same type on one item. But your solutions are brilliant :smiley:

I know it is a bit late to get an answer on this topic, but I just found this solution today searching the web, and it works great. Concerning your problem, you have 2 CoBlocks available for the ‘when … collides with …’ and I thing you are using the first one in the image below, instead of the second one, which is used un @Stefan examples.