- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道还有其他问题,但似乎没有一个能帮助我解决问题。在我的游戏中,有一个玩家发射一枚导弹,飞向负 y 直到它从屏幕上消失,然后回到原来的位置等待再次发射。然而,每次发射导弹时,它都会变得更快,就像 Timer() 中的延迟变短一样。代码:
主类:
public class ShooterGame extends JFrame{
static int playerX=500;
static int playerY=520;
InputHandler input = new InputHandler(this);
public static Player player = new Player(playerX,playerY,50,50);
public static void main(String[] args){
ShooterGame game = new ShooterGame();
game.run();
System.exit(0);
}
static int windowWidth = 1300;
static int windowHeight = 600;
static int fps = 30;
public static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
public static Graphics bbg;
public void run(){
boolean running = true;
initialize();
while(running){
long time = System.currentTimeMillis();
update();
draw();
time = (1000 / fps) - (System.currentTimeMillis() - time);
if (time > 0) {
try{
Thread.sleep(time);
}
catch(Exception e){};
};
}
}
public void initialize(){
setTitle("--- Shooter Game ---");
setSize(windowWidth, windowHeight);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void update(){
player.update(input);
}
public void draw(){
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.BLACK);
bbg.fillRect(0, 0, windowWidth, windowHeight);
player.Draw(bbg);
enemie1.Draw(bbg,10,100);
if(game.player.Player.missileRunning) game.player.Player.missile.Draw(bbg);
g.drawImage(backBuffer, 0, 0, this);
}
public static Graphics getMainGraphics(){
return bbg;
}
}
玩家类:
public class Player{
private BufferedImage sprite;
public BufferedImage missileSprite;
public int x, y, width, height;
private int fixedX;
private final double speed = 5.0d;
public static Missile missile;
public static boolean missileRunning = false;
public static boolean missileReady = true;
public Player(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
missile = new Missile(this.x);
try{
URL url = this.getClass().getResource("ship.png");
sprite = ImageIO.read(url);
} catch(IOException e){}
try{
URL url2 = this.getClass().getResource("missile.png");
missileSprite = ImageIO.read(url2);
} catch(IOException e){}
}
public void keyPlayer(double delta, InputHandler i){
if(i.isKeyDown(KeyEvent.VK_D)){
if(this.x>=1240) return;
else this.x+=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_A)){
if(this.x<=0) return;
else this.x-=speed*delta;
}
if(i.isKeyDown(KeyEvent.VK_SPACE)){
if(missileReady){
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:/Users/Gabriel/Desktop/Programacao/Other/java/ShootGame/game/player/Fire.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
missile.x=this.x + 20;
missileRunning=true;
}
}
}
public void update(InputHandler inputP){
keyPlayer(2.0, inputP);
updateMissile(game.ShooterGame.backBuffer.getGraphics());
}
public void updateMissile(Graphics g){
if(missileRunning){
missileReady=false;
missile.update(g);
}
}
public Rectangle missileBounds(){
return new Rectangle(missile.x, game.player.Missile.y, 6, 18);
}
public void Draw(Graphics a){
a.drawImage(sprite,x,y,width,height,null);
}
public Rectangle getBounds(){
return new Rectangle(x,y,width,height);
}
}
导弹等级:
public class Missile{
public BufferedImage sprite;
public static int x;
public static int y=504;
private int interval = 2000;
private Timer timer2;
private boolean isAlive = game.player.Player.missileRunning;
static final AtomicInteger count = new AtomicInteger();
public static boolean timerReady;
public Missile(int x){
this.x=x;
this.y=504;
this.sprite=sprite;
try{
URL url = this.getClass().getResource("missile.png");
sprite = ImageIO.read(url);
} catch(IOException e){System.out.println("Error loading image");}
}
public void Draw(Graphics g){
g.drawImage(sprite,x,this.y,6,18,null);
}
public void update(Graphics g){ //The problem
if(game.player.Player.missileRunning==true){
timerReady=true;
if(checkTimer()){
timer2 = new Timer();
timer2.schedule(new Move(), 0, interval);
}
this.y = y;
if(y <= 0){
game.player.Player.missileRunning=false;
timerReady=false;
y=504;
if(!checkTimer()){
timer2.cancel();
timer2.purge();
}
timer2=null;
reload();
}
}
}
public boolean checkTimer(){
if(timerReady){
return true;
} else {
return false;
}
}
class Move extends TimerTask{
public void run(){
int keeper = 3;
if(keeper>0) y-=interval/1000;
}
}
public synchronized void reload(){
Timer missileBetween = new Timer();
missileBetween.cancel();
missileBetween = new Timer();
TimerTask readyMissile = new TimerTask(){
public void run(){
game.player.Player.missileReady=true;
}
};
missileBetween.schedule(readyMissile, 20);
}
public static int getNumber(){
return count.get();
}
public static AtomicInteger getAtomic(){
return count;
}
}
(我不会发布我的所有程序,只发布对问题重要的部分)(如有遗漏,请指出)
谢谢
最佳答案
不运行代码...
timerReady=true;
if(checkTimer()){
timer2 = new Timer();
timer2.schedule(new Move(), 0, interval);
}
基本上意味着 checkTimer
将始终返回 true
,这意味着每次调用 update
时,您都在创建另一个 Timer
,这大概是在创建几十个定时器,都是独立更新游戏状态...这会导致对象加速...
这种逻辑和功能应该由主循环控制。在每个循环中,您应该检查对象的状态并决定移动或删除它,这里不应该涉及其他“计时器”或“循环”......
哦,学会在没有static
的情况下做事,它会给你带来更多的问题,然后它会在这种情况下解决。
还有……
Timer missileBetween = new Timer();
missileBetween.cancel();
missileBetween = new Timer();
...毫无意义,您创建一个本地 Timer
,取消它,即使它实际上并未运行,创建一个新的本地实例并安排一些任务...
你失去了对本地版本的引用,无法再修改...
关于java - 定时器在 Java 中加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914108/
哪种定时器在性能方面更适合使用? Jquery 计时器或 Javascript 计时器。 具有计时器的页面没有任何 Jquery 代码。 谢谢 最佳答案 在仅使用计时器的页面上引用完整的 JQuery
R 语言有没有简单的方法来设置定时器功能?计时器函数是指位于 session 后台并每隔一段时间执行一次的函数。 干杯! 最佳答案 tcltk2 包中有 tclTaskSchedule 函数(和其
我想在点击发生后调用 setTimeout()。 如果用户在 300ms 过去之前再次点击,我想停止那个计时器,触发另一个函数并重新启动原来的计时器。 我知道 setTimeout() 但我不确定如何
请参阅下面的代码...它会在页面加载 + 8 秒后更改图像,然后继续每 1 秒更改一次。 setInterval(function(){ setTimeout(function(){
我正在尝试使用计时器来安排应用程序中的重复事件。但是,我希望能够实时调整事件触发的时间段(根据用户输入)。 例如: public class HelperTimer extends TimerTask
setTimeout()--用于指定在一段特定的时间后执行某段程序。 格式: [定时
setTimeout 和 clearTimeout 复制代码 代码如下: var obj = setTimeout(cb, ms); setTim
if(e.getSource()==continuous) { TimerTask task = new TimerTask() { public void run()
请谁能告诉我如何在 iPhone 的 cocos2d 中实现启动游戏的倒计时器。 我的意思是,按下“播放”时,一个新场景会出现,显示数字“3”、“2”、“1”,然后显示“GO!”一词。 最佳答案 来自
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
这个问题在这里已经有了答案: Lua Program Delay (2 个答案) 关闭 7 年前。 我目前使用 Corona SDK,Lua 作为我的主要语言。我在使用此代码时遇到问题 - 当我运行
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
到目前为止,我使用的每种方法都只是暂时卡住我的程序,但我希望游戏继续运行,我只希望盾牌 boolean 值在 X 时间内为 true,然后在时间到期后返回 false,有吗有办法做到这一点吗?谢谢。
我需要创建一个异步线程,它运行一次,延迟 2 分钟,并且可以随时终止。我看到了几种可能的解决方案: ScheduledExecutorService 和 FutureTask 允许我中断正在运行的任务
我开发了一个简单的应用程序并使用了计时器,但如果我多次运行计时器,计时器会丢弃此异常:线程“AWT-EventQueue-0”java.lang.IllegalStateException 中的异常:
我正在实现一个计时器: timer = new Timer(); timer.schedule(new TimerTask() { @Overr
我有一个有点复杂的 iOS 用户界面,我需要每秒重新加载 UICollectionView 的特定单元格以显示时间(有点像复杂的秒表),我还需要每秒做一些其他事情在这次通话中。 问题的第 1 部分 我
我一直在研究可用于 QueryPerformanceCounter()/QueryPerformanceFrequency() 的不同类型的计时器,在进一步研究之后,我发现了一个使用计时器类的例子..
我正在尝试以微秒为单位做一个计时器,但它不太管用。 #include #include #include using namespace std; int main () { struc
假设我有一个整数数组 int timeouts [] = {1000 , 2000 , 3000 , 3500}; 我想创建一个计时器,最多计时 3.5 秒,并在毫秒计数等于数组元素之一时调用相同的函
我是一名优秀的程序员,十分优秀!