在Android/Java上真的很受欢迎。触按精灵按钮上的事件以执行特定操作
在过去的一天左右,我真的被困在这个问题上,我真的可以利用别人的意见来判断我的错误 所以我有一个“玩游戏”的图像,我将它加载到我的游戏中,如下所示:在Android/Java上真的很受欢迎。触按精灵按钮上的事件以执行特定操作,java,android,2d,Java,Android,2d,在过去的一天左右,我真的被困在这个问题上,我真的可以利用别人的意见来判断我的错误 所以我有一个“玩游戏”的图像,我将它加载到我的游戏中,如下所示: playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this); SimpleControl对象如下所示: public SimpleControl(float x, float y, float width, float height,
playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this);
SimpleControl对象如下所示:
public SimpleControl(float x, float y, float width, float height,
String bitmapName, GameScreen gameScreen) {
super(x, y, width, height, gameScreen.getGame().getAssetManager()
.getBitmap(bitmapName), gameScreen);
public boolean isActivated() {
// Consider any touch events occurring in this update
Input input = mGameScreen.getGame().getInput();
// Check if any of the touch events were on this control
BoundingBox bound = getBound();
for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
if (input.existsTouch(idx)) {
if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
return true; }}}
return false; }
Input input = mGame.getInput();
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchevent = touchEvents.get(0);
{
if (playGame.isActivated()) {
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen aboutScreen = new AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
公共SimpleControl(浮点x、浮点y、浮点宽度、浮点高度、,
字符串位图名称,游戏屏幕(游戏屏幕){
super(x,y,宽度,高度,gameScreen.getGame().getAssetManager()
.getBitmap(位图名称),游戏屏幕);
公共布尔值已激活(){
/考虑在此更新中发生的任何触摸事件
Input Input=mGameScreen.getGame().getInput();
//检查此控件上是否存在任何触摸事件
BoundingBox bound=getBound();
对于(int idx=0;idx
我的游戏类中有一个更新方法,如下所示:
public SimpleControl(float x, float y, float width, float height,
String bitmapName, GameScreen gameScreen) {
super(x, y, width, height, gameScreen.getGame().getAssetManager()
.getBitmap(bitmapName), gameScreen);
public boolean isActivated() {
// Consider any touch events occurring in this update
Input input = mGameScreen.getGame().getInput();
// Check if any of the touch events were on this control
BoundingBox bound = getBound();
for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
if (input.existsTouch(idx)) {
if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
return true; }}}
return false; }
Input input = mGame.getInput();
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchevent = touchEvents.get(0);
{
if (playGame.isActivated()) {
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen aboutScreen = new AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
Input-Input=mGame.getInput();
List touchEvents=input.getTouchEvents();
如果(touchEvents.size()>0){
TouchEvent TouchEvent=touchEvents.get(0);
{
if(playGame.isActivated()){
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen AboutScreen=新的AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
不幸的是,当我运行此功能时,按钮不会拾取任何触摸事件,但屏幕上有一小部分会:
BoundingBox.java:
公共边界框(浮动x、浮动y、浮动半宽、浮动半高){
这个.x=x;
这个。y=y;
这个.halfWidth=halfWidth;
此参数。半高=半高;
}
公共布尔包含(浮点x,浮点y){
返回(this.x-this.halfWidthx
&&this.y-this.halfHeighty);
}
提前感谢:)您的包含(浮点x,浮点y)
看起来不正确
this.x-this.halfwidth
表示它在边界框开始之前的位置检查x
例如,你的x应该是-
(x>=this.x&&x=this.y&&y