Keywords:

target targets hashit beenhit

Collision Detection

Open the Collision Detection sandbox to play around as you read this

You can detect when two sprites collide using collision detection. For performance reasons, SCL uses a very specific method to do this. First, you must specify which sprites to monitor for collisions. Secondly, collisions are detected based on the center point of the colliding sprite.

For example, to determine when an arrow hits a ball, you specify that the ball is the target of the arrow. When the center point of the arrow overlaps a non-transparent point on the ball sprite, the collision events are fired.

create sprite from arrow.png as Arrow where   center=50,12 x=50 y=100 hashit=HasHit having target=Ball   alt=(sub     create vector where speed=100   end) end create sprite from ball.png as Ball   where center=25,25 x=250 y=100 beenhit=BeenHit end create routine as Start   launch Arrow   launch Ball end create routine as HasHit   drop Arrow end create routine as BeenHit   drop Ball end

You may also want to target many sprites at once. In this case you can target a group of sprites using the “into” keyword in your sprite definition and the targets keyword instead of target.

create sprite from arrow.png as Arrow where   center=50,12 x=50 y=100 hashit=HasHit having targets=Balls   alt=(sub     create vector where speed=100     end) end create sprite from ball.png as Ball1 into Balls   where center=25,25 x=250 y=100 beenhit=BeenHit end create sprite from ball.png as Ball2 into Balls   where center=25,25 x=350 y=100 beenhit=BeenHit end create routine as Start   launch Arrow   launch Ball1   launch Ball2 end create routine as HasHit   update sprite Arrow where alpha=Arrow.alpha-.35 end create routine as BeenHit   drop _sprite end

In the example above, both ball sprites are put into the same group, Balls. The arrow uses targets to detect collisions with any sprite in the Balls group.