سؤال

I am using a game engine from Beginning Android Games and making a breakout game from scratch. I have the paddle/ball/bricks working but right now I am working out the bugs. Randomly, my pong will hit any wall and it will just stick to it and vibrate left and right and kind of just fly off the screen. Another example is sometimes the pong hits the paddle in such a way that instead of bouncing off of it, it goes through it and then proceeds to change direction after it goes through it, I feel like its not obeying my bounds. I am testing on a nexus 7 here is the pong class

public class Pong {
    public float x, y;
    public int speedX, speedY;
    public static final int LEFT = 0;
    public static final int RIGHT = 0;
    public static final int WIDTH = 32;
    public static final int HEIGHT = 32;
    public static final int DIAMETER = 15;
    public int direction;
    public boolean inBounds;

    public Pong(int x, int y) {
        this.x = x;
        this.y = y;
        speedX = 500;
        speedY = 500;
        inBounds = true;
    }

    public void stop() {
        speedX = 0;
        speedY = 0;
    }

    public void update(float deltaTime) {
        if (inBounds) {
            x += speedX * deltaTime;
            y += speedY * deltaTime;
            if (x + WIDTH >= 800) {
                speedX *= -1;
            }
            if (x <= 0) {
                speedX *= -1;
            }
            if (y >= 1280) {
                // inBounds = false;
                speedY *= -1;
            }
            if (y <= 0) {
                speedY *= -1;
            }
        }

    }

    public void reverse() {
        speedY *= -1;
    }

}

my paddle class

public class Paddle {
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int NEUTRAL = 2;
    public static final int WIDTH = 192;
    public static final int HEIGHT = 32;
    public float x;
    public int y;
    public int speed;
    public int direction;

    public Paddle(int x, int y) {
        this.x = x;
        this.y = y;

    }

    public void turnLeft() {
        speed = 500;
        direction = LEFT;
    }

    public void turnRight() {
        speed = 500;
        direction = RIGHT;
    }

    public void stop() {
        speed = 0;
    }

    public void update(float deltaTime) {

        if (direction == LEFT && x > 0) {
            x -= speed * deltaTime;
        } else if (direction == RIGHT && x + WIDTH < 800) {
            x += speed * deltaTime;
        } else if (direction == NEUTRAL) {
            stop();
        }

    }
}

and the collision detection I made which is probably wrong

private boolean paddleCollision(Paddle paddle, Pong pong) {
        if (pong.x <= paddle.x + Paddle.WIDTH) {
            if (pong.x >= paddle.x) {
                if (pong.y + Pong.HEIGHT >= paddle.y) {
                    return true;
                }
            }
        }
        return false;

    }

    private boolean tileCollision(Tile tile, Pong pong) {
        if (tile == null) {
            return false;
        }
        if (pong.x <= tile.x + Tile.WIDTH) {
            if (pong.x >= tile.x) {
                if (pong.y <= tile.y + Tile.HEIGHT) {
                    if (pong.y + Pong.HEIGHT >= tile.y) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
هل كانت مفيدة؟

المحلول

I'm not sure this is the solution, but: I didn't like much the Paddle::update() Pong::update() methods because with if (x > 0 ... x < 800) you are ensuring that the initial position is valid and then set the final position to something that could be outside your screen... I'd go with something like:

public class Paddle {
    public void stop() {
        speed = 0;
        direction = NEUTRAL;        
    }

    public void update(float deltaTime) {
        // here, x is valid
        x += (direction == LEFT?-1:+1) * speed * deltaTime;

        if (x < 0) {
            x = 0;
            stop();
        }
        else if (x > 800) {
            x = 800;
            stop();
        }

        // here, x is valid
    }
}

public class Pong {
    public void update(float deltaTime) {
        if (inBounds) {
            x += speedX * deltaTime;
            y += speedY * deltaTime;
            if (x + WIDTH >= 800) {
                speedX *= -1;
                x = 800-WIDTH-1;
            }
            if (x <= 0) {
                speedX *= -1;
                x = 1;
            }
            if (y >= 1280) {
                speedY *= -1;
                y = 1279;
            }
            if (y <= 0) {
                speedY *= -1;
                y = 1;
            }

            // here, Pong is in screen
        }

    }
}

نصائح أخرى

I have had this problem before, and if i may make a suggestion that may help i would make two if statments for bounds. if(x<=0||x>=MaxX), (same for y)your problem might be that you have so many conditions it gets mixed up.. Im sorry if that doesnt fix your problem, i will change my answer if that doesnt work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top