# Group sprites following other sprites

**URL:** <https://forum.code.org/t/group-sprites-following-other-sprites/40514>\
**Category:** Coding and Debugging Help\
**Created:** [March 7, 2025, 9:03pm UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514 "2025-03-07T21:03:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![su8scri88le](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@su8scri88le](https://forum.code.org/u/su8scri88le)\
**Post date:** [March 7, 2025, 9:03pm UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/1 "2025-03-07T21:03:08Z")

</div>

([Game Lab - Code.org](https://studio.code.org/projects/gamelab/HHv7s9JIq2lijfY0qHxegSizKFLAUC_OhAG2POGQL1M))  
My student is trying to create sprites in a group following another sprite, neither of us have any clue on how to create this behavior  
We have tried whatever we could think of, nothing has worked.

---

<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:** [March 8, 2025, 9:17pm UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/2 "2025-03-08T21:17:38Z")

</div>

Hello @su8scri88le,

I would recommend checking out this [topic](https://forum.code.org/t/group-assistance/38704/2) if this thread does not satisfy what you are looking for feel free to update the thread to give a more specific case of what type of following you are looking for

# Varrience

---

<div class="post-metadata">

**Author:** ![su8scri88le](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@su8scri88le](https://forum.code.org/u/su8scri88le)\
**Post date:** [March 9, 2025, 2:30am UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/3 "2025-03-09T02:30:03Z")

</div>

This is not the type of behavior we are looking for, I apologize for not being more clear in my previous post. What we are looking for is code to make a sprite move toward another one, as an attacking enemy.

---

<div class="post-metadata">

**Author:** ![ferg.christian13](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@ferg.christian13](https://forum.code.org/u/ferg.christian13)\
**Post date:** [March 9, 2025, 2:59am UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/4 "2025-03-09T02:59:14Z")

</div>

You could try this:

```auto
  
  var dx = targetX+randomNumber(10,30) - creature.x;
  var dy = targetY+randomNumber(10,30) - creature.y;
  var angle = Math.atan2(dx,dy);
  creature.x += speed*Math.sin(angle);
  creature.y += speed*Math.cos(angle);

```

where targetX and targetY are the points your enemy wants to move toward. dx is the distance x and dy is the distance y, this takes those values and finds the angle to the target and moves in that direction

---

<div class="post-metadata">

**Author:** ![su8scri88le](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@su8scri88le](https://forum.code.org/u/su8scri88le)\
**Post date:** [March 9, 2025, 6:28am UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/5 "2025-03-09T06:28:13Z")

</div>

i need this to work with groups so i can spawn multiple of them. Is there any way to do that?

---

<div class="post-metadata">

**Author:** ![ferg.christian13](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@ferg.christian13](https://forum.code.org/u/ferg.christian13)\
**Post date:** [March 9, 2025, 3:28pm UTC](https://forum.code.org/t/group-sprites-following-other-sprites/40514/6 "2025-03-09T15:28:24Z")

</div>

Use the for statement and add each sprite to the group, then every frame loop through every sprite in that group and apply the movement script to it.

```auto

var sprites = createGroup();
var spriteAmount = 10;
var defaultSpeed = 2;
for (var i = 0; i < spriteAmount; i++){
  
  var sprite = createSprite(randomNumber(0,400),randomNumber(0,400)); // makes a sprite at a random position at the screen
  sprite.scale = 0.5;
  sprites.add(sprite); //adds it to the group
  
}

function draw() {
  background("white");
  for (var i = 0; i < sprites.length; i++){ // starts i at 0 and as long as i < the amount of sprites in the group increases i.
     
    var thisSprite = sprites.get(i); //makes a variable for the current sprite in the group
    
    move(thisSprite, camera.mouseX, camera.mouseY, defaultSpeed); //calls the movement script 
    
  }
  
  
  drawSprites();
}
//previous code:
function move(sprite, targetX, targetY, speed){
  var dx = targetX+randomNumber(10,30) - sprite.x;
  var dy = targetY+randomNumber(10,30) - sprite.y;
  var angle = Math.atan2(dx,dy);
  sprite.x += speed*Math.sin(angle);
  sprite.y += speed*Math.cos(angle);
  
  
}

```
