# Problem with Flyer Game

**URL:** <https://forum.code.org/t/problem-with-flyer-game/38079>\
**Category:** Coding and Debugging Help\
**Created:** [May 8, 2023, 7:14pm UTC](https://forum.code.org/t/problem-with-flyer-game/38079 "2023-05-08T19:14:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmarro](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jmarro](https://forum.code.org/u/jmarro)\
**Post date:** [May 8, 2023, 7:14pm UTC](https://forum.code.org/t/problem-with-flyer-game/38079/1 "2023-05-08T19:14:10Z")

</div>

> **[Check out what I made](https://studio.code.org/projects/gamelab/EJ4hPoKHbZyEJu5MUJiY5UnJCATjdluPArR_sc0udps)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![jmarro](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jmarro](https://forum.code.org/u/jmarro)\
**Post date:** [May 8, 2023, 7:15pm UTC](https://forum.code.org/t/problem-with-flyer-game/38079/2 "2023-05-08T19:15:09Z")

</div>

Help with looping please!

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [May 8, 2023, 9:23pm UTC](https://forum.code.org/t/problem-with-flyer-game/38079/3 "2023-05-08T21:23:28Z")

</div>

Well this is something that you should be able to do on your own I’ll provide the solution but first we should understand what’s really going on first don’t you think?

> Why won’t my enemies re-spawn?

Let’s take a look at the instances of rock that you made

```auto
var rock1 = createSprite(-50, randomNumber(0, 400));
rock1.setAnimation("rock1");
rock1.scale = .2;
rock1.velocityX = 5; // this is what we care about on both of them
var rock = createSprite(randomNumber(0, 400), -50);
rock.setAnimation("rock");
rock.velocityY = 5; // this one as well
rock.scale = .2;

```

by looking at this sample of your code you should be able to deduce that **rock1 is traveling on the x-plane** while **rock is traveling on the y-plane** , now lets also look at the loop in which you want the enemies to re-spawn

```auto
// LOOPING
  if (rock.x > 450) {
    rock.x = 50;
    rock.y = randomNumber(50, 350);
  }
  if (rock1.y > 450) {
    rock1.y = 50;
    rock1.x = randomNumber(50, 350);
  }

```

> What’s actually wrong with my program?

figure it out yet? if not here your doing the exact opposite comparisons on the velocity that your started your rocks in which means rock.x will never reach 450 & rock1.y will never reach 450 either so you either have to

1:) swap comparisons or change the spawn position of both rocks along with the set velocities at the start

2:) modify comparisons in the loop

> Solution Example

since you wanted the loop explicitly fixed here I will do the 2nd option i provided in my explanation

```auto
if (rock.y > 450) {
    rock.y = 0;
    rock.x = randomNumber(50, 350);
  }
  if (rock1.x > 450) {
    rock1.x = 0;
    rock1.y = randomNumber(50, 350);
  }

```

Hope this helps!
