我正在尝试检测鼠标悬停图像的时间(播放图像),当我点击鼠标时会发生一些事情。
我在屏幕中央画了一个图像,我不知道如何在我自己的代码上实现它:
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
f = (float) (0.1 * Main_Activity.apg.getHeight() / 180);
play = new Image("res/play_image.png");
play_Original_Width = play.getWidth();
play_Original_Height = play.getHeight();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = screenSize.getWidth();
height = screenSize.getHeight();
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g) throws SlickException {
play.draw(Main_Activity.apg.getWidth() / 2 - ((play_Original_Width * f) / 2),
Main_Activity.apg.getHeight() / 2 - ((play_Original_Height * f) / 2), f);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException {
int x = Mouse.getX();
int y = Mouse.getY();
}
apg:
apg = new AppGameContainer(new Main_Activity(game_Name));
apg.setDisplayMode((int) width, (int) height, false);
apg.setShowFPS(false);
apg.start();
如何设法检测鼠标悬停图像的时间(播放图像)?
先谢谢你。
答案 0 :(得分:0)
您只需要测试您的x值是否在Image X和Image X +图像宽度之间,y值是否相同。
在您的更新方法中:
int x = Mouse.getX();
int y = Mouse.getY();
int playX = Main_Activity.apg.getWidth() / 2 - ((play_Original_Width * f) / 2);
int playY = Main_Activity.apg.getHeight() / 2 - ((play_Original_Height * f) / 2);
if (x > playX && x < playX+play_Original_Width && y > playY && y < playY + play_Original_Height) {
// then your mouse coordinates are over your image.
}
附加(如果对您有用,请使用):
在某些个人代码中,我曾经实现了一个继承自Rectangle并包含Image的ImageRectangle。有了这种行为,我可以使用方法intersects
来检测这个。我更喜欢使用mouseListener来管理这些鼠标事件。
public ImageRectangle extends Rectangle {
Image play;
public ImageRectangle(Image,x,y,f){...}
public void init(...){...}
public void render(...){this.image.draw();}
public void update(...){...}
}
使用mouseMoved方法:
Rectangle mousePosition = new Rectangle(x,y,1,1);
if(mousePosition.intersects(playImageRectangle)) {//do whatever you need}