0

In Scratch I'm trying to achieve this "follow another sprite" movement, where the other sprite follows the first sprite's movement exactly, like in this game, Mountain of Faith:

https://www.youtube.com/watch?v=fqHYHOD2-ck

Basically what I'm looking is for the pseudo-code that would help me in both Scratch and JavaScript. I'm not really sure what to do other than that it (obviously) involved the X and Y position of the other sprite.

Thanks.

Matcha
  • 131
  • 6
  • You have the principle exactly right: base your followers' (x, y) on the (x, y) of the followed sprite. Now it's up to you to experiment and decide exactly how you want to calculate the followers' positions. I would start perhaps with an array that holds the last N positions of the followed sprite (you decide what N is, maybe 100 or so), then have your following sprites track the positions in that array, but after some delay, maybe 3 frames or so? Good luck! – SaganRitual Mar 28 '18 at 23:53
  • @GreatBigBore Sorry this sounds stupid but what is N in the first place and why would I set it to 100? And how does Scratch hold arrays or is it too limited to do that? – Matcha Mar 29 '18 at 00:13

3 Answers3

0

Here's some pseudocode that will get you started. This definitely won't be your final solution; you'll have to play around with the details to get it exactly like you want it. Best of luck to you!

let trackingArrayMaxSize = 100;
let trackingArrayFollowerStart = 50;
var trackingArrayLeaderIndex = 0;
var trackingArrayFollowerIndex = 0;
var trackingArray = [ ];

// Scratch, or whatever game engine you're using, is going to call this once per frame.
// Have a look at the Scratch wiki https://en.scratch-wiki.info/wiki/Game_Loop
// to see how Scratch does it.
function update() {

  ////////////
  // Use the array to track the current position of the main sprite.
  ////////////
  if(trackingArray.number_of_elements < trackingArrayMaxSize) {

    // If the tracking array hasn't reached the max size, then
    // just keep appending our current position to it.
    trackingArray.append(main_sprite_current_position)

  } else {

    // If we've reached the max size, then we start treating the
    // array as a circular array, storing each new position in the
    // next array index as normal, but wrapping back to index 0
    // when we reach the end.
    trackingArray[trackingArrayLeaderIndex] = main_sprite_current_position
    trackingArrayLeaderIndex = (trackingArrayLeaderIndex + 1) % trackingArrayMaxSize
  }

  ////////////
  // Read back from the array to set the position of your following sprite
  ////////////
  if(trackingArray.number_of_elements > trackingArrayFollowersStart) {

    // When the array becomes full enough (experiment to decide how full),
    // set your follower position to wherever the leader was, that many frames before.
    //
    //
    // So say we start with trackingArrayMaxSize = 100 and trackingArrayFollowersStart = 50.
    // At the top of this function each frame, we'll be storing the leader's positions into
    // the array. When the array has 50 elements in it, we come here and set the
    // follower's position to trackingArray[0] -- and up top, we'll be storing the leader's
    // position to trackingArray[50].
    //
    // Next time through, we'll store leader to trackingArray[51], and here we'll be setting the
    // follower's position to trackingArray[1]. So your follower will always be 50 frames
    // behind your leader.
    following_sprite_current_position = trackingArray[trackingArrayFollowerIndex]
    trackingArrayFollowerIndex = (trackingArrayFollowerIndex + 1) % trackingArrayMaxSize
  }

}
SaganRitual
  • 3,143
  • 2
  • 24
  • 40
0

This type of problem is one of inverse kinematics. I created a project that does this a while back. https://scratch.mit.edu/projects/250005153/

Essentially, what you want to do is have each successive member of the group move towards the member before it if it is greater than a certain distance away. This distance would be like the radii of circles, for example. This mimics the real life equivalent of a rope or atoms in a chain. Hope this helps!

0

I don't know enough of JavaScript yet but, This sort of thing is made really easy in Scratch.

Point toward Sprite, move (speed of Sprite) steps. Done.

And if that way don't work, Go To Sprite, point in direction of Sprite, turn 180, move distance required, and do this for each link going and pointing to each concurrent following link one behind the other. (in other words go to and point in direction of the last in the line)

This can be done with multiple sprites (easiest way), variables setting temporary positions and stamping, lists and stamping, or you could use clones (which is a little bit more complicated).

Nerp
  • 1