我正在开发太空船游戏。
我有一个EnemyFactory类用这个方法创建敌人:
public void createPattern(String patternJson, final String enemyJson, final Vector2 position) {
System.out.println(TAG + "- Entering pattern creation..");
Json json = new Json();
final ObjectMap objectMap = json.fromJson(ObjectMap.class, Gdx.files.internal(patternJson));
new Thread(new Runnable() {
@Override
public void run() {
float count = Float.parseFloat(objectMap.get("count").toString());
float interval = Float.parseFloat(objectMap.get("interval").toString());
float intervalDelta = 0;
System.out.println(TAG + "- Running thread..");
for(int i = 0; i < count; i++) {
do{
intervalDelta += Gdx.graphics.getDeltaTime();
System.out.println(TAG + "- interval => " + intervalDelta);
}while(intervalDelta < interval);
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
System.out.println(TAG + "- creating enemy ship..");
createEnemy(enemyJson, position);
}
});
intervalDelta = 0;
}
}
}).start();
}
这个想法是在一个时间间隔(0.5秒)内创建5个敌人,但是当我运行应用程序时,它会运行for循环5次,然后创建5个敌人但没有时间间隔。
有什么建议吗?
编辑:
我做了另一个版本的功能:
public void createPattern(String patternJson, final String enemyJson, final Vector2 position) {
System.out.println(TAG + "- Entering pattern creation..");
Json json = new Json();
final ObjectMap objectMap = json.fromJson(ObjectMap.class, Gdx.files.internal(patternJson));
float count = Float.parseFloat(objectMap.get("count").toString());
float interval = Float.parseFloat(objectMap.get("interval").toString());
float intervalDelta = 0;
Timer timer = Timer.instance();
timer.scheduleTask(new Timer.Task() {
@Override
public void run() {
createEnemy(enemyJson, position);
}
},0,interval,(int) count);
}
这就是发生的事情,仍然不是理想的行为:
答案 0 :(得分:1)
这是您的任务代码。
注意 TimeUnit 类,对延迟任务非常有用。
public class ScheduledExecution {
public void createEnemyByPattern() {
EnemyActivator factory = new EnemyActivator(5, 500);
new Thread(factory).start();
}
}
class EnemyActivator implements Runnable {
private final int enemyCount;
private final long creationDelay;
EnemyActivator(int enemyCount, long creationDelay) {
this.enemyCount = enemyCount;
this.creationDelay = creationDelay;
}
@Override
public void run() {
for(int i=0; i<enemyCount; ++i) {
activateEnemy();
try {
TimeUnit.MILLISECONDS.sleep(creationDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void activateEnemy() {
// create and send enemy code here
}
}
答案 1 :(得分:0)
从您提供的代码中无法分辨出错误 - 它可能是代码之外的内容。但是,遵循良好的游戏编程习惯,我建议您使用以下内容:
public class MyGameClass <extends Game|implements ApplicationListener|whatever> {
// ...
float totalDelta=0;
float mechanicsDelta= // your interval here
float shipCount=0, totalShips= // count goes here
public void render(){
totalDelta+=Gdx.graphics.getDeltaTime();
int mechanicsFrames=0;
while(totalDelta>mechanicsDelta){
if(shipCount<totalShips){
// spawn ship
shipCount++;
}
// more game mechanics here
if(mechanicsFrames>4){
System.out.println("Warning: FPS is too low! Skipping frames...");
// on a normal system this shouldn't happen
// but in case it does we can't have your game freeze
// insert some graceful error out here
break;
}
mechanicsFrames++;
}
// render here
}
// ...
}
同时考虑将Pool<...>
用于敌舰,因为实例化/垃圾收集对象很慢。