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

2013/12/27

how to use getX and getY

deathburrito16 deathburrito16

2013/12/27

#
when I tried this code, I got a "variable getX might not have been initialized."
 /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        {
            if (Greenfoot.isKeyDown("w"))
            {
                move(1);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-1);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            turn(-2);
            move(1);
        }
       if (Greenfoot.isKeyDown("d"))
        {
        turn(2);
        move(1);
    }}
    int getX;
    int getY;
    if (Greenfoot.isKeyDown("m"))
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX + 5, getY);
 
    }
    }
    }
deathburrito16 deathburrito16

2013/12/27

#
I'm probably being a complete idiot, but I started programming like a week ago.
danpost danpost

2013/12/27

#
Lines 26 and 27 creates two int fields (variables) named 'getX' and 'getY'. These fields are never set to any values. Please note that no methods are being called within these lines. All method calls must end with a set of parenthesis. What goes between them, if anything, is determined by the declaration statement (signature) of the method. Please refer to the Java lesson on Classes and Objects, particularly the page on Defining Methods. In fact, you may want to go to the Home page and start at the beginning (click on 'Previous' from the top page of that lesson).
deathburrito16 deathburrito16

2013/12/28

#
ok figured it out now
You need to login to post a reply.