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

2012/5/19

WANTED! : Good coder

h123n h123n

2012/5/19

#
Can someone help me to make a method where all bears turn to face a pig? in case you want to know here is my code
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()
 {
     //no code for bear... yet.
    }
}
//and for the tree(thats the player, yes I know it is weird)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class tree here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class tree extends Actor
{
    /**
     * Act - do whatever the tree wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
      private int PigsAdded = 0; 
    public void act() 
    {
        if(Greenfoot.mouseClicked(this)) {
            World world;
            world = getWorld();
            world.addObject (new bear(), getX(), getY());
            PigPlaces();
    }   
    if(Greenfoot.isKeyDown("space"))
    move(3);
    if(Greenfoot.isKeyDown("right"))
    turn(5);
    if(Greenfoot.isKeyDown("left"))
    turn(-5);
}
public void PigPlaces()
{
if( (PigsAdded ==0)||(PigsAdded==1))
{
World world;
world = getWorld();
world.addObject(new poorpig(),400,400);
PigsAdded++;
}
if(( PigsAdded ==2)||(PigsAdded==3))
{
World world;
world = getWorld();
world.addObject(new poorpig(),200,200);
PigsAdded++;
}
}
}
steved steved

2012/5/19

#
Ok this is simple. In the bear class when you want them to turn to a pig just make a method and put this code inside.
Pig pig = getWorld().getObjects(Pig.class).get(0);
turnTowards(Pig.getX(), Pig.getY());
Im also fairly new to this so im not sure how this will work if there is more then one pig in the world. Cant wait to see this game - it looks odd :D but i guess it can be any weirder then my eat the pie games.
h123n h123n

2012/5/19

#
Oh by the way I loved your pie games!!! :D
h123n h123n

2012/5/19

#
It says can not find Pig.class how do I fix this?
steved steved

2012/5/19

#
Thanks :D. If you have any suggestions fell free to post them, but I wont be online for a while - I'm traveling.
steved steved

2012/5/19

#
Let me show you what the code that makes the red guys folow the pies is:
        if (Greenfoot.getRandomNumber(100) < 2 && !getWorld().getObjects(Pie.class).isEmpty())
        {
            Pie pie = (Pie) getWorld().getObjects(Pie.class).get(0);
            turnTowards(pie.getX(), pie.getY());
        }
Try making it into a statement like this: if (!getWorld().get(Objects(Pig.class).isEmpty()) { // the code } The if statement checks to see wether or not the pig exists I think. If this doesnt work you will need someone like danpost or davmac to help you. }
steved steved

2012/5/19

#
also this will nly work if your class is Pig with a capitol, java is case sensitive!
h123n h123n

2012/5/19

#
just in case you need to know here is code for pig ps it still doesn't know what Pig class is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class poorpig here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class poorpig extends Actor
{
    /**
     * Act - do whatever the poorpig 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.
    }    
}
steved steved

2012/5/19

#
You need to change anything that says Pig to poorpig. you were using the wrong class name.
h123n h123n

2012/5/19

#
I know BUT it still says it wasn't expecting things can you possibly try it out?
steved steved

2012/5/19

#
I hope I've helped but im going to lose wifi soon. Ill keep reloading this page to see if you reply untill I lose conection. Don't forget to comment on eat the pie with any suggestions :D. BTW I think its amazing that your programming java at age 10 - im 13 and I didnt start any programming till I was 11 and that was liberty BASIC which is a fairly easy language.
h123n h123n

2012/5/19

#
I started basic programming when I was only7 I used alice and when I was 8 I knew all about variables, methods and events. It was my friend who got me into greenfoot
danpost danpost

2012/5/19

#
Thinking it through logically, to turn toward the closest pig, this is what needs done: 1) find the closest pig by starting at distance of 1 and incrementing distance until pigs found in range or out of range by this pseudo-code:
set range to 1;
while (not out of range AND not pigs in range) increment range;
2) upon exiting the loop, pigs in range List will either be empty or ... not; close with this pseudo-code:
if (not pigs in range is empty) 
{
    set closest pig to random pig from pigs in range;
    turn towards the point (closest pig's x-coordinate, closest pig's y-coordinate);
}
All this would be done in the bear class.
You need to login to post a reply.