java游戏移动_java - Java游戏 - 如何让敌人移动? - SO中文参考 - www.soinside.com

Java游戏 - 如何让敌人移动?

问题描述 投票:1回答:2

我正在做一个射击游戏,并添加了许多带阵列的敌人,然后在地图上给他们一个随机位置,但我不知道如何让他们在到达他们的位置后移动。这是我的敌人类:

import com.badlogic.gdx.math.Vector2;

import java.util.Random;

public class Enemy {

private static final Random r = new Random();

int x = r.nextInt(36);

int y = r.nextInt(24);

Vector2 vect = new Vector2(x,y);

float ROTATION_SPEED = 500;

public Follower(float SPEED, float rotation, float width, float height,

Vector2 position) {

super(SPEED, rotation, width, height, position);

}

public void advance(float delta, Ship ship) {

if(rotation > 360)

rotation -= 360;

position.lerp(vect, delta);

rotation += delta * ROTATION_SPEED;

super.update(ship);

//Edited: i forget to put this lines:

if(vect.equals(this.getPosition())){

x = r.nextInt(36);

y = r.nextInt(24);

}

}

我应该在这个类中实现什么样的方法来使它们在一定时间后移动x / y值?

java

game-physics

2个回答

1

投票

当你在没有多线程的情况下使用Thread.sleep时,整个游戏都会冻结。但你也可以使用Timer和TimerTask来解决它,特别是初学者(你可以在之前将它添加到你的代码中):

import java.util.Timer;

import java.util.TimerTask;

public class Enemy{

Timer t;

public Enemy(){ //in your constructor

t = new Timer();

t.scheduleAtFixedRate(new TimerTask(){

public void run(){

/*here the code for the movement, e.g:

x += 5;

y += 5;*/

}

}, delay, speed); //delay is after which time it should start usually delay is 0, speed is the time in ms e.g. 9

}

}

0

投票

Thread.sleep是在进行进一步处理之前休眠一段时间的方法。您将很好地开始研究Java中的多线程以轻松解决此类问题。你可以从这里开始:http://docs.oracle.com/javase/tutorial/essential/concurrency/

要立即解决,只需在while循环中编写Thread.sleep()。

热门问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值