在 Scratch 中,我试图实现这种“跟随另一个精灵”运动,其中另一个精灵完全跟随第一个精灵的运动,就像在这个游戏中一样,信仰之山:
https://www.youtube.com/watch?v=fqHYHOD2-ck
基本上,我正在寻找可以在 Scratch 和 JavaScript 中帮助我的伪代码。除了(显然)涉及另一个精灵的 X 和 Y 位置之外,我真的不确定该怎么做。
谢谢。
在 Scratch 中,我试图实现这种“跟随另一个精灵”运动,其中另一个精灵完全跟随第一个精灵的运动,就像在这个游戏中一样,信仰之山:
https://www.youtube.com/watch?v=fqHYHOD2-ck
基本上,我正在寻找可以在 Scratch 和 JavaScript 中帮助我的伪代码。除了(显然)涉及另一个精灵的 X 和 Y 位置之外,我真的不确定该怎么做。
谢谢。
这种类型的问题是逆运动学问题之一。我创建了一个项目来执行此操作。https://scratch.mit.edu/projects/250005153/
本质上,您想要做的是让组中的每个连续成员移动到它之前的成员,如果它大于一定距离。例如,这个距离就像圆的半径。这模仿了现实生活中的绳索或链中的原子。希望这可以帮助!
这是一些可以帮助您入门的伪代码。这绝对不是您的最终解决方案;你必须玩弄细节才能完全按照你想要的方式得到它。祝你好运!
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
}
}
我对 JavaScript 的了解还不够,但是,这种事情在 Scratch 中非常容易。
指向精灵,移动(精灵的速度)步骤。完毕。
如果这种方式不起作用,则转到 Sprite,指向 Sprite 的方向,转 180,移动所需的距离,并为每个链接执行此操作,并依次指向每个并发跟随链接。(换句话说,去并指向最后一行的方向)
这可以通过多个精灵(最简单的方法)、设置临时位置和标记、列表和标记的变量来完成,或者您可以使用克隆(这有点复杂)。