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

2012/10/30

PROBLEM USING IMAGES AS OBJECTS

DOGGY DOGGY

2012/10/30

#
hello everyone, im making a pong game just as in the downloadable scenarios but i have a problem using the "bat" (paddle) image which i used for the class. I used a picture to create the bat but now the ball doesnt recognize the image as an object and just runs thru it. It doesnt bounce back after hitting it but just run thru it. I thought maybe i can use the getOneIntersectingObject to let the ball recognize the bat but im not pretty sure how to use it. I have two batts that need to be recognized by the ball. The classes im using: 2 player classes (which uses the bat image i want to get recognized) 1 ball class can somebody help me?
MatheMagician MatheMagician

2012/10/30

#
To figure out whether the ball is touching the paddle or not, you can use this code:
if(getOneIntersectingObject(Paddle.class)!= null)
{
       //do whatever you need to do
}
Replace the word Paddle with the name of your bat class.
DOGGY DOGGY

2012/10/30

#
Thankyou for your reply, it works but know i want to write the code for line nr 03. in your reply. ( where //do whatever you need to do stands) I would like to let the ball leave the paddle in the opposite side of which it came in. Can you help me with this code?
MatheMagician MatheMagician

2012/10/30

#
Well, what code do you have so far to make it move?
DOGGY DOGGY

2012/11/1

#
NOW ALL I HAVE IS THIS: public void checkForBat() { if (getOneIntersectingObject(Player.class)!=null) { setRotation(Greenfoot.getRandomNumber(180)); } I would like to change the getRandomNumber for a code which makes the ball leave the bat in the opposite of which it came in. For example if it came from the right it need to bounce of the bat en leave to the left of the bat Is this enough information?
Gevater_Tod4711 Gevater_Tod4711

2012/11/1

#
I think you need 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);
                }
            }
        }
    }
Using this method the angle of reflection is like the angle of incidince.
You need to login to post a reply.