This site requires JavaScript, please enable it in your browser!
Greenfoot back
Avinash
Avinash wrote ...

2012/10/29

problems with a ball in a field

Avinash Avinash

2012/10/29

#
Hello, i'm making a football game but i have some problems with the ball. The ball has to stay in the lines of the field and if it hits the line of the field it needs to bounce back. I have a code but the code isn't finished yet. The ball also acts weird sometimes, it stays on the line(egde of the world) of the field and its than non-stop shaking. Can somebody help me out? this is the code: public class Voetbal extends Actor { /** * Act - do whatever the Voetbal wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int moveDirection = 1; public void act() { if(((Stadion) getWorld()).wereldEinde(this)) { moveDirection *= -1; setRotation(Greenfoot.getRandomNumber(180)); } move(8 * moveDirection); } }
Gevater_Tod4711 Gevater_Tod4711

2012/10/29

#
To let the ball bounce off you could use this method:
    public void changeDirection() {
        if (getX() < 20) {
            if (getRotation() < 180) {
                setRotation(180-getRotation());
            }
            else if (getRotation() >= 180) {
                setRotation((180-getRotation()));
            }
            if (getX() < 20) {
                for (int i = 0; i < 5 && getX() < 20; i++) {
                    setLocation(getX()+5, getY());
                }
            }
        }
        else if (getX() > getWorld().getWidth()-20) {
            if (getRotation() <= 90) {
                setRotation(180-getRotation());
            }
            else if (getRotation() > 270) {
                setRotation(180+(360-getRotation()));
            }
            if (getX() > getWorld().getWidth()-20) {
                for (int i = 0; i < 5 && getX() > getWorld().getWidth()-20; i++) {
                    setLocation(getX()-5, getY());
                }
            }
        }
        else if (getY() < 20) {
            if (getRotation() <= 270) {
                setRotation(90+(270-getRotation()));
            }
            else if (getRotation() > 270) {
                setRotation(90-(getRotation()-270));
            }
            if (getY() < 20) {
                for (int i = 0; i < 5 && getY() < 20; i++) {
                    setLocation(getX(), getY()+5);
                }
            }
        }
        else if (getY() > getWorld().getHeight() - 20) {
            if (getRotation() <= 90) {
                setRotation(270+(90-getRotation()));
            }
            else if (getRotation() > 90) {
                setRotation(270-(getRotation()-90));
            }
            if (getY() > getWorld().getHeight() - 20) {
                for (int i = 0; i < 5 && getY() > getWorld().getHeight() - 20; i++) {
                    setLocation(getX(), getY()-5);
                }
            }
        }
    }
but it would probably be better not to use setRotation but to set the location of the ball using variables like this:
int velX;
int velY;

setLocation(getX()+velX, getY()+velY);
so the ball could run much more precise and it would be easyer to let it bounce off and also other stuff.
You need to login to post a reply.