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

2016/8/31

Is there a way I can make my main character be pushed back whenever the "Zombies" hit it (Which would work in any direction)?

Dudimus Dudimus

2016/8/31

#
Here's Chased's (Main character) code:
import greenfoot.*;
import java.awt.Color;


/**
 * Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
 */
public class Chased extends Actor
{
    private boolean touchingZ = false;
    private boolean touchingZ2 = false;
    private boolean touchingZ3 = false;
    private int rotation;
    Gun gun = new Gun(); // the gun for this actor
 
    int timer;
    int bullet=10;
   
   
    /**
     * creates image for actor
     */
    public Chased()
    {
        // the image for this actor
        GreenfootImage image = new GreenfootImage(50, 50);
        image.fillOval(0, 0, 50, 50);
        image.setColor(Color.green);
        image.fillOval(7, 7, 36, 36);
        setImage(image);
    }
    
    /**
     * adds the gun for this actor into the world
     *
     * @param world the world this actor was added into
     */
    public void addedToWorld(World world)
    {
        world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
    }
    
    public void movement()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("a")) {
            if(getOneObjectInFront(Car.class)==null)
            dx--;
        }
        if (Greenfoot.isKeyDown("d")){ 
              if(getOneObjectInFront(Car.class)==null)
            dx++;
        }
        if (Greenfoot.isKeyDown("w")) {
              if(getOneObjectInFront(Car.class)==null)
            dy--;
        }
        if (Greenfoot.isKeyDown("s")) {
              if(getOneObjectInFront(Car.class)==null)
            dy++;
        }            
        setLocation(getX()+1*dx, getY()+1*dy);
        // limit to world bounds
        if (getX() < 25) setLocation(25, getY());
        if (getY() < 25) setLocation(getX(), 25);
        if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
        if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
        // position gun
        gun.setLocation(getX(), getY());
         Actor chaser = getOneIntersectingObject(Chaser.class);
         
      
    }
    
  
    
      /**
     * move user-controlled actor and its gun; also, check for game over
     */
    public void act()
    {
       movement();  
        Chaser chaser = new Chaser();
        getOneIntersectingObject(Chaser.class);
      
       if (chaser != null)
      
                {
            World world = getWorld();
                    Welt welt = (Welt)world; 
                    
                    HealthBar healthbar = welt.getHealthBar();
                     
                    if(touchingZ == false)
                    {
                      
                       healthbar.loseHealth();
                        
                        touchingZ = true;
                       
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
        }else{
                    touchingZ = false;
                }
                
            
                
       Actor runner = getOneIntersectingObject(Runner.class);
         
       if (runner != null)
      
                {
            World world = getWorld();
                    Welt welt = (Welt)world; 
               
                    HealthBar healthbar = welt.getHealthBar();
                   
                    if(touchingZ2 == false)
                    {
                        healthbar.loseHealth();
                        
                        touchingZ2 = true;
                       
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
        }else{
                    touchingZ2 = false;
                }
                
       Actor bloater = getOneIntersectingObject(Bloater.class);
         
       if (bloater != null)
      
                {
            World world = getWorld();
                    Welt welt = (Welt)world; 
               
                    HealthBar healthbar = welt.getHealthBar();
                
                    if(touchingZ3 == false)
                    {
                        healthbar.loseHealth();
                        
                        touchingZ3 = true;
                       
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
        }else{
                    touchingZ3 = false;
                }
                
       
          
        }
        
    
     
    
        public Actor getOneObjectInFront(Class a)
       {
        GreenfootImage myImage=getImage();
        int distanceToFront=myImage.getWidth()/2;
        int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
        int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
        return(getOneObjectAtOffset(xOffset, yOffset, a));
        }
     
    /**
     * Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
     */
    private class Gun extends Actor
    {
        /**
         * creates image for actor
         */
        public Gun()
        {
            // the image for this actor
            GreenfootImage image = new GreenfootImage("Picture1.png");
           
            setImage(image);
        }
         
    public boolean Munition(){
        if(bullet>0){
            return true;
        }
        else{
            return false;
        }
    }
    GifImage gifImage = new GifImage("57c3b88d56899519809115.gif");
    private boolean mouseclick;
    /**
         * responds to mouse movement and mouse button clicks
         */
        public void act()
        {
            // turn towards mouse when mouse moves
            if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
            }
            // detect mouse clicks to fire shot   
           
            World world = getWorld();
                    Welt welt = (Welt)world;

                    AmmoBar ammobar = welt.getAmmoBar();
                     
                   
            if(!mouseclick && (Greenfoot.mouseClicked(null)))
            {
                
             
                    if(Munition()==true){
                    mouseclick=true;
                    getWorld().addObject(new Shot(), getX(), getY());
                    bullet--;
                    ammobar.loseAmmo();
                    if (bullet ==0){
                        timer = 2000;
                    }
                
                }
            }
            if(mouseclick&& !Greenfoot.isKeyDown("space"))
            {
                mouseclick=false;
            }
           
             if (timer > 0){
                    timer--;
                    if (timer==0){
                        bullet+=10;
                        ammobar.addAmmo();
                    }
                }
                
        }
        
       
       
       /**
         * Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
         */
        private class Shot extends QActor
        {
            
            /**
             * sets bounds fields and creates the image for this actor
             */
            public Shot()
            {
                setBoundedAction(QActor.REMOVE, 5); // set bounds fields
                // create image for this actor
                setImage(gifImage.getCurrentImage());
                
            }
            
            /**
             * initializes rotation and position of actor
             *
             * @param world the world this actor was added into
             */
            public void addedToWorld(World world)
            {
              
                setRotation(Gun.this.getRotation()); // set rotation (to that of gun)
                move(7000); // set position (at end of gun)
                rotation = getRotation();
                setRotation(0);
            }
            
            /**
             * moves actor and checks for removal of actor
             */
            public void act()
            {
                
                setVX(0);
                setVY(0);
                addForce(0, getRotation()*QVAL);
                move(); // moving (equivalent to 'move(5)' for a non-QActor)
                 World world = getWorld();
                    Welt welt = (Welt)world;
                     Numberz numberz = welt.getNumberz();
                          
                if (hits(Chaser.class) || atWorldEdge()) 
                {
                    getWorld().removeObject(this); // removing
                    numberz.loseNum();
                   
                }else if (hits(Runner.class) || atWorldEdge()) 
                {
                    numberz.addNum();
                    getWorld().removeObject(this); // removing
                    numberz.loseNum();
                   
                }else if (hits(Bloater.class) || atWorldEdge()) 
                {
                    numberz.addNum();
                    getWorld().removeObject(this); // removing
                    numberz.loseNum();
                   
                } 
                
                
            }
                private boolean touchingFire = false;
                /**
             * internal method to detect object collision; returns true if collision detected, else false
             *
             * @param cls the class of object to check for collision with
             * @return a flag indicating whether an object of given class was detected or not
             */
            private boolean hits(Class cls)
            {
                // get intersecting object and return result
                
                 World world = getWorld();
                    Welt welt = (Welt)world;
 
                Actor chased = getOneIntersectingObject(Chased.class);
                if (chased != null){
                   HealthBar healthbar = welt.getHealthBar();
                    if(touchingFire == false)
                    {
                        
                        healthbar.loseHealth();
                        
                        touchingFire = true;
                        
                      
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
                }else{
          
                    touchingFire = false;
                 
                }
                Actor clsActor = getOneIntersectingObject(cls);
                if (clsActor != null)
                {
                    // remove intersector and bump score
                    getWorld().removeObject(clsActor);
                    
                    return true; 
                }
                return false;
                 
            }
             
            /**
             * internal method that returns a flag indicating world edge encroachment
             *
             * @return a flag indicating whether the actor has encroached on a world edge or not
             */
            private boolean atWorldEdge()
            {
                // return state of encroachment on world edge
                return getX() <= 0 || getY() <= 0 ||
                    getX() >= getWorld().getWidth()-1 ||
                    getY() >= getWorld().getHeight()-1;
            }
        }
    }
}
Super_Hippo Super_Hippo

2016/8/31

#
It can be solved like this:
Actor zombie = getOneIntersectingObject(Zombie.class);
if (zombie != null)
{
    int rot = getRotation();
    turnTowards(zombie.getX(), zombie.getY());
    move(-5); //how far the character should be pushed back
    setRotation(rot);
}
Dudimus Dudimus

2016/8/31

#
I tried putting it but I get an error message saying: stackoverflow, nullpointerexception, etc. What should I do?
danpost danpost

2016/8/31

#
You could, instead of having the main character check for zombies (chasers), have the zombies check for the main character (that they are chasing). Then the main character can be moved in the same direction that the zombie had just moved when making contact. The health of the main character can still be dealt from that location (the class of the chasers). I would suggest that since the health value is something that belongs to the chased, that it (the value and the bar that displays it) be something that is maintained within the Chased class. I am not saying that you must move them (as your project can work properly as is) -- just that you should keep that in mind for future reference. Basically, place fields and object references in the class that creates the objects that they belong to.
Dudimus Dudimus

2016/8/31

#
Oh wait, it works! Thank so much.
You need to login to post a reply.