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

2014/4/15

Having trouble replacing object

Justis52 Justis52

2014/4/15

#
Every time i try and replace an object it always says that my object that I'm trying to show by replacing it isn't a variable.what do i do????it also has to replace when a key is pressed.how would i put that in code I'm coming from Alice so i know what to code i just don't know how to make the methods.
 public void moveOnKeyPress()  
    {  
        if(Greenfoot.isKeyDown("left")) {  
            moveLeft();  
        }  
         if(Greenfoot.isKeyDown("right")) {  
            moveRight();  
        }  
           
        if(Greenfoot.isKeyDown("up")) {  
            moveUp();  
        }  
        if(Greenfoot.isKeyDown("down")) {  
            moveDown();  
        }   if(Greenfoot.isKeyDown("F")) {  
            replace();  
        }  
    } public void replace()
    { Actor goku = getOneObjectAtOffset(0, 0, goku.class); if (goku != null) { getWorld().removeObject(goku);Actor gokucharge = getOneObjectAtOffset(0, 0, gokucharge.class);} }
}
danpost danpost

2014/4/15

#
Cancel that. I see code now.
danpost danpost

2014/4/15

#
First off, it is not wise to name your variables with the same name as the class (especially if you are going to ask if the value of that variable is null). So, before anything else, rename 'goku' to anything else (maybe 'gokuActor' or 'gokuObject') and similar with 'gokucharge'. The only other thing I can immediately see, is your last line of code that gets a 'gokucharge' object does not do anything (you are assigning a local variable to an object (if it is not 'null') and then not doing anything else with it.
You need to login to post a reply.