JavaScript将对象从起始位置移动到结束位置



我正在网上学习课程,如果我卡住了,我找不到帮助.....我使用括号和p5.js这些是我的指示:

  • 通过创建x和y属性初始化到您的位置来编辑聚光灯对象。还有endX和endY属性初始化为Minsky的位置之一。

  • 分配其他2个聚光灯并创建所需的属性。

  • 通过调整x和y属性的增量,使聚光灯完美地从你移动到Minskys。如果一切都正确,那么它将在目标上方停止。

  • 使用

    调整x和y属性
  • "+ =";或"+">
  • "- =";或";产生绯闻;

/*(明斯基兄弟是我需要聚光灯的目标,"你的位置";是起始位置)

我将复制粘贴我的代码和消息,当我提交:

// other variables, you don't need to change these
var img, spotlight_image;
var spotlight1;
var spotlight2;
var spotlight3;

function preload()
{
img = loadImage('scene.png');

spotlight_image = loadImage('spotlight.png')
}
function setup()
{
createCanvas(img.width, img.height);
//complete the initialisation of the first spotlight
//with properties x, y, endX and endY
spotlight1 = {
image: spotlight_image

x: 164,
y: 810,
endX: 780,
endY: 640,
}
//Initialize the second and third spotlights
spotlight2 = {
image: spotlight_image           
x: 164,
y: 810,
endX: 480,
endY: 474,

}
spotlight3 = { 
image: spotlight_image
x: 164,
y: 810,
endX:766,
endY: 290,
}
}
function draw()
{
image(img, 0, 0);
// alter the properties x and y of the objects below to animate the spotlights

spotlight.x += 1; 
spotlight.y += 1;

////////// DO NOT CHANGE ANYTHING BELOW /////////////
var spotlights = [spotlight1, spotlight2, spotlight3];
var spotlightSize = 300;
blendMode(BLEND);
background(30);
for (var i = 0; i < spotlights.length; i++)
{
var spotlight = spotlights[i];
//stop the spotlight if it's near enough to endx and endy
if(spotlight)
{
//stop the spotlight if it goes off of the screen
spotlight.x = min(spotlight.x, 960);
spotlight.y = min(spotlight.y, 945);
spotlight.x = max(spotlight.x, 0);
spotlight.y = max(spotlight.y, 0);
if (abs(spotlight.endX - spotlight.x) < 50
&& abs(spotlight.endY - spotlight.y) < 50)
{
spotlight.x = spotlight.endX;
spotlight.y = spotlight.endY;
}

image(spotlight.image, spotlight.x-spotlightSize/2,
spotlight.y-spotlightSize/2, spotlightSize, spotlightSize);
}
}
blendMode(DARKEST);
image(img, 0, 0);
////////// DONOT CHANGE ANYTHING ABOVE /////////////
}

提交时收到的消息:

编译错误SyntaxError: Unexpected identifier

引用

这很容易做到。如果我理解正确的话,你想要在一定的时间内将一个物体移动到一个点。你所要做的就是得到两个X坐标和两个Y坐标之间的距离,除以你希望它移动的帧数,然后每帧更新它的位置,直到它到达目的地。

最新更新