Sprites not looping

Hi. Me and my student are trying to get the cats to loop on and off the screen. They will loop off screen but do not come back.

We have tried adjusting the sprite.velocityx and sprite.velocityy from -50 to 50.

Hi @kjones,

If I understand your question correctly, when the orange cat’s Y gets to the bottom, you would like it to reset back to y=50 and a random x. The same for the brown cat…when it’s X is greater than 450, you would like it to reset back at x random and y=50. (I wonder if you meant x=50 and random y? so that it was at the same position on the left and a random position vertically).

If this is the case, then I would look at your boolean question in the if statements. For the orange cat, you are testing for VelocityY. Should you be testing for just Y? For the brown cat, you are testing for Y but the cat is moving along the X axis. Should you be testing for X?

Am I seeing your game correctly with those tips?

First comment out the dog bounce:
Screenshot 2020-12-19 at 1.27.33 PM
Next set up some watchers:
Screenshot 2020-12-19 at 1.27.51 PM
Now run that program.

The x and y of the cats are not doing what they are supposed to.
Screenshot 2020-12-19 at 1.40.14 PM

Let’s look at how we start those sprites out:

var orange_cat_1 = createSprite(randomNumber(0, 400), -50);
// other code removed for clarity.
var brown_cat_1 = createSprite(-50, randomNumber(0, 400));

What we see is that orange cat has a random x and a fixed y. Brown cat has a fixed x and a random y. Compare that to your code:

  if (orange_cat_1.velocityY > 450) {
    orange_cat_1.x = randomNumber(50, 350);
    orange_cat_1.y = 50;
  if (brown_cat_1.y > 450) {
    brown_cat_1.x = randomNumber(50, 350);
    brown_cat_1.y = 50;

Brown cat is broken.
Let’s look at the direction they move in:

orange_cat_1.velocityY = 3;
brown_cat_1.velocityX = 3;

So the orange cat has y increasing by 3 each time. Wait! What are we checking there?
Brown cat has the x increasing by 3 each time. Are we checking if x has gone out of bounds?

Make some changes and see what happens.