[英]How to make an object move from one point to another via coordinates?
I currently have a 2d game in which I am trying to move a .png image following certain coordinates. 我目前有一个2D游戏,我试图按照某些坐标移动.png图像。 If I where to place this object at (0,0) and then try to move to (500,500), I could do the following:
如果我将此对象放在(0,0)然后尝试移动到(500,500),我可以执行以下操作:
public void move() {
if (x < 500) {
x += velocityX;
}
if (y < 500) {
y += velocityY;
}
}
This would work, but only because its a perfectly diagonal line. 这可行,但仅仅因为它是完美的对角线。 With that system, I can only move the object horizontally, vertically, or diagonally, but never in between.
使用该系统,我只能水平,垂直或对角移动对象,但从不介于两者之间。 If I were to try something like:
如果我尝试这样的事情:
public void move() {
if (x < 500) {
x += velocityX;
}
if (y < 400) {
y += velocityY;
}
}
The object would just move in a perfect diagonal angle until it reached 400 on the y axis, it would move horizontally for the last bit to reach 500 on the x axis. 物体将以完美的对角线移动,直到在y轴上达到400,它将水平移动,最后一个位在x轴上达到500。 How would I make it so that I could move the object to any coordinates while following a straight path?
我如何制作它以便我可以在沿着直线路径移动物体到任何坐标?
To move perfect diagonally in your window, you must make sure that the ratio of velocityX
and velocityY` should be like this, 要在窗口中对角移动完美,必须确保
velocityX
和velocityY`的比率应该是这样的,
velocityX width
----------- = -----------
velocityY height
in Your case if width
is 500 , height
is 400 and if velocityX
is 5, then 在你的情况下,如果
width
是500, height
是400,如果velocityX
是5,那么
velocityY should be 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.