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

2013/11/11

Keep getting an error

craigd04 craigd04

2013/11/11

#
Okay I've to make the car collide with three different objects using different code. The first two are fine and work, heres an example -
    */
    public void act() 
    {
        // Add your action code here.
        checkCollision();
    }    
    
       private void checkCollision()
    {//check if the character collides with other game objects         
        
       
       //check if the character collides with the car    
      Actor Car = getOneIntersectingObject(Car.class);
        if(Car!=null)
    { 
       setLocation((Greenfoot.getRandomNumber(600)+50),(Greenfoot.getRandomNumber(600)+50) );  
             
           //endif
    }
}
}
I'm stuck on the bear though can anyone help?
 /**
     * Act - do whatever the Bear wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        checkCollision();
       
    }    
    
       private void checkCollision()
    {//check if the character collides with other game objects         
        
       
       //check if the character collides with the car    
        Actor Car = getObjectsInRange(Bear.class);
        if(Car!=null)
    { 
       setLocation((Greenfoot.getRandomNumber(600)+50),(Greenfoot.getRandomNumber(600)+50) );  
             
           //endif
    }
}
}
bourne bourne

2013/11/11

#
You are missing a parameter at line 17 getObjectsInRange(int radius, java.lang.Class cls) Return all objects within range 'radius' around this object.
craigd04 craigd04

2013/11/11

#
bourne wrote...
You are missing a parameter at line 17 getObjectsInRange(int radius, java.lang.Class cls) Return all objects within range 'radius' around this object.
What goes after the int radius part? I don't know what this java.lang.class is :S
bourne bourne

2013/11/11

#
Exactly what you do have, Bear.class
craigd04 craigd04

2013/11/11

#
bourne wrote...
Exactly what you do have, Bear.class
Thats what I thought. I get this error then. "incompatible types"
bourne bourne

2013/11/11

#
Do you actually have a class type Bear? And can I see what you have now?
craigd04 craigd04

2013/11/11

#
Okay This is my Bear Actor -
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bear here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bear extends Actor
{
    /**
     * Act - do whatever the Bear wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        checkCollision();
       
    }    
    
       private void checkCollision()
    {//check if the character collides with other game objects         
        
       
       //check if the character collides with the car    
        Actor Car = (getObjectsInRange(35, Bear.class));
        if(Car!=null)
    { 
       setLocation((Greenfoot.getRandomNumber(600)+50),(Greenfoot.getRandomNumber(600)+50) );  
             
           //endif
    }
}
}
this is my Car Actor -
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car here.
 * 
 * @author (Craig Dewart) 
 * @version (2013)
 */
public class Car extends Actor
{
    //class level variables
    private int prevX;
    private int prevY;
    private int collisions;
    private boolean collided = false;
    
    /**
     * Act - do whatever the Car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movePlayer();
        checkCollision();
        storeCurrentPosition();
    }    
    
    
    
    public void movePlayer()
    {
        if(Greenfoot.isKeyDown("left"))
        turn(-5);
        
        if(Greenfoot.isKeyDown("right"))
        turn(5);
        
        if(Greenfoot.isKeyDown("up"))
        move(5);
        
        if(Greenfoot.isKeyDown("down"))
        move(-5);
    }
    
      private void storeCurrentPosition()
   {
       prevX = getX();
       prevY = getY();
    } // end of method sotre current position
    
    public boolean atWorldEdge(Actor actor)
    {//checks to see if the game character is within the game boundary
     //returns true if they are at the edge and false if they are not
        
       if((actor.getX()>=getWorld().getWidth()-(actor.getImage().getWidth()/2))||
          (actor.getX()<=(actor.getImage().getWidth()/2))||
          (actor.getY()>=getWorld().getHeight()- (actor.getImage().getHeight()/2))||
          (actor.getY()<=(actor.getImage().getHeight()/2)))
            return true;
       else
            return false;
       //endif
    }//end of method atWorldEdge
    
    public void checkCollision()
    {//checks to see if it is colliding and if it is it will count the points.
      Actor ball = getOneIntersectingObject(Ball.class);
      Actor balloon = getOneObjectAtOffset(0,0,Balloon.class);
      
      if(collided==false)
      {
//         Actor ball = getOneIntersectingObject(Ball.class);
        if(ball!=null)
         {
          setLocation(prevX , prevY);
          collisions++;
          collided=true;
        }
        
       //endif
           
//          Actor balloon = getOneObjectAtOffset(0,0,Balloon.class);
        if(balloon!=null)
        {
        setLocation(prevX , prevY);
        collisions++;
        System.out.println (collisions);
        collided=true;
        }
           
        if(!getObjectsInRange(35, Bear.class).isEmpty())
          { setLocation(prevX , prevY); 
              collisions++;
            System.out.println (collisions);
            collided=true;
            }

           
            
        if(atWorldEdge(this))
           { setLocation(prevX , prevY);  
       //endif
       }
       
    }
    if((ball==null)||(balloon==null)||(getObjectsInRange(35, Bear.class).isEmpty()))
       collided=false;
    }
    
//    public int getScore()
//    {
//        return collisions;
//     }
}
bourne bourne

2013/11/11

#
Actor Car = (getObjectsInRange(35, Bear.class));
getObjectsInRange returns a list so cannot be used to assign to a variable of type Actor Also by convention, the variable "Car" here should start with a lowercase letter And perhaps instead of "car", using a descriptive name like "bear" for what is intended for it to contain
You need to login to post a reply.