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

2013/8/11

Power up help?

1
2
danpost danpost

2013/8/14

#
danpost wrote...
add to the zombie act method, if intersecting person, then if counter is 10 remove self else remove person.
// in act method of Zombie class
Actor person = getOneIntersectingObject(Person.class);
if (person null)
{
    if (bigZombieCounter == 10) getWorld().removeObject(this);
    else getWorld().removeObject(person);
}
alicija alicija

2013/8/14

#
I've added the code that code in but when the zombie eats the gold brain, the person disappears. I want it so that when the zombie is bigger, if it touches the people, then the people are eaten, removing them from the world. But when the zombie its normal smaller size, if the people touch the zombie, then the zombie gets eaten. :)
__Synderz__ __Synderz__

2013/8/15

#
No offense but i dont think those people will want to eat that zombie even if they r eating them...
__Synderz__ __Synderz__

2013/8/15

#
alicija for a beginner ur doin some really cool stuff! i can barely make anything right now keep on working!:))))))))) ^ | Many Smiles for u
danpost danpost

2013/8/15

#
alicija wrote...
I want it so that when the zombie is bigger, if it touches the people, then the people are eaten, removing them from the world. But when the zombie its normal smaller size, if the people touch the zombie, then the zombie gets eaten. :)
My last code snippet should do that, exactly. I did notice that line 3 should be 'if (person != null)'. From what code you have provided, I cannot tell what is causing the person to be removed when the zombie eats the gold brain (it does not appear to be in the zombie class, unless you are making the zombie so large that the person would almost assuredly touch it when it becomes big).
alicija alicija

2013/8/15

#
I'm not sure why it isn't working. If there are people chasing the zombie on the other side of the screen, as soon as the zombie eats the gold brain, the people are disappearing off of the screen without intersecting them. Here is the zombie code: (The person chasing the zombie is called man by the way)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    private boolean facingLeft;
    private int bigZombieCounter = 10; 

    public static int currentX;
    public static int currentY;

    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        keyboard(); 
        eat();
        spawn();

        currentX = getX();
        currentY = getY();

        Actor man = getOneIntersectingObject(Man.class);  
        if (man != null)  
        {  
            if (bigZombieCounter == 10) getWorld().removeObject(this);  
            else getWorld().removeObject(man);  
        }  

    }

    public void spawn()
    {
        Actor brain;
        brain = getOneObjectAtOffset(0, 0, Brain.class);
        if (brain != null)
        {
            World world;
            world = getWorld();
            world.removeObject(brain);

            getWorld().addObject(new Brain(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));  

        }  
    }

    public void eat()
    {
        Actor goldBrain = getOneObjectAtOffset(0, 0, goldBrain.class);
        if(goldBrain != null) 
        {
            getWorld().removeObject(goldBrain);
            bigZombieCounter = 500; 
            if (facingLeft) setImage("zombieBigFlip.png"); else setImage("zombieBig.png");
        }

        if (bigZombieCounter > 0) 
        {  
            bigZombieCounter--;  
            if (bigZombieCounter == 0) 
            {  
                if (facingLeft) setImage("zombieleft.png");
                else setImage("zombieright.png");
            }  

        } 
    }

    public void keyboard()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            leftControl();
        }

        if(Greenfoot.isKeyDown("right"))
        {
            rightControl();
        }

        if(Greenfoot.isKeyDown("up"))
        {
            upControl();
        }

        if(Greenfoot.isKeyDown("down"))
        {
            downControl();
        }
    }

    public void leftControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x-5,y);
        facingLeft = true;
        if (bigZombieCounter == 0) setImage("zombieleft.png");
        else setImage("zombieBigFlip.png");
    }

    public void rightControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x+5,y);
        facingLeft = false;
        if (bigZombieCounter == 0) setImage("zombieright.png");
        else setImage("zombieBig.png");
    }

    public void upControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x,y-5);
    }

    public void downControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x,y+5);
    }
}
And here is the person's code: (I haven't added the code for the person to eat the zombie when it is at normal size yet if that has anything to do with why the code is not working)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Man here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Man extends Actor
{
    private int x;
    private int y;
    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        AI();
        worldEdge();
    }    

    public void AI()
    {
        turnTowards(Zombie.currentX, Zombie.currentY);
        move(1);
        
    }

    public void worldEdge()
    {

        if(atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(45)-20);
        }
    }

    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }

}
danpost danpost

2013/8/15

#
I tested your code and, from what I can tell, it works properly! I would suggest maybe double checking the size of your images (not the visible size, but the actual buffered image size). I can only think that either the man image or one or both of the big zombie images is oversized causing intersection of the images before they appear to touch. You can inspect the images or load them into a paintshop program to get the actual size of the images.
alicija alicija

2013/8/16

#
I had a look a the code again before, and it was working fine. I think it was to do with the buffered image size. But, I don't know what's happening now. I didn't add anything else in or take anything out (from what I am aware of), but now, the zombie eats the people chasing him when he is at normal size and when he is at bigger size. I have had a good look at the code and I can't work out why it is doing this.. Here is the zombie code again:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    private boolean facingLeft;
    private int bigZombieCounter = 10; 

    public static int currentX;
    public static int currentY;

    private Counter counter;

    public Zombie(Counter pointCounter)
    {
        counter = pointCounter;
    } 

    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        keyboard(); 
        eat();
        spawn();

        currentX = getX();
        currentY = getY();

        Actor person = getOneIntersectingObject(Man.class);  
        if (person != null)  
        {  
            if (bigZombieCounter == 10) getWorld().removeObject(this);  
            else getWorld().removeObject(person); 
            counter.add(10);
        }
    }

    public void spawn()
    {
        Actor brain;
        brain = getOneObjectAtOffset(0, 0, Brain.class); 
        if (brain != null)
        {
            World world;
            world = getWorld();
            world.removeObject(brain);
            counter.add(2);

            getWorld().addObject(new Brain(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));  

        }  
    }

    public void eat()
    {
        Actor goldBrain = getOneObjectAtOffset(0, 0, goldBrain.class);
        if(goldBrain != null) 
        {
            getWorld().removeObject(goldBrain);
            counter.add(5);
            bigZombieCounter = 500; 
            if (facingLeft) setImage("zombieBigFlip.png"); else setImage("zombieBig.png");
        }

        if (bigZombieCounter > 0) 
        {  
            bigZombieCounter--;  
            if (bigZombieCounter == 0) 
            {  
                if (facingLeft) setImage("zombieleft.png");
                else setImage("zombieright.png");
            }  
        } 
    }

    public void keyboard()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            leftControl();
        }

        if(Greenfoot.isKeyDown("right"))
        {
            rightControl();
        }

        if(Greenfoot.isKeyDown("up"))
        {
            upControl();
        }

        if(Greenfoot.isKeyDown("down"))
        {
            downControl();
        }
    }

    public void leftControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x-5,y);
        facingLeft = true;
        if (bigZombieCounter == 0) setImage("zombieleft.png");
        else setImage("zombieBigFlip.png");
    }

    public void rightControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x+5,y);
        facingLeft = false;
        if (bigZombieCounter == 0) setImage("zombieright.png");
        else setImage("zombieBig.png");
    }

    public void upControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x,y-5);
    }

    public void downControl()
    {
        int x = getX();
        int y = getY();
        setLocation(x,y+5);
    }
}
And here is the person's code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Man here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Man extends Actor
{
    private int x;
    private int y;

    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        gameOver();
        AI();
        worldEdge();
    }    

    public void gameOver()
    {
        Actor actor = getOneObjectAtOffset(0, 0, Zombie.class);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
            gameOver();
            Greenfoot.stop();
        }
    }

    public void AI()
    {
        turnTowards(Zombie.currentX, Zombie.currentY);
        move(1);
    }

    public void worldEdge()
    {
        if(atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(45)-20);
        }
    }

    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }

}
danpost danpost

2013/8/16

#
In the Zombie class, change '10' in lines 12 and 40 to '0'.
alicija alicija

2013/8/19

#
It is working fine now. Thanks heaps! :)
You need to login to post a reply.
1
2