简体   繁体   English

Slick2d,如何在屏幕上绘制字符串几秒钟?

[英]Slick2d, How can i draw a string on screen for a few seconds?

I've been trying to figure this out, all I want to do is be able to draw a string for longer than just a frame, but when I call it in the method I want it to flash up then disappear immediately, any advice would be appreciated :) I'm using something like this: 我一直试图弄清楚这一点,我要做的就是能够画一个比框架更长的字符串,但是当我在方法中调用它时,我希望它闪烁然后立即消失,任何建议都会不胜感激:)我正在使用这样的东西:

g.drawString("You got a Key!", 100, 100);

I'm doing this in a method which is called after an Item is picked up 我正在使用一种方法,在拾取物品后称为

public void addItemFound(Graphics g){
    ip.mainInventory[ip.getFirstEmptyStack()] = getItemStackFound();
    System.out.println(this.getItemFound() + " added");
    g.drawString("You Got a Key!", 100, 100);
}

That's the full method if you were interested :) Thanks!Also apologies for the dumb question, i'm a newbie to this :P 如果您有兴趣,那就是完整的方法:)谢谢!对于这个愚蠢的问题也表示歉意,我是这个新手:P

I believe that the best way to do this project would be to draw the scene at regular intervals eg 10 milliseconds using a Thread.sleep() . 我相信,执行此项目的最佳方法是使用Thread.sleep()定期绘制场景(例如10毫秒Thread.sleep() This way, you can simply add a variable to show the message for, say, 100 loops (1 second) like this: 这样,您可以简单地添加一个变量来显示消息,例如100个循环(1秒),如下所示:

private LinkedList<String> drawStringList= new LinkedList<>();
private LinkedList<Integer> drawStringTimeout= new LinkedList<>();
private LinkedList<Integer[]> drawStringPos= new LinkedList<>();


public void addText(String stringToWrite, int posX, int posY, int timeOut) {
    drawStringList.add(stringToWrite);
    int[] pos = new int[2];
    pos[0] = posX;
    pos[1] = posY;
    drawStringPos.add(pos);
    drawStringTimeout.add(timeOut);
}

private void mainLoop() {
...items to be drawn here...
for(int i=0;i<drawStringList.size();i++){
    g.drawString(drawStringList.get(i),drawStringPos.get(i)[0],drawStringPos.get(i)[1]);
    drawStringTimeout.set(i,drawStringTimeout.get(i)-1);
    if(drawStringTimeout.get(i)<=0) {
        drawStringList.remove(i);
        drawStringTimeout.remove(i);
        drawStringPos.remove(i);
    }
}
try { Thread.sleep(10); } catch (Exception e) {}
}

In this code, you must add the string you want to draw to drawStringList , add the number of loops you want it to stay for to drawStringTimeout and add the position you would like to draw it in to drawStringPos as an array (you could use a point if you wanted to). 在此代码,您必须添加要绘制的字符串drawStringList ,添加你希望它留到循环的次数drawStringTimeout并添加你想画它的位置drawStringPos作为数组(您可以使用点如果你想)。 I have made a method to do this. 我已经做了一个方法来做到这一点。

I don't know what Dan300 is trying to tell you to do but that's way, way, way over complicated. 我不知道是什么Dan300是想告诉你这样做,但是这是方式,方法, 方式过于复杂。 Slick2D works on gamestates: Slick2D适用于游戏状态:

http://slick.ninjacave.com/javadoc/org/newdawn/slick/state/GameState.html http://slick.ninjacave.com/javadoc/org/newdawn/slick/state/GameState.html

The gamestate has a method called render(). gamestate有一个称为render()的方法。 The render() is called every single cycle of the loop to update your screen with drawing information. 在循环的每个循环中都会调用一次render(),以使用绘图信息更新屏幕。 If you want to draw the text on the screen for a longer time you should be drawing the text somewhere within the stack space of this render() function. 如果要在屏幕上绘制文本更长的时间,则应在此render()函数的堆栈空间内的某个位置绘制文本。

What is happening now is you have a function with one specific purpose that only exists every so briefly: add an item to the player. 现在发生的事情是,您有一个具有特定目的的功能,该功能仅在很短的时间内就存在:向播放器添加一个项目。 The game comes across this statement and when adding an item within that 1 cycle the text will be drawn. 游戏遇到此声明,并且在该1个周期内添加项目时,将绘制文本。 But the next cycle when the player isn't picking up an item it won't come by that drawString statement and you won't have your string on your screen longer than 1 game cycle. 但是在下一个循环中,当玩家没有拾取物品时,该drawString语句将不会出现该循环,并且您的字符串在屏幕上的停留时间不会超过1个游戏周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM