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

2015/5/29

Help with following

Bert389 Bert389

2015/5/29

#
I am creating a game where a villain chases a hero. When the hero gets the camoflauge power up, it will turn into the same image as the background. What I am having trouble with is making the villain stop chasing the hero when the hero is camoflauged. My follow method works and my wander method works, the problem is getting the villain to switch from one to the other. Any suggestions?
public class villain extends Actor
{
    GreenfootImage camoHero = new GreenfootImage("space.jpg");
    hero smile = new hero();
    /**
     * Act - do whatever the villain wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(getImage() != camoHero)
        {
        follow();
        move(2);}
        else
        {
        wander();
        
        move(2);
        }
    }    
    
    public void follow()
    {
        
       
        
        
        String target;
        
        List <hero> group = getObjectsInRange(1000,hero.class);
        if(group.size()>0)
        {
            Actor guy = group.get(0);
            int targetX = guy.getX();
            int targetY = guy.getY();
            turnTowards(targetX, targetY);
        }
        
            
            
            
         }
   
    public void wander()
    {
        
        
            
       if(Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90)-45);
    }
    if(getX() <= 5 || getX() >= getWorld().getWidth() - 5)
    {
        turn(180);
    }
    if(getY() <= 5 || getY() >= getWorld().getHeight() - 5)
    {
        turn(180);

    }  
                
                
                
                
           
        
        }
    
    }
    
danpost danpost

2015/5/29

#
When you say that the image of the hero is the image of the background when camouflaged, do you mean that the hero is essentially invisible? Line 11 checks to see if the 'camoHero' image is set as the image of the villain. Is that really what you want to check? And, in either case, the images will not be the same unless 'camoHero', specifically, is set as the image (creating a new image from the file "space.jpg" file will never be "the same image" as one already created and set (they are two distinct object and one is not the other and comparing them with '==' will always return a false value). The same goes with the 'smile' field which holds a 'Hero' object; each villian object created will create a unique hero object (fortunately, these objects are not being referenced anywhere; so, you are just wasting memory with them). If you only have one Hero object in the world at any time, then maybe you should keep the 'hero smile' field in your world class and add a getter method for the villain object to access it with:
// in World subclass
private hero smile = new hero();
// in constructor of same class
addObject(smile, < x, y >);
// add method in same class
public hero getHero()
{
    return smile;
}
Then in the villain class you can use (I will call your World subclass 'MyWorld'):
hero smile = ((MyWorld)getWorld()).getHero();
if (smile.getWorld() == getWorld())
{
    // etc.
}
The last line 2 ensures that the hero object returned is actually in the same world as the villain object is in. Line 4 should be replaced with code to check the image of the hero object and either follow or wander. Use 'smile' to refer to the hero object; for examples:
turnTowards(smile.getX(), smile.getY());
// or
if (smile.getCamoImage() == smile.getImage())
The last line assumes there is a field in the hero class that holds the 'camoHero' image that will be set to the hero object when camouflaged. The call to 'getCamoImage' also assumes there is a getter method that returns that image. As such, the image will indeed be that image when camouflaged.
You need to login to post a reply.