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

2013/5/14

I need help ... im making an air hockey game but i need help

sergiodeleon702 sergiodeleon702

2013/5/14

#
what method could i use so that the paddle moves when the mouse presses it and follows it so that it can hit the puck
Gevater_Tod4711 Gevater_Tod4711

2013/5/14

#
You just need to set the position of the paddle to the mouse position if the mouse is clicked:
private boolean mouseKeyDown = false;

public void act() {
    if (!mouseKeyDown && Greenfoot.mousePressed(null)) {
        mouseKeyDown = true;
    }
    else if (mouseKeyDown && Greenfoot.mouseClicked(null)) {
        mouseKeyDown = false;
    }
    if (mouseKeyDown) {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null) {
            setLocation(mouse.getX(), mouse.getY());
        }
    }
}
If you add this code in your paddle class it should work.
sergiodeleon702 sergiodeleon702

2013/5/14

#
Thanks ! What code could i use so that when the paddle hits the puck it makes it move ? so that its an actual game ?
Gevater_Tod4711 Gevater_Tod4711

2013/5/14

#
If the puck has got a variable for his moving speed you just have to set this variable:
//in your puck class:
private int speed = 5;

public void act() {
    move(speed);
}

public void setSpeed(int speed) {
    this.speed = speed;
}
//in your paddle class:
public void act() {
    //the mouse follower stuff;
    Puck puck = getOneIntersectingObject(Puck.class);
    if (puck != null) {
        puck.setSpeed(7);//or any other value;
    }
}
You need to login to post a reply.