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

2014/1/2

Couple more errors

1
2
Ushi1 Ushi1

2014/1/2

#
Sorry I'm here again, but I have encountered a few more errors: The first is when I want to make my car drive without having to hold down a key all of the time. so far I have:
 /**
     * Control the car with arrow keys and change image
     */
    public void driveWithKeys()
    {
        if ("left".equals(key))
        {
            setRotation(270);
            move(4);
            setImage ("crook car left.png");
        }

        if("space".equals(key))
        {
            move(0);
        }

        accelerateWithTab();
    }

    /**
     * Car accelerates with tab held down
     */
    public void accelerateWithTab()
    {
        if (Greenfoot.isKeyDown ("tab"))
        {
            move (12);
        }
    }
( I haven't finished it as it does not work) My second problem sort of continues my previous post, but it is about detecting objects in a range from an actor. I want to make my policemen start following a robber once it comes near...
public boolean isBaddyNear()
    {
    Baddies Baddies;
    Baddies = (Baddies)getObjectsInRange(1200, Baddies.class);

    if(Baddies != null)
    {
    move (3);
    }
    }
Sorry to go on, but my third and final problem is where I want to 'drag' an object but not with my mouse. I want my car to move with a money bag once they are touching, and then drop it off in the hide-out. Basically I want to extend my method for driving to the money bag, but only when the money bag and the car are touching. So that if the car is touching the money bag, drive with keys until if the money bag is touching the hideout, where it will stop driving with keys, leaving the moneybag in the hideout. Thanks again, and just to reiterate that I am new to greenfoot and java, so I am looking for simple, easy code.
erdelf erdelf

2014/1/2

#
to your second problem, this should work
    public boolean isBaddyNear()  
        {  
        Baddies Baddies;  
        Baddies = (Baddies)getObjectsInRange(1200, Baddies.class);  
      
        if(Baddies.get(0) != null)  
        {  
          turnTowards(Baddies.get(0); 
        }  
        } 
        public void act()
        {
            move(3);
        } 
to the third problem, copy the driveWithKeys Method in the money bag class and this in the act method
if(!getObjectsAtOffset(0,0,car.class).isEmpty() && getObjectsAtOffset(0,0,Hideout.class).isEmpty())
driveWithKeys();
danpost danpost

2014/1/2

#
erdelf wrote...
to your second problem, this should work
    public boolean isBaddyNear()  
        {  
        Baddies Baddies;  
        Baddies = (Baddies)getObjectsInRange(1200, Baddies.class);  
      
        if(Baddies.get(0) != null)  
        {  
          turnTowards(Baddies.get(0); 
        }  
        } 
        public void act()
        {
            move(3);
        }
This will not work, In line 4, getObjectsInRange returns a list (not a Baddies object) and therefore cannot be assigned to the 'Baddies' field. You need to get an element from the list and assign it to the field by using the 'get' method of the List class (refer to the List API ). Then line 6 would be 'if (Baddies != null)'
erdelf erdelf

2014/1/2

#
yeah, i didnt saw that, change line 4 to
 List Baddies; 
danpost danpost

2014/1/2

#
erdelf wrote...
yeah, i didnt saw that, change line 4 to
 List Baddies; 
That still will not work. 'turnTowards' needs two int parameters, not a Actor object and line 6 would error if no Baddies are in range.
erdelf erdelf

2014/1/2

#
well, ok replace line 6-9 with
    if(!Baddies.isEmpty())
if(Baddies.get(0) != null)     
      turnTowards(Baddies.get(0).getX(), Baddies.get(0).getY());   
Ushi1 Ushi1

2014/1/2

#
erdelf wrote...
well, ok replace line 6-9 with
    if(!Baddies.isEmpty())
if(Baddies.get(0) != null)     
      turnTowards(Baddies.get(0).getX(), Baddies.get(0).getY());   
line 2 of this little extract - addies.get(0) !=null) I presume that this is Baddies, but it keeps coming up with the error - 'not a statement'. I have tried putting in '{' and '(' ahead of it and have tried other things but it still comes up with an error?
erdelf erdelf

2014/1/2

#
oh sry... i dont know why I forgot that
if(Baddies.get(0) != null)       
Ushi1 Ushi1

2014/1/2

#
so if(Baddies.get(0) != null) instead of if(!Baddies.isEmpty()) or as well as? Thanks
erdelf erdelf

2014/1/2

#
as well
Ushi1 Ushi1

2014/1/2

#
Thanks guys, but I now have a problem still...
public boolean isBaddyNear()    
    {    
        //Baddies VBaddies;    
        List ListBaddies;    

        ListBaddies = getObjectsInRange(1200, Baddies.class); 
        
        if(!ListBaddies.isEmpty())  
            if(ListBaddies.get(0) != null) 
                //VBaddies = ListBaddies.get(0) ;
                turnTowards((ListBaddies.get(0)).getX(), VBaddies.getY()); 
                //turnTowards(Baddies.get(0).getX(), ListBaddies.get(0).getY()); 
    }   
I get an error saying 'cannot find symbol - method getX()
danpost danpost

2014/1/3

#
No element in the list returned by 'getObjectsInRange' will be null. Either the list is empty, or all elements in the list will be of the class specified as Object class objects. Therefore, line 9 in the previous code post will always be 'true'. In line 11, '(ListBaddies.get(0)).getX() errs because the object from the list is not declared an Actor (or of any subclass of Actor). Finally, nowhere is the method returning a boolean value and, therefore, the method should have a 'void' return type.
public void faceBaddies() // changed name to something more appropriate
{
    if (getObjectsInRange(1200, Baddies.class).isEmpty()) return; // any baddies?
    Baddies baddies = (Baddies)getObjectsInRange(1200, Baddies.class).get(0); // get baddies
    turnTowards(baddies.getX(), baddies.getY()); // face baddies
}
Ushi1 Ushi1

2014/1/3

#
Thanks
Ushi1 Ushi1

2014/1/3

#
This is working, and my Goodies are facing the Baddies when told to, but is there a way of setting the front of the Goodies to the top, instead of the right side? At the moment it is fine if my Goodies' front is on the right side, like on a right-facing truck, but not for a face - where the top is the front that should face the Baddies. Sorry for being unclear.[Disallowed URL]
Ushi1 Ushi1

2014/1/4

#
Going all the way back to problem 1... I have decided to use the
isKeyDown("left");
method instead of the getKey() methid, but I still want to use the getKey method to start moving. So far I have this:
    public void checkKeys()
    {
        Greenfoot.getKey() lastkey;
        
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-3);
        }

        if(Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }
        
        if(Greenfoot.isKeyDown("space"))
        {
            move(0);
        }
        
        if(Greenfoot.isKeyDown("tab"))
        {
            move(12);
        }
        
        if(lastkey.equals("m"))
        {
            move(4);
        }
    }
But it doesn't work as the variable at the beginning cannot have a method in it. I understand that, but is there a way to do this anyway? Also how should I do the last little bit at the end?
There are more replies on the next page.
1
2