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

2013/12/19

Non-Static/Static Conflict

GeicoCoder GeicoCoder

2013/12/19

#
In my breakout game I'm trying to switch to a new level and the static/non-static reference keeps popping up. I've read previous discussions, but they don't help with my situation. Here is the malfunctioning code for my second paddle and second world: World2:
public static void ballIsOut2()
    {
        Paddle2.newBall2();
        lifeLost();
    }
Paddle2:
/**
     * When the paddle gets created, create a ball as well.
     */
    public void addedToWorld(World2 world2)
    {
        newBall2();
    }
    
    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown ("left")) {
            moveSideways(-9);
        }
        if (Greenfoot.isKeyDown ("right")) {
            moveSideways(9);
        }
        if (haveBall2() && Greenfoot.isKeyDown ("space")) {
            releaseBall2();
            
        }
        
    }
    
    public void moveSideways(int dist)
    {
        setLocation (getX() + dist, getY());
        if (myBall2 != null) {
            myBall2.move (dist);
        }
    }
    
    public void newBall2()
    {
        myBall2 = new Ball2();
        getWorld2().addObject (myBall2, getX(), getY()-27);
    }
        
    public boolean haveBall2()
    {
        return myBall2 != null;
    }
        
    public void releaseBall2()
    {
        myBall2.release();
        myBall2 = null;
    }
danpost danpost

2013/12/19

#
'newBall2' is a non-static method (and cannot be made static). You cannot call that method on the name of the class; you need an object created from that class to call that method on. As I do not know what class you are calling the method on or where, if anywhere, you have a reference to your Paddle2 object, it is impossible to determine how to fix this.
You need to login to post a reply.