# Enemies change Y position though code doesn't appear to do that

**URL:** <https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856>\
**Category:** Coding and Debugging Help\
**Created:** [March 9, 2026, 7:13pm UTC](https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856 "2026-03-09T19:13:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrichard](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jrichard/32/8919_2.png) [@jrichard](https://forum.code.org/u/jrichard)\
**Post date:** [March 9, 2026, 7:13pm UTC](https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856/1 "2026-03-09T19:13:14Z")

</div>

I’m making a Sonic Side Scroller as an example for my students. I have two issues. The second issue is related to the enemies scrolling. Also, a minor issue: I have the ground looping and can’t seem to get the math right for seamless looping. I can’t remember how to do that. Help with that would be appreciated as well.

**Link to the project or level:** [Game Lab - Code.org](https://studio.code.org/projects/gamelab/c0b7d964-fd39-4c89-8629-d3a65fa0a124)

**What I expect to happen:** Enemies should loop on the screen and randomize their X position either when they touch each other, Sonic, or when they scroll off the screen.  
**What actually happens:** They do loop on the x-axis, but they also randomize on the y-axis. I have my rings doing that, but the code for enemies doesn’t seem to include anything about the y-axis.  
**What I’ve tried:** I have scanned every line of code for anything related to motoBug, crabmeat, spikes, or moledrill and don’t see anything related to the y-axis.

---

<div class="post-metadata">

**Author:** ![edavis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/edavis/32/4927_2.png) [@edavis](https://forum.code.org/u/edavis)\
**Post date:** [March 11, 2026, 1:17am UTC](https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856/2 "2026-03-11T01:17:23Z")

</div>

Hello, this is a very neat game! I am not seeing the issue with the randomization of the x axis for the enemies. Maybe you already fixed it or maybe I’m missing something. Could you give a little more information if you’re still seeing the issue?

---

<div class="post-metadata">

**Author:** ![jrichard](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jrichard/32/8919_2.png) [@jrichard](https://forum.code.org/u/jrichard)\
**Post date:** [March 12, 2026, 4:36pm UTC](https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856/3 "2026-03-12T16:36:19Z")

</div>

It’s not the x-position that I was concerned with. The enemies loop on the x-axis, but they also seem to randomize their y-position sometimes though I don’t see where in the code it does this.

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [March 13, 2026, 9:54pm UTC](https://forum.code.org/t/enemies-change-y-position-though-code-doesnt-appear-to-do-that/41856/4 "2026-03-13T21:54:29Z")

</div>

Hi @jrichard ,

It looks like you figured out your enemy y positions. When they were added to the group, motobug became enemies[0], crabmeat became enemies[1], moledrill became enemies[3], etc. The x and y positions are then set in different blocks of code using for loops. The x positions for each are reset in the hit() function on line 70. You know this already because it is only resetting their x and not the y positions.

I think I got your background to loop. I think that is what your next question was. I set the bg2 x position so that is always relative to the first bg x position:

var bg = createSprite(200, 200);  
bg.setAnimation(“1”);  
var bg2 = createSprite(bg.x+400, 200);  
bg2.setAnimation(“2”);

Then in the background() function, I did also set the movement and positioning for bg2 relative to bg:  
function gameBackground() {  
textVisible = false;  
bg.velocityX=-1;  
bg2.velocityX=-1;  
bg2.x=bg.x+400;  
if (bg.x \< -200) {  
bg.x = 200;  
}  
}

It seems to be working in [this version](https://studio.code.org/projects/gamelab/97ce3df3-a2a3-47e9-9f5a-ec8442c59b9e) but I didn’t play a full game:)

Hope it helps! Fun game!

~Michelle
