繁体   English   中英

Android-复古壁球游戏-球拍/屏幕侧面碰撞

[英]Android - Retro Squash Game - racket/screen sides collision

我正在阅读“通过构建Android游戏学习Java”,并且在Retro Squash Game示例中,我不知道如何为球拍实现碰撞检测。 球拍的移动使用onTouchEvent。 我试图实现一个if语句,但是直到下一次触摸事件才会被检查-因此它无法正常工作。 请帮忙。

    //Event that handles in which direction is the racket moving according to where is the user touching the screen
    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        //gets the movement action without the pointers(ACTION_MASK) ??? -U: handles multitouch

        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            //what happens if user touches the screen and holds
            case MotionEvent.ACTION_DOWN:
                //if the screen was touched on the right side of the display than move the racket right
                if (motionEvent.getX() >= (screenWidth / 2) && racketPosition.x <= screenWidth) {
                    racketIsMovingLeft = false;
                    racketIsMovingRight = true;
                } else if (motionEvent.getX() < (screenWidth / 2) && racketPosition.x >= 0) {
                    racketIsMovingLeft = true;
                    racketIsMovingRight = false;
                } else {
                    racketIsMovingLeft = false;
                    racketIsMovingRight = false;
                }

                break;
            //when the user lets go of the screen the racket immediately stops moving
            case MotionEvent.ACTION_UP:
                racketIsMovingLeft = false;
                racketIsMovingRight = false;
                break;
        }
        return true;
    }

好。 我找到了解决方案。 我没有查看正确的代码-抱歉。 我已经编辑了负责更改球拍的racketPoint.x的片段。 这里是:

    public void updateCount() {
        //change the racket position according to the movement
        if (racketIsMovingRight && (racketPosition.x+racketWidth/2)<=screenWidth) {
            racketPosition.x += racketSpeed;
        }

        if (racketIsMovingLeft && (racketPosition.x-racketWidth/2)>=0) {
            racketPosition.x -= racketSpeed;
        }

//其余的代码

暂无
暂无

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

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