美好的一天。
我有以下声明和作业,并想知道我如何制作我的图像(一个有图标的JLabel - 敌人单位)移动。标签被称为敌人。我已经在下面实现了Runnable和我的代码 - switch case位于重写的Run()方法中。还请准确指出放置/运行每个方法/实例/线程的位置。我稍后会实现多个单独的线程来同时执行操作,但是我需要清除它。这是在Windows上开发的 - 用于Windows项目的Net bean - 不适用于手持设备
static Point EnemyLocation;
// This throws an error - Might it be due to putting an implementable
// overridden method in a thread?
Thread moveTheEnemy = new Thread(run());
public Window()
{
initComponents();
EnemyLocation = new Point(
(int)(Enemy.getLocation().getX() + Enemy.getWidth()/2 - 7),
(int)(Enemy.getLocation().getY() - 3 + Enemy.getHeight()/2)
);
}
// How do i go about running this run method in that thread?
// And Can i Run it on a button click? Simply indicating
// ThreadName.start in the Button wont start the actions.
@Override
public void run(){
boolean stop = false;
// A Method is to be added here to flag for when the stop
// will change... Dont worry about this part... I need
// the Cases below to work.
while (stop == false)
{
for (int i = 0; i < 1; i++)
{
Random rand = new Random();
int a = rand.nextInt(4);
switch (a) {
case 0:
EnemyLocation.setLocation(
EnemyLocation.x + 4, EnemyLocation.y);
break;
case 1:
EnemyLocation.setLocation(
EnemyLocation.x - 4, EnemyLocation.y);
break;
case 2:
EnemyLocation.setLocation(
EnemyLocation.x, EnemyLocation.y + 4);
break;
case 3:
EnemyLocation.setLocation(
EnemyLocation.x, EnemyLocation.y - 4);
break;
default:
EnemyLocation.setLocation(
EnemyLocation.y + 4, EnemyLocation.y);
//It comes down to this Exception Each time...
throw new AssertionError("Failure to attempt Move");
}
}
}
}
答案 0 :(得分:0)
e.g。 javadoc告诉您如何在线程中运行runnable(doc中的第三个示例)。您只需将Runnable赋予Thread构造函数并启动Thread即可。但是,我会建议你研究你的java基础知识并阅读一些关于游戏编程的文学,然后花很多时间在一个非感觉项目上(对不起)。 编辑:“非感觉项目”实际上似乎太冒犯了。我的意思是,如果你不理解基础知识并且你的代码/问题告诉我,你不会成功完成。但我可能错了。