How do you make the screen change when two sprites collide/touch each other?

  • I would like it so on level 1, when you press start you have to jump over all the objects and when you touch one of them it changes the screen.
  • What actually happens when I press start and play the game is that I can just touch the obstacles and they will go right through the thing trying to avoid it.
  • I’ve already tried to look at some videos on youtube but their game is a different format to mine so the code doesn’t work.

(Also it isn’t a finished game so when you press play, only go into level one. And I’ve only coded the rock to move around the screen as an obstacle - at the moment)

Thanks for any help on this!

First, let me observe that this is the type of game students would use Game Lab to create. There are many extensions in Game Lab for touching sprites. But you seem to have simplified the animation to a point where App Lab will work fine.

I like how you have abstracted out moveObject() and wrapAround().

What you are missing is code to detect a collision. In this program, you would need to know if Benny is jumping. Perhaps a boolean? Then all you need to do is check for collisions each time moveObject() is called. For a horizontal scrolling game like this you simply check if the object is under the Benny image and that Benny is not currently jumping. You can check if the object is under Benny by comparing x positions. Looking at your code I know you can code that easily.

If it is the case that the object is under Benny and he is not in the jump position you can assume a collision, else no collision. With simplified animation, I would not worry about the images actually overlapping which is way too complex for AP CSP students.

Hey Kate, I see you tried to implement it in your code but it didn’t seem to really work - here’s a simple collision system that I made up.

Only major change is in this event

onEvent("Startbutton", "click", function( ) {
  timedLoop(50, function() {
    moveObject ("Rock", -10);
    var Jump = getProperty("BennyGameRun", "hidden");
    var RockX = getProperty("Rock", "x");
    if (!Jump && RockX <= 50) {

I remixed it if you want to see the code (all changes are towards the bottom)
Remix: Better With Bandaids - App Lab

Basically you’ve already coded everything needed for collisions, so I just had to add a few lines of code to make it work. The 50 you see in the code represents around when the rock touches the sprite, and Jump represents if the player is midair or not.

Thank you so much! This has really helped me!