简体   繁体   English

如何通过坐标使对象从一个点移动到另一个点?

[英]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.

相关问题 如何将物体从一个点以固定角度均匀地移动到另一点? - How to move an object uniformly from one point to another at a fixed angle? 尝试将对象从固定的起点移动到鼠标单击的坐标 - Trying to move an object from a fixed starting point to the coordinates of a mouse click 如何将对象形状从一个位置移动到另一位置? - How would I move an object shape from one location to another? 如何将引用从一个对象移动到另一个对象? - How can I move a reference from one object to another? 如何从扩展线程的类开始在JPanel中创建动画(从一个点移动一个圆圈到另一个点)? - How do I start an animation (move a circle from one point to another) in a JPanel from a class that extends thread? 将物体从一个点移动到另一个点 - Moving an object from one point to another 将对象从一个数组列表移动到另一个数组列表 - move an object from one array list to another Java将播放器从一个点平滑移动到另一个点 - Java move player smoothly from one point to another 将鼠标从一个点移动到另一个点的算法 - Algorithm to move mouse from one point to another in a straight line 如何使用Android Canvas将图像从一个点移动到另一个点 - How can I move an image from one point to another using Android Canvas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM