FAQ: How Do I Detect When A Collision Has Ended?

In Stencyl, detecting when a collision occured is handled through the collision event/detector. However, there is no such events for when collisions are NOT occuring. Thus the question comes up somewhat frequently as to how one detects when a collision is over.

Let’s start with two boolean attributes. Let’s name the first “Actor Hit?” and the second “Actor Was Hit?” (Since they are very similar in name, I’ve highlighted them to help you keep them sorted.) “Actor Hit?” acts as a go-between for the collision event and “Actor Was Hit?”. “Actor Was Hit?” is the boolean that will actually reflect whether a collision is going on (true) at the moment or not (false).

First, in your collision event, set “Actor Hit?” to true. This is our simple way of signifying that a collision occured, of course.

Now, let’s use the Always wrapper. This is where we will update “Actor Was Hit?”. Set “Actor Was Hit?” to the value of “Actor Hit?”.

Makes sense so far, but how does this help us? All we’ve done so far is pass the collision boolean into another boolean. We haven’t actually handled what happens to facilitate “Actor Was Hit?” going back to false when the collision is over. After you’ve put the above step into your Always wrapper, set “Actor Hit?” to false.

Here’s how it works. We did just set “Actor Hit?” to false, but should a collision still be happening, it will be put back to true. That means next run of the Always wrapper, “Actor Was Hit?” just sees “Actor Hit?” at true again and stays true itself. Once the collision is over, “Actor Hit?” stays at false, and then “Actor Was Hit?” sees that and becomes false too.

And there you go. You use “Actor Was Hit?” (not “Actor Hit?”) to check and see if a collision is occuring at the moment or not. Here is some example code for you to look at:

CollisionCheck

Happy Stencyling!

Advertisements
Posted in Collisions, FAQ, Stencyl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: