【发布时间】:2013-11-30 23:57:47
【问题描述】:
我希望当它到达 y pos = 170 时让对象向上滚动。然后当它到达 51 时它会再次向下滚动。这是我的代码..
import java.awt.*;
import java.applet.*;
public class ani1 extends Applet implements Runnable{
Thread run01;
int spacex = 51,spacey = 91;
int score = 0;
public void start() {
if (run01 == null){
run01 = new Thread(this);
run01.start();
}
}
public void stop() {
if(run01 != null){
run01 = null;
}
}
public void run() {
while(true){
repaint();
try {
Thread.sleep(30);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g){
g.setColor(Color.gray);
g.fillRoundRect(35, 70, 250, 300, 10, 250);
g.fillRect(230,20,50,50);
g.setColor(Color.white);
g.fillRoundRect(45,85,230,100,10,250);
g.setColor(Color.black);
g.drawRect(50, 90, 150, 90);
g.drawString("SPACE-X",215,100);
g.drawString("Level: 1",210,120);
g.drawString("Score : "+ score, 210,135);
g.drawString("Life : - - -",210,150);
g.fillRect(spacex, spacey, 10, 10);
spacey++;
if(spacey >170){
spacey--;
}
}
}
【问题讨论】:
-
创建一个可以控制对象位置的模型。绘制时,使用此模型绘制对象的位置...
-
我只想让它成为一个循环。我实际上并没有以此为基础制作游戏。只是一个简单的循环动画。
-
问题是当它到达 169 或 170 时它会停在那里并且不再向上滚动。
-
我没有看到任何移动代码?
-
当我运行确切的代码时,fillrect 会向下滚动。
标签: java animation applet awt thread-sleep