0

我正在尝试在 aBitmap的屏幕上移动onTouchaRect。当然,我不希望 Image 消失或离开屏幕,所以我为屏幕的每一侧、顶部和底部创建一个 Rect。一个我为我美丽的艺术品道歉。

onTouch

@Override
public boolean onTouch(View v, MotionEvent event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            // Left Key Touched
            if (x <= bLeftRect.right && x >= bLeftRect.left
                    && y >= bLeftRect.top && y <= bLeftRect.bottom) {

                hero.setX(hero.getX() - tileDimen);
                heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
                if (heroRect.intersect(wallTopRect)) {
                    hero.setX(hero.getX() + tileDimen);
                } else if (heroRect.intersect(wallLeftRect)) {
                    hero.setX(hero.getX() + tileDimen);
                }

                //etc....

有没有更好的方法,因为我将有更多需要碰撞矩形的对象。我在想也许会创建 1 个“周界” Rect

//Right key touched
if (perimeterRect.contains(heroRect)) {
                    hero.setX(hero.getX() + tileDimen);
                 } else {
                  //do nothing (collision) 
                 }

这会是更好的方法吗?非常感谢任何建议/见解。

谢谢,

马特

4

1 回答 1

0

想通了:检查 heroRect 是否在 each 上的 perimeterRect 内要简单得多,如果 heroRect 在MotionEventperimeter 内,然后检查 heroRect 是否与任何其他Rects碰撞

我的 onTouch 方法:

@Override
public boolean onTouch(View v, MotionEvent event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            // Left Key Touched
            if (x <= bLeftRect.right && x >= bLeftRect.left
                    && y >= bLeftRect.top && y <= bLeftRect.bottom) {

                hero.setX(hero.getX() - tileDimen);
                heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);

                if (!(perimeterRect.contains(heroRect))) {
                    hero.setX(hero.getX() + tileDimen);
                    heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
                } //else if hero collides with other Rects... {
                // }

                // Up Key Touched
            } else if (x <= bUpRect.right && x >= bUpRect.left
                    && y >= bUpRect.top && y <= bUpRect.bottom) {

                hero.setY(hero.getY() - tileDimen);
                heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);

                if (!(perimeterRect.contains(heroRect))) {
                    hero.setY(hero.getY() + tileDimen);
                    heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
                } //else if hero collides with other Rects... {
                // }

                // Right Key Touched
            } else if (x <= bRightRect.right && x >= bRightRect.left
                    && y >= bRightRect.top && y <= bRightRect.bottom) {

                hero.setX(hero.getX() + tileDimen);
                heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);

                if (!(perimeterRect.contains(heroRect))) {
                    hero.setX(hero.getX() - tileDimen);
                    heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
                } //else if hero collides with other Rects... {
                // }

                // Down Key Touched
            } else if (x <= bDownRect.right && x >= bDownRect.left
                    && y >= bDownRect.top && y <= bDownRect.bottom) {

                hero.setY(hero.getY() + tileDimen);
                heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);

                if (!(perimeterRect.contains(heroRect))) {
                    hero.setY(hero.getY() - tileDimen);
                    heroRect.set(hero.getX(), hero.getY(), hero.getX() + tileDimen, hero.getY() + tileDimen);
                } //else if hero collides with other Rects... {
                // }

            } else {
                //button was not pressed, do nothing
            }

            break;
    }
    return true;
}

现在位图不会越过边界 Rect :)

后

于 2013-09-04T22:37:26.067 回答