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

2016/4/30

moving actor one given location and go to another

1
2
NIRO17 NIRO17

2016/4/30

#
hi!!!... i'm going to develop a zombie game..so i created three actor classes (zombie,parent and children).what i want to do is zombie first find the given location of parents and eat(remove) them and as soon as possible zombie have to move to the child and eat child and game over..please someone help me.. this is the code i have developed so far.. public void act() { move(1); chasePrent(); Actor Parent=getOneIntersectingObject(parent.class); Actor child=getOneIntersectingObject(child.class); if(Survivor!=nulll){ getWorld().removeObject(parent); getWorld().removeObject(me); } } public void parent(){ turnTowards(parent.parentX, parent.parentY); } }
danpost danpost

2016/4/30

#
Please show the code for the entire class -- and have it indented properly and use code tags (if this does not make sense, click on the link below the reply box 'Posting code? read this!').
NIRO17 NIRO17

2016/5/1

#
here is my world class.in here in set zombies,parent and child set into the world randomly.

    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
    
    /**
     * Write a description of class MyWorld here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MyWorld extends World
    {
    
        /**
         * Constructor for objects of class MyWorld.
         * 
         */
        public MyWorld()
        {    
            // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
            super(600, 400, 1);
            
            
         prepaire();
        }
        
        public void prepaire(){
        zombie b1 = new zombie();
          int zX1=Greenfoot.getRandomNumber(getWidth());
          int zY1=Greenfoot.getRandomNumber(getHeight());
          addObject(b1,zX1,zY1);
          
          child c1 = new child();
          int cX1=Greenfoot.getRandomNumber(getWidth());
          int cY1=Greenfoot.getRandomNumber(getHeight());
          addObject(c1,cX1,cY1);
          
          
          child c2 = new child();
          int cX2=Greenfoot.getRandomNumber(getWidth());
          int cY2=Greenfoot.getRandomNumber(getHeight());
          addObject(c2,cX2,cY2);
          
          parent a1 = new parent();
          int pX1=Greenfoot.getRandomNumber(getWidth());
          int pY1=Greenfoot.getRandomNumber(getHeight());
            /*speed change*/
          addObject(a1,pX1,pY1);
          
          parent a2 = new parent();
          int pX2=Greenfoot.getRandomNumber(getWidth());
          int pY2=Greenfoot.getRandomNumber(getHeight());
          addObject(a2,pX2,pY2);
          
        
    }
    }

NIRO17 NIRO17

2016/5/1

#
and this is my parent class and also child class like this..

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class parent here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class parent extends Actor
{
    /**
     * Act - do whatever the parent wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public static int parentX, parentY;
    public void act() 
    {
         parentX=getX();
       parentY=getY();
        
        // Add your action code here.
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class parent here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class parent extends Actor
{
    /**
     * Act - do whatever the parent wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public static int parentX, parentY;
    public void act() 
    {
         parentX=getX();
       parentY=getY();
        
        // Add your action code here.
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class parent here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class parent extends Actor
{
    /**
     * Act - do whatever the parent wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public static int parentX, parentY;
    public void act() 
    {
         parentX=getX();
       parentY=getY();
        
        // Add your action code here.
    }    
}
NIRO17 NIRO17

2016/5/1

#
and this is my zombie class... i am confusing with how to chage this code according to my senario.. this zombie only find the parent in the world and eat and stuck in the final parent location.but i want to after finishing eat the parent, immidiatly go to the child locations and eat child and stop like parent.
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
{
    /**
     * 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() 
    {
        chaseParent();
        // Add your action code here.
    }    

    public void chaseParent(){ 

        turnTowards(parent.parentX, parent.parentY);
        Actor parent=getOneIntersectingObject(parent.class);
        getWorld().removeObject(parent);

        move(2);
    }
}
danpost danpost

2016/5/1

#
Okay. The problem is the way you set up the parent and child classes. Because you are updating the location of the actors in the static fields, two things will result. The first one is that the zombies will always go after the parents in a specific order, which may not be too big of a deal. The other is that once all the parents are removed, the static fields will continue to hold the location of the last parent removed. That is way the zombies get stuck there. You could modify the 'act' method of the zombie class to compensate by making sure a parent is at that location (or, rather, anywhere in the world) before proceeding there. If no parent is there, then the child location should be the next target:
public void act()
{
    if (getWorld().getObjects(parent.class).isEmpty())
    {
        chaseChild();
    }
    else
    {
        chaseParent();
    }
}
Then add the 'chaseChild' method to the class similar to the 'chaseParent' method.
NIRO17 NIRO17

2016/5/1

#
thank you very much..it's working as i want..so another problem..how can i stop the scenario after remove the final child instead of stuck in the final child location.
NIRO17 NIRO17

2016/5/1

#
and also i want to know how to zombie(actor) avoid an obstacle and continue it's moving to the parent or child class however.
danpost danpost

2016/5/1

#
NIRO17 wrote...
thank you very much..it's working as i want..so another problem..how can i stop the scenario after remove the final child instead of stuck in the final child location.
In world class act method, add the following:
if (getObjects(child.class).isEmpty()) Greenfoot.stop();
danpost danpost

2016/5/1

#
NIRO17 wrote...
and also i want to know how to zombie(actor) avoid an obstacle and continue it's moving to the parent or child class however.
That will take a lot more work on the zombie movement code. Try to figure something out Add fields to track specific conditions and use them to vary the movement until some other condition changes. Then, return to normal movement.
NIRO17 NIRO17

2016/5/1

#
sorry danpost I didn't get it.could you please give me an example how to avoid an obstacle in my code. obstacle avoid is the mst important part in my simiulator..
NIRO17 NIRO17

2016/5/1

#
i have make a Barrel class in the world as i did to the parent class.and then i made a method to zombie to avoid barrel and go to the parent and child class and call that method in zombie act class..but it din't work.zombie does not avoid the barrel.it derectly go to the parent and child.here is my zombie class.

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
{
    /**
     * 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()

    {
        moveAround();
        if (getWorld().getObjects(parent.class).isEmpty())
        {
            chaseChild();
        }
        else
        {
            chaseParent();
        }
            moveAround();
    }    
    public void chaseParent(){ 
        turnTowards(parent.parentX, parent.parentY);
        Actor parent=getOneIntersectingObject(parent.class);
        getWorld().removeObject(parent);

        move(2);
    }

    public void chaseChild(){ 

        turnTowards(child.childX, child.childY);
        Actor child=getOneIntersectingObject(child.class);
        getWorld().removeObject(child);

        move(2);
    }

    public void moveAround(){
        int speed = 2; 
        move(speed);
        if (getOneIntersectingObject(Barrel.class) != null) move(-speed);
        if(Greenfoot.getRandomNumber(100)<10){
            turn(Greenfoot.getRandomNumber(180)-90);
        }

    }
}

NIRO17 NIRO17

2016/5/1

#
if there is an obstacle(barrel) when zombie moving to the target directly(parent and child), i want to avoid the obstacle and continue movement to the target.
danpost danpost

2016/5/1

#
Add an instance field:
private boolean sidestepping;
set it to true and turn when an obstacle is found. Put the movement code in an 'if' block with the condition that sidestepping is not true. I summary:
if not sidestepping
{
    normal movement
}
else
{
    sidestep
    set sidestepping to false
}
You could add an instance counter field to increase the number of side steps taken before resuming normal movement.
NIRO17 NIRO17

2016/5/1

#
in my zombie where i have to put this code??
There are more replies on the next page.
1
2