简体   繁体   English

鼠标事件加剧问题

[英]Aggravating Issue with Mouse Event

For my computer science class, I was asked to create a game. 在我的计算机科学课上,我被要求创建一个游戏。 Everything was going smoothly until I came upon the issue of mouse input. 一切进展顺利,直到我遇到了鼠标输入的问题。 What I am trying to accomplish is to click a mouse at a certain position in the JFrame, then have a bullet initialize at my character's spot and shoot towards the point of the mouse. 我要完成的工作是在JFrame中的某个位置单击鼠标,然后在我的角色的位置初始化一个项目符号,并朝着鼠标的位置射击。 I already understand how to use trig in order to get the bullet to shoot at that angle. 我已经了解了如何使用Trig来使子弹以该角度射击。 The problem is that when I shoot a bullet after clicking the mouse, the x and y positions of my character are not updated to the bullet, meaning that the bullet always initializes in the same place. 问题是,当我单击鼠标后发射项目符号时,角色的x和y位置不会更新为项目符号,这意味着项目符号始终在同一位置初始化。 Here are code segments in which I believe are causing the problem. 我认为这是导致问题的代码段。

public void mousePressed(MouseEvent e)
{       
    handler.addObject(new Bullet("res\\Fireball.png",x,y + ,ID.BasicEnemy,handler));
}

public void mouseReleased(MouseEvent e)
{
}


public void tick() 
{

    x+=velX;
    y+=velY;

    x = Game.clamp(x,0,Game.WIDTH-40);
    y = Game.clamp(y,0,Game.HEIGHT-40);

    collision();


}

From what I gathered through research, the reason why the x and y in mousePressed() method is not updating is because the mouse event is in a different thread than my tick. 根据我的研究,mousePressed()方法中的x和y不更新的原因是,鼠标事件与我的刻度不在同一线程中。 I'm a little new to java game programming and I was wondering if there would be anyone out there who could give me explicit suggestions. 我是Java游戏编程的新手,我想知道是否有人可以给我明确的建议。 The actual code of my game is very long, so I narrowed it down to the piece of code above.(I tried synchronized reserved word and volatile variables, but I might have implemented them wrong) 游戏的实际代码很长,因此我将其范围缩小到上面的代码部分(我尝试了同步保留字和易失性变量,但可能将它们实现错误)

And if this helps, conversely, if I do this below, the bullet object never appears! 相反,如果这有帮助,那么如果我在下面进行此操作,则子弹对象将永远不会出现!

public void mousePressed(MouseEvent e)
{       
    press=true;
}

public void mouseReleased(MouseEvent e)
{
    press=false;
}


public void tick() 
{

    x+=velX;
    y+=velY;

    x = Game.clamp(x,0,Game.WIDTH-40);
    y = Game.clamp(y,0,Game.HEIGHT-40);


    collision();

    if (press)
        handler.addObject(new Bullet("res\\Fireball.png",(int)x,(int)y ,ID.BasicEnemy,handler));
}

Here: 这里:

public void mousePressed(MouseEvent e) {       
    handler.addObject(new Bullet("res\\Fireball.png",x,y + ,ID.BasicEnemy,handler));
}

You never use the MouseEvent Point object, but seem to use an unchanging x and y value. 您从不使用MouseEvent Point对象,但似乎使用不变的x和y值。 Perhaps (hard to say) you want to use the x and y from the MouseEvent object: 也许(很难说)您想使用MouseEvent对象中的x和y:

public void mousePressed(MouseEvent e) {       
    handler.addObject(new Bullet("res\\Fireball.png", e.getX(), e.getY(), 
        ID.BasicEnemy, handler));
}

If this does not solve your problem, then consider creating and posting a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. 如果这不能解决您的问题,请考虑创建并发布一个最小,完整和可验证的示例程序 ,在该程序中 ,您将代码压缩为仍可编译和运行的最小位,没有外部依赖项(例如,需要链接到数据库)或图片),没有与您的问题无关的额外代码,但仍然可以演示您的问题。

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

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