Is there a more advanced isTouching method which returns the specific sprite being touched?

So I’m already familiar with the isTouching method which is called on a sprite with another sprite passed in as an argument, and returns a Boolean showing whether or not the two sprites are currently touching each other.

I am trying to make a more traditional paddle-ball type game out of Lesson 25, challenge 10b-c, where bricks become weaker and eventually disappear when the ball runs into them.

Right now, I am using if-statements to check each brick being touched before the ball bounces off of it. I noticed the lag when the ball does touch the brick and I think this is an inefficient way to constantly be polling for whether or not the brick touches the ball.

A better way would be to figure out that the ball is touching something, and receive a return value of the sprite which the ball is touching. I suppose I could track changes in the ball’s velocity to determine when it bounces off something, and figure out which brick it is near using its location, but I was wondering if there’s already a function written which could help figure out what the sprite is running into.

Or is there a way to leverage the bounceOff function to accomplish this task?

Any input would be greatly appreciated, thanks!

If you create a group of bricks and add each brick to the group, then you can check to see if the group of bricks is touching the ball. If it is, then figure out which brick is touching the ball by using a loop through the bricks checking each individual one and if it is make the ball bounce as well. I modified the brick creation code at the beginning to test it out with a lot of bricks and I don’t notice any lag on my computer. I also modified the brickStruck() function to figure out which brick was actually hit.

Here is a link to the project using a group.

1 Like