如何实现这种“跟随另一个精灵”的运动?

时间:2018-03-28 23:44:19

标签: javascript mit-scratch

在Scratch中我试图实现这个“跟随另一个精灵”运动,其他精灵完全遵循第一个精灵的运动,就像在这个游戏中,信仰之山:

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

基本上我正在寻找的是能够在Scratch和JavaScript中帮助我的伪代码。除了它(显然)涉及另一个精灵的X和Y位置之外,我不确定该做什么。

感谢。

3 个答案:

答案 0 :(得分:0)

这是一些可以帮助你入门的伪代码。这肯定不是你的最终解决方案;你必须玩弄细节才能让它完全像你想要的那样。祝你好运!

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
  }

}

答案 1 :(得分:0)

这种类型的问题是逆运动学之一。我创建了一个项目,前不久就这样做了。 https://scratch.mit.edu/projects/250005153/

本质上,您想要做的是,如果组中的每个后续成员都大于某个距离,则将其移向该成员之前。例如,该距离将类似于圆的半径。这模拟了绳索或链中原子的真实生活等效条件。希望这会有所帮助!

答案 2 :(得分:0)

我还不太了解JavaScript,但是, 在Scratch中,这种事情真的很容易。

指向Sprite,移动(Sprite速度)步骤。 完成。

如果那样行不通, 转到Sprite,指向Sprite的方向,旋转180, 移动所需的距离,并针对每个链接进行此操作, 指向后面的每个并发链接 其他。 (换句话说,指向并指向行中最后一个的方向)

这可以通过多个精灵(最简单的方法),变量来完成 设置临时职位和盖章,清单和盖章,或 您可以使用克隆(有点复杂)。