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

2012/2/15

Help with nested if statement

lex404 lex404

2012/2/15

#
i got the lower levels of the if statement correct. however i'm trying to have actor1 move IF actor2 reaches a given coordinate. the pseudocode would be: if ( getX of actor2 =380) { do command } what would be the correct/best way to implement this? TIA!
danpost danpost

2012/2/15

#
If 'actor2' is a reference to the actor that needs to be at a certain coordinate for actor1 to move, then in the class for actor1
if (actor2.getX() == 380)
You will find the documentation in Greenfoot's 'Actor API'.
lex404 lex404

2012/2/15

#
that was the first thing i tried and i got the error "non-static method getX() cannot be referenced from a static context. Here's the code for the actor
public void act() 
    { 
       if(actor2.getX()==380)
       {
        if(getX()==532 & getY()<675)
        {
           setRotation(90);
           setLocation(getX(), getY()+1);
        }
        
        if(getY()==675 & getX()>203)
        {
           setRotation(180);
           setLocation(getX()-1, getY());
        }
        
        if(getX()==203 & getY()<676 & getY()>290)
        {
            setRotation(270);
            setLocation(getX(), getY()-1);
        }
       
        if(getX()==203 & getY()==290)
        {
            setRotation(0);
            
        }
       }   
   }
danpost danpost

2012/2/15

#
The only thing I can think, is that it is something in your actor2 class. I do no see a reason for that error message from the code above.
nccb nccb

2012/2/15

#
The code looks fine, so the problem must be elsewhere in the code, perhaps in the surrounding class declaration. Do you still have the declaration there (the class wombat { bit )? Can you post the complete code for that actor class?
lex404 lex404

2012/2/15

#
sure: actor 1:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ant1 extends Actor
{
    /**
     * Act - do whatever the ant3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    { 
       if(ant2.getY()==380)
       {
        if(getX()==532 & getY()<675)
        {
           setRotation(90);
           setLocation(getX(), getY()+1);
        }
        
        if(getY()==675 & getX()>203)
        {
           setRotation(180);
           setLocation(getX()-1, getY());
        }
        
        if(getX()==203 & getY()<676 & getY()>290)
        {
            setRotation(270);
            setLocation(getX(), getY()-1);
        }
       
        if(getX()==203 & getY()==290)
        {
            setRotation(0);
            
        }
       }   
   }
}
actor2:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ant2 extends Actor
{
    public void act() 
    {
        if(getY()<380)
        {
            setRotation(90);
            setLocation(getX(), getY()+1);
        }
    }   
}
lex404 lex404

2012/2/15

#
.
Duta Duta

2012/2/15

#
Looking at those classes you posted above, the problem is that you're trying to do <A class>.<A method> which you can only do if the method is static - what you need to do is have <An object>.<A method> To do this, change the following (lines 12-37 of ant1.class):
if(ant2.getY()==380)
{
    //Some code omitted
}
to this instead:
if(!getWorld().getObjects(ant2.class).isEmpty())
{
    ant2 antTwo = (ant2) getWorld().getObjects(ant2.class).get(0);
    if(antTwo.getY() == 380)
    {
        //Some code omitted
    }
}
Duta Duta

2012/2/15

#
Also as a pointer to stop you getting into bad habits: In java, there are certain naming conventions - you don't have to do them, but its very useful to do them for both clarity when you're looking at your code and if someone else if looking at your code. Here are some major ones: For class names (in your case ant1 and ant2), they should start with a capital letter - like so: AClass AnotherClass or in your case: Ant1 Ant2 This is known as CamelCase, or Upper CamelCase. Methods and variables should start with a lower-case letter - like so: aVariable anotherVariable aMethod() anotherMethod() This is known as Mixed case, or Lower CamelCase. Something that's useful about this is that you could change the line:
ant2 antTwo = (ant2) getWorld().getObjects(ant2.class).get(0);
to
Ant2 ant2 = (Ant2) getWorld().getObjects(Ant2.class).get(0);
My point being that, because java is case-sensitive, you can have objects of the same name as their class, as long as at least one of the letters differs in case. Oh, and I suppose I might as well mention this as well - constant fields should like this: A_CONSTANT ANOTHER_CONSTANT Constants are static final variables, which are declared like this:
public static final int AN_INT = 5;
public static final String A_STRING = "A string.";
static means that the variable (or method) is related to the class, as opposed to objects of the class. final (in terms of variables - it's different when talking about methods or classes) means that the variable cannot be changed. For more info on naming conventions, there are many pages on the web about it - here's one
lex404 lex404

2012/2/15

#
thanks! that definitely helped!! im going to go through the code and fix it up with better naming conventions.
You need to login to post a reply.