Unit 6 Lesson 9 Help


I have two groups that are trying to create games where they want something to happen if one image touches another image. For example, one wants a basket to catch falling fruit. She was able to make the fruit fall and the basket move back and forth across the bottom, but we are unable to figure out how to get the fruit to change its y value when it touches the basket. The premise of the game is that once a fruit hits the ground, the game is over. Any ideas how we can make this happen? We know how to do it in Game Lab, but not in Maker Toolkit using App Lab.


Here is a link to her code to give an idea of what she is trying to do: https://studio.code.org/projects/applab/Ea5fM4RMiVs2sKgAgebVh4SMRjqR8hHPCRemyvIcm6o


I guess the problem is that there is no nice isTouching function in app lab.
I’d try and use the distance formula between the two objects and then check if that distance is less than a certain value.
something like

  var basket_X = getProperty("basket2", "x");
  var basket_Y = getProperty("basket2", "y");
  var banana1_X = getProperty("banana1", "x");
  var banana1_Y = getProperty("banana1", "y");

var distance = Math.sqrt( ( banana1_X- basket_X )*( banana1_X- basket_X ) + ( banana1_Y- basket_Y )*( banana1_Y- basket_Y ) ) ;
//not sure what number to put for 20. You can experiment with that.
if(distance < 20) 
// do what you need to when they collide. 

Hopefully this will help. Let us know how it goes.