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

2011/11/25

Get mouse momentum

darkmist255 darkmist255

2011/11/25

#
I'm working on a ping-pong style game (2d of course) and want to make it so the player can hit harder to make the ball go faster. I will only post the code that is relevant to the momentum. This is my current code for the paddle:
public class PlayerPaddle extends Actor
{
    private int mouseYcalc, mouseXcalc;
    public int mouseX5, mouseX4, mouseX3, mouseX2, mouseX1, mouseY5, mouseY4, mouseY3, mouseY2, mouseY1 = 0;
    public void act() 
    {
        calculatePaddleMomentum();
    }  
    public void calculatePaddleMomentum()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)
        {
        mouseXcalc = mouse.getX();
        mouseYcalc = mouse.getY();
        
        mouseX5 = mouseX4;
        mouseX4 = mouseX3;
        mouseX3 = mouseX2;
        mouseX2 = mouseX1;
        mouseX1 = mouseXcalc;
        
        mouseY5 = mouseY4;
        mouseY4 = mouseY3;
        mouseY3 = mouseY2;
        mouseY2 = mouseY1;
        mouseY1 = mouseYcalc;
        }
        
    }
    public int getPaddleMomentumX()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)
        {
        return ((int)(mouseX5 - mouseX1));
        }
        else
        return 999999; // for error detection in Ball class
    }
    
    public int getPaddleMomentumY()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)
        {
        return ((int)(mouseY5 - mouseY1));
        }
        else
        return 999999; // for error detection in Ball class
    }
}
and for the ball:
public class Ball extends Actor
{
    private String xDirection = "right";
    Table world = (Table)getWorld();
    PlayerPaddle playerpaddle = (PlayerPaddle)world.playerpaddle;
    
    //Physics:
    private double yMomentum = 10;
    private double xMomentum = 5;

    public void checkPaddleCollision()
    {
        if(wait5 == 0)
        {
        PlayerPaddle collidedBlue = (PlayerPaddle)getOneIntersectingObject(PlayerPaddle.class);
        if (collidedBlue != null && xDirection == "left")
        {
            if(playerpaddle.getPaddleMomentumX() != 999999 && playerpaddle.getPaddleMomentumY() != 999999)
            {
            xMomentum = playerpaddle.getPaddleMomentumX();
            yMomentum = playerpaddle.getPaddleMomentumY();
            xDirection = "right";
            }
}
    public void act() 
    {
        if(wait5 > 0)
        {
            wait5 = (wait5 - 1);
        }
        checkPaddleCollision();
        calcPhysics();
        moveBall();
    }
        }
        EnemyPaddle collidedRed = (EnemyPaddle)getOneIntersectingObject(EnemyPaddle.class);
        if (collidedRed != null && xDirection == "right")
        {
            xMomentum = ((Greenfoot.getRandomNumber(7) + 3) * -1);
            yMomentum = (((Greenfoot.getRandomNumber(3) - 4) * (Greenfoot.getRandomNumber(3) + 1)));
            xDirection = "left";
        }
        }
    }
}
I can compile without error but when I run it and the ball hits the player paddle I get the error terminal saying "java.lang.NullPointerException at Ball.checkPaddleCollision(Ball.java:62) <-- if(playerpaddle.getPaddleMomentumX() != 999999 && playerpaddle.getPaddleMomentumY() != 999999) at Ball.act(Ball.java:29) <-- checkPaddleCollision(); " This might be because of the mouse.getX() thing, so if it is do you think I should just use getX() of the paddle, since it follows the mouse?
kiarocks kiarocks

2011/11/25

#
Does it list a caused by?
danpost danpost

2011/11/25

#
Copy and paste lines 24 thru 33 of the Ball class to line 10. (Your act() method appears within the checkPaddleCollision() method). When the checkPaddleCollision() method is called from act(), it runs into the act() method which calls the checkPaddleCollision() method. That is probably the cause of your error.
darkmist255 darkmist255

2011/11/25

#
Urgh, that was just a typo, sorry. In my code they're seperate, I just skipped the method in between that makes it follow the mouse and left out a }. @kiarocks no, this is exactly what it says: java.lang.NullPointerException at Ball.checkPaddleCollision(Ball.java:64) at Ball.act(Ball.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
kiarocks kiarocks

2011/11/25

#
put this in the act() before Line 29.
playerpaddle = (PlayerPaddle)world.playerpaddle;
darkmist255 darkmist255

2011/11/26

#
Thanks! That eliminated the error, but can you explain your reason for putting that before line 29? What purpose does it serve and why does it eliminate the nullPointer? Because I'll probably run into something like this again and I want to know how to solve it :D.
danpost danpost

2011/11/26

#
I am not sure, but it may be that you create the ball before the playerpaddle and 'playerpaddle' was therefore set to 'null'.
kiarocks kiarocks

2011/11/27

#
that code gets the player paddle, which may not have been existent in the variable.
darkmist255 darkmist255

2011/11/27

#
Thanks :D! I got it all finished up here
You need to login to post a reply.