[英]Move rectangle on processing?
I have the following code (I didn't write the setup and the draw here) and I want to move a rectangle on Processing. 我有以下代码(我没有在这里写设置和绘图),我想在Processing上移动一个矩形。 Left if I press 'a' and right if I press 'd'. 如果按'a'则向左,如果按'd'则向右。 With this code I do it but if moveTo is a low number, the rectangle moves too slowly and if moveTo is a high number, the rectangle moves jerkily. 使用此代码,我执行此操作,但如果moveTo为低数字,则矩形移动得太慢,如果moveTo为高数字,则矩形会急剧移动。 It's not fluid... How can I do this animation? 它不流畅......我怎么能做这个动画?
int moveTo = 5;
void keyPressed()
{
if (key == 'a')
{
xTopRect = xTopRect - moveTo;
xBottomRect = xBottomRect - moveTo;
xFstPointTriangle = xFstPointTriangle - moveTo;
xSndPointTriangle = xSndPointTriangle - moveTo;
xTrdPointTriangle = xTrdPointTriangle - moveTo;
}
else if (key == 'd')
{
xTopRect = xTopRect + moveTo;
xBottomRect = xBottomRect + moveTo;
xFstPointTriangle = xFstPointTriangle + moveTo;
xSndPointTriangle = xSndPointTriangle + moveTo;
xTrdPointTriangle = xTrdPointTriangle + moveTo;
}
}
I've found this solution and it works (don't look at the variables) I call move() in draw() 我找到了这个解决方案并且它有效(不要看变量)我在draw()中调用move()
void move()
{
if (keyPressed) {
if (key == 'a') {
x = x - speed;
} else if (key == 'd') {
x = x + speed;
}
}
}
int moveTo = 5;
int MAX_MOVE_TO=100;
int counter=0;
void keyPressed()
{
counter++;
if(counter==(MAX_MOVE_TO-moveTo))
{
if (key == 'a')
{
xTopRect = xTopRect - 1;
xBottomRect = xBottomRect - 1;
xFstPointTriangle = xFstPointTriangle - 1;
xSndPointTriangle = xSndPointTriangle - 1;
xTrdPointTriangle = xTrdPointTriangle - 1;
}
else if (key == 'd')
{
xTopRect = xTopRect + 1;
xBottomRect = xBottomRect + 1;
xFstPointTriangle = xFstPointTriangle + 1;
xSndPointTriangle = xSndPointTriangle + 1;
xTrdPointTriangle = xTrdPointTriangle + 1;
}
counter=0;
}
}
just try different values of moveTo between 0 and 99 please. 请尝试不同的moveTo值在0到99之间。 It should move smoothly at all speeds. 它应该在所有速度下平稳移动。
You are moving objects with a keypress but you can do it in a different thread so it can move when you are not pressing a key too. 您正在使用按键移动对象,但您可以在不同的线程中执行此操作,以便在您不按键时它也可以移动。
Currently, your code is using a KeyListener
, I assume, to trigger the movement. 目前,我认为您的代码使用KeyListener
来触发移动。 One thing you could do is to create and ArrayList<Integer>
to hold the values of the keys that are down. 您可以做的一件事是创建和ArrayList<Integer>
来保存关闭的键的值。 Then, in a loop, check the ArrayList
for the A and D key values, and move if they are in it. 然后,在循环中,检查ArrayList
中的A和D键值,如果它们在其中则移动。
The problem right now is that the KeyListener
only registers a KeyEvent
every do often. 现在的问题是KeyListener
每次都经常注册一个KeyEvent
。 With this solution, you will be able to control how often the rectangle's location updates. 使用此解决方案,您将能够控制矩形位置更新的频率。
ArrayList<Integer> keysDown = new ArrayList<Integer>();
void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode()) {
keysDown.add(e.getKeyCode());
}
}
void keyReleased(KeyEvent e) {
if (keysDown.contains(e.getKeyCode()) {
keysDown.remove(e.getKeyCode());
}
}
Then in your update or draw loop, 然后在您的更新或绘制循环中,
if (keysDown.contains(KeyEvent.VK_A)) {
xTopRect = xTopRect - moveTo;
xBottomRect = xBottomRect - moveTo;
xFstPointTriangle = xFstPointTriangle - moveTo;
xSndPointTriangle = xSndPointTriangle - moveTo;
xTrdPointTriangle = xTrdPointTriangle - moveTo;
} else if (keysDown.contains(KeyEvent.VK_D)) {
xTopRect = xTopRect + moveTo;
xBottomRect = xBottomRect + moveTo;
xFstPointTriangle = xFstPointTriangle + moveTo;
xSndPointTriangle = xSndPointTriangle + moveTo;
xTrdPointTriangle = xTrdPointTriangle + moveTo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.