- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究一种将 LWJGL 用于我的输入系统的方法。我在检测它是单击还是按住时遇到问题。当我点击时,该事件会触发两次,而不是一次。
while(Keyboard.next())
{
if(Keyboard.getEventKeyState())
{
if(Keyboard.isRepeatEvent())
{
//Key held.
doAction(Keyboard.getEventKey(), true, false);
}
else
{
//Key pressed
doAction(Keyboard.getEventKey(), false, false);
}
}
else
{
//Fired when key is released.
doAction(Keyboard.getEventKey(), false, true);
}
}
编辑:我已经解决了这个问题并对其进行了修改。给你,修改版。 (该死的,Teamviewer..)
/**
* Updates all mouse info, keys bound, and performs actions.
*/
public static void tick()
{
mouseButtons[0] = Mouse.isButtonDown(0);
mouseButtons[1] = Mouse.isButtonDown(1);
mousePos[0] = Mouse.getX();
mousePos[1] = Mouse.getY();
while(Keyboard.next())
{
doAction(0, false);
if(Keyboard.getEventKeyState())
{
if(!Keyboard.isRepeatEvent())
{
doAction(Keyboard.getEventKey(), false);
}
}
else
{
doAction(Keyboard.getEventKey(), true);
}
}
while(Mouse.next())
{
}
}
/**
* Does the associated action for each key. Called automatically from tick.
* @param key The key to check & perform associated action
*/
public static void doAction(int key, boolean ifReleased)
{
if(mouseButtons[0])
{
}
if(mouseButtons[1])
{
}
if(key == 2 & !ifReleased)
{
System.out.println("a");
}
if(Keyboard.isKeyDown(3))
{
System.out.println("b");
}
}
最佳答案
我知道自问这个问题以来已经有一段时间了,但我自己想出了一个解决方案。我的 InputHelper
让您可以确定键或鼠标按钮是按下、释放还是按住,并且可以从任何其他类访问而无需初始化和共享它的相同实例。
它的工作原理是有 2 个数组,1 个用于鼠标事件,1 个用于键盘事件,每个数组为每个键存储一个枚举值。如果有按钮或按键事件,更新时,更新函数会将该按钮/键的适当数组中的值设置为某个枚举。然后,在下一次更新时,它将所有按键和按钮事件设置为无事件,并重复该过程,处理任何新事件。
/*
* Handles mouse and keyboard input and stores values for keys
* down, released, or pressed, that can be accessed from anywhere.
*
* To update the input helper, add this line into the main draw loop:
* InputHelper.update();
*
* Use as so (can be used from anywhere):
* InputHelper.isKeyDown(Keyboard.KEY_SPACE);
*/
import java.util.ArrayList;
import org.lwjgl.input.*;
/**
*
* @author Jocopa3
*/
public class InputHelper {
private static InputHelper input = new InputHelper(); //Singleton class instance
private enum EventState {
NONE,PRESSED,DOWN,RELEASED;
}
private ArrayList<EventState> mouseEvents;
private ArrayList<EventState> keyboardEvents;
public InputHelper(){
//Mouse initialization
mouseEvents = new ArrayList<EventState>();
//Add mouse events to Array list
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.add(EventState.NONE);
}
//Keyboard initialization
keyboardEvents = new ArrayList<EventState>();
//Add keyboard events to Array list
for(int i = 0; i < Keyboard.KEYBOARD_SIZE; i++) {
keyboardEvents.add(EventState.NONE);
}
}
private void Update(){
resetKeys(); //clear Keyboard events
//Set Key down events (more accurate than using repeat-event method)
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++){
if(Keyboard.isKeyDown(i))
keyboardEvents.set(i, EventState.DOWN);
}
while(Keyboard.next()){ //Handle all Keyboard events
int key = Keyboard.getEventKey();
if(key<0) continue; //Ignore no events
if(Keyboard.getEventKeyState()){
if(!Keyboard.isRepeatEvent()){
keyboardEvents.set(key, EventState.PRESSED);
}
}else{
keyboardEvents.set(key, EventState.RELEASED);
}
}
resetMouse(); //clear Mouse events
//Set Mouse down events
for(int i = 0; i < Mouse.getButtonCount(); i++){
if(Mouse.isButtonDown(i))
mouseEvents.set(i, EventState.DOWN);
}
while (Mouse.next()){ //Handle all Mouse events
int button = Mouse.getEventButton();
if(button<0) continue; //Ignore no events
if (Mouse.getEventButtonState()) {
mouseEvents.set(button, EventState.PRESSED);
}else {
mouseEvents.set(button, EventState.RELEASED);
}
}
}
//Set all Keyboard events to false
private void resetKeys(){
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++) {
keyboardEvents.set(i, EventState.NONE);
}
}
//Set all Mouse events to false
private void resetMouse(){
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.set(i, EventState.NONE);
}
}
//Non-static version of methods (Only used in the singleton instance)
private boolean KeyDown(int key){
return keyboardEvents.get(key)==EventState.DOWN;
}
private boolean KeyPressed(int key){
return keyboardEvents.get(key)==EventState.PRESSED;
}
private boolean KeyReleased(int key){
return keyboardEvents.get(key)==EventState.RELEASED;
}
private boolean MouseButtonDown(int key){
return mouseEvents.get(key)==EventState.DOWN;
}
private boolean MouseButtonPressed(int key){
return mouseEvents.get(key)==EventState.PRESSED;
}
private boolean MouseButtonReleased(int key){
return mouseEvents.get(key)==EventState.RELEASED;
}
//Static version of methods (called from anywhere, return singleton instance value)
public static boolean isKeyDown(int key){
return input.KeyDown(key);
}
public static boolean isKeyPressed(int key){
return input.KeyPressed(key);
}
public static boolean isKeyReleased(int key){
return input.KeyReleased(key);
}
public static boolean isButtonDown(int key){
return input.MouseButtonDown(key);
}
public static boolean isButtonPressed(int key){
return input.MouseButtonPressed(key);
}
public static boolean isButtonReleased(int key){
return input.MouseButtonReleased(key);
}
public static void update(){
input.Update();
}
}
它必须手动更新每一帧,所以你的主绘制循环你应该添加行 InputHelper.update();
像这样:
while(!Display.isCloseRequested()) {
InputHelper.update(); //Should go before other code that uses the inputs
//Rest of code here
}
一旦设置为更新每一帧,您就可以在需要确定鼠标或按键按钮的输入状态的任何地方使用它,如下所示:
//Mouse test
if(InputHelper.isButtonPressed(0))
System.out.println("Left Mouse button pressed");
if(InputHelper.isButtonDown(0))
System.out.println("Left Mouse button down");
if(InputHelper.isButtonReleased(0))
System.out.println("Left Mouse button released");
//Keyboard Test
if(InputHelper.isKeyPressed(Keyboard.KEY_SPACE))
System.out.println("Space key pressed");
if(InputHelper.isKeyDown(Keyboard.KEY_SPACE))
System.out.println("Space key down");
if(InputHelper.isKeyReleased(Keyboard.KEY_SPACE))
System.out.println("Space key released");
关于java - LWJGL 的单击和按住问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053847/
如何以编程方式创建最大化的 lwjgl 窗口或使已创建的显示最大化? 注意:我不是问如何为显示器设置全屏模式。 最佳答案 Display.setResizable(true) 这将启用最大化按钮。 关
我对使用外部 Java 库非常陌生,我从基于 LWJGL 构建的 Slick2D 开始。我编写了一个简单的 StateBasedGame,它只在屏幕上打印三个选项。它给了我一个关于某些(jar?)文件
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr ary.path at
我正在 Itellij 中制作一个依赖 org.lwjgl.input 代码的 Minecraft mod。该库已从 LWJGL 3 中删除,因此我必须使用 LWJGL 2。我将正确的 LWJGL 2
我安装了 lwjgl 库和 natives 并将其放入/lib 文件夹。在 Eclipse Java 构建路径上设置库位置和 native 库位置。当我尝试在 Eclipse 上运行它时,它可以工作,
所以我已经使用 LWJGL 开发一个程序很长一段时间了,现在我希望能够轻松地向其他人展示它。但是,当我尝试将其导出为可运行的 jar 文件并在控制台中运行时,出现上述错误。 我还尝试将其导出为普通的
我尝试使用 LWJGL 编译 ( NetBeans ) Java 项目。我单击了项目 -> 属性,然后单击库 -> 编译。我添加了 Jars 位置、源文件位置和 javadoc 位置。 当我尝试构建项
在 NEHE 第 06 课 (http://nehe.gamedev.net/tutorial/texture_mapping/12038/) 中,您可以加载图像并用它来纹理对象。我目前使用的是LWJ
我正在尝试向我的程序添加全屏功能,但我无法让它工作。我想 Display.setFullscreen(true); 我尝试将其位置更改为创建显示的位置或设置显示模式的位置上方,但仍然无法正常工作。有什
我刚刚开始使用 LWJGL 和 OpenGL,但遇到了问题。我正在尝试弄清楚如何使用我找到的游戏引擎为我正在制作的游戏绘制一条简单的线条。 这是我正在使用的引擎的链接:https://github.c
我正在尝试在 java lwjgl 2.9 中编译着色器,如果我运行我的游戏,它无法编译着色器。问题是为什么着色器不重新识别 const 关键字,如果我删除 const 那么它会抛出错误 float
我在使用 Java 和 lwjgl 导入时遇到问题: import org.lwjgl.input.Keyboard; 行不通。我已经添加了 jar : lwjgl-glfw.jar lwjgl-op
我在 OpenGL 上下文中移动实体时遇到问题:当我尝试放置一个实体时,位置似乎是正确的,但是当实体开始移动时,一切都会出错,并且碰撞不起作用。我是 OpenGL 新手,我怀疑我的世界矩阵或模型矩阵是
我正在做一项家庭作业,使用 LightWeight Java 游戏库修改教授给出的代码。问题是我无法完全加载测试代码来开始测试修改。我已经按照修改中的说明链接了 jar 文件,但仍然存在一个挥之不去的
我是 Java 新手,我想将基本的 LWJGL 小程序上传到我的网站并在浏览器中播放。我不知道该怎么做,我想确保在继续开发游戏之前我知道如何让它工作。请帮助我,因为我不知道如何将 JWJGL 小程序或
我正在尝试旋转屏幕上的两个对象之一。我该怎么做?我只能让它旋转屏幕上的所有对象。代码: glColor3f(1, 0, 0); glBegin(GL_QUADS
我目前正在渲染一个天空盒,如下所示: //Front Face GL11.glBindTexture(GL11.GL_TEXTURE_2D, skybox[0].getText
我正在编写类似 Minecraft 的游戏。为了加载纹理和字体(目前没有声音),我使用 slick。 Slick 不再开发,maven 存储库不可用。是否有其他库(LibGDX 除外)可用于加载 lw
我正在尝试使用opengl在lwjgl显示屏上显示具有自定义字体的文本。目前,我正在使用自定义位图字体类从png文件加载字体,并以与tileet相同的方式显示它们。它可以正常工作,但是当我将文本绘制到
我正在使用 VBO 来渲染游戏中的所有立方体(因为会有很多立方体),并且我希望它们都带有纹理。当我运行我的代码时,立方体没有纹理(相反,它看起来有点红色,因为我的纹理是砖墙),并且只有当我非常接近立方
我是一名优秀的程序员,十分优秀!