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

2016/12/7

Adding Objects With an Actor

1
2
Wasupmacuz Wasupmacuz

2016/12/7

#
I need help using finding a way so that if an actor (let's call it "hero") touches an enemy then it will create my GameOver actor in the middle of the screen. I've tried multiple things but I'm very new to java and haven't found a way yet.
danpost danpost

2016/12/7

#
Wasupmacuz wrote...
I need help using finding a way so that if an actor (let's call it "hero") touches an enemy then it will create my GameOver actor in the middle of the screen. I've tried multiple things but I'm very new to java and haven't found a way yet.
What have you tried? Show what you tried and tell us where you tried it (show code tried and indicate what class and method you placed the code in).
Wasupmacuz Wasupmacuz

2016/12/8

#
danpost wrote...
Wasupmacuz wrote...
I need help using finding a way so that if an actor (let's call it "hero") touches an enemy then it will create my GameOver actor in the middle of the screen. I've tried multiple things but I'm very new to java and haven't found a way yet.
What have you tried? Show what you tried and tell us where you tried it (show code tried and indicate what class and method you placed the code in).
well, I deleted all the code but I tried doing it with just plain public booleans. and then figured that would never work so right now I'm trying with static variables. This is my world code. The touching variable is what I'm trying to get to work.
import java.util.*;
import greenfoot.*;
import java.awt.Color;

/**
 * 
 */
public class Beach extends World
{
    public static boolean touching;
    /**
     * Constructor for objects of class MyWorld.
     */
    public Beach()
    {
        super(1000, 600, 1);
        addObject (new Turtle(),500,550);
        addObject (new Seagull(),10,50);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Crab crab = new Crab();
        addObject(crab,18,266);
        Crab crab2 = new Crab();
        addObject(crab2,22,359);
        Crab crab3 = new Crab();
        addObject(crab3,575,427);
        Crab crab4 = new Crab();
        addObject(crab4,981,321);
        crab3.setLocation(978,460);
        Crab crab5 = new Crab();
        addObject(crab5,25,558);
    }
    public void act()
    {
        if (touching==true)
        {
            addObject(new GameOver(),500,300);
        }
    }
}
This is my class that I wanted to use to tell the world to make the GameOver actor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 
 */
public class Turtle extends Actor
{
    public Turtle()
    {
        setRotation(270);
    }
    /**
     * Act - do whatever the Turtle wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move(1);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(1);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move(1);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(1);
        }
        if (Greenfoot.isKeyDown("right") & (Greenfoot.isKeyDown("up")))
        {
            setRotation(315);
        }
        if (Greenfoot.isKeyDown("right") & (Greenfoot.isKeyDown("down")))
        {
            setRotation(45);
        }
        if (Greenfoot.isKeyDown("left") & (Greenfoot.isKeyDown("up")))
        {
            setRotation(225);
        }
        if (Greenfoot.isKeyDown("left") & (Greenfoot.isKeyDown("down")))
        {
            setRotation(135);
        }
        if (isAtEdge()==true)
        {
            turn(180);
            move(15);
        }
        if (isTouching(Seagull.class) || isTouching(Crab.class))
        {
            Beach.getInstance.touching=true;
        }
    }
}
danpost danpost

2016/12/8

#
Line 60 is outrageous. 'getInstance' does not have any parenthesis after is, so it is treated as a field (a static field because a Class name is used to refer to it) which does not exist. There also is no 'getInstance' method in the Beach, Actor or Object class; so, even as a method, it will not be found. If all you are doing is adding a Game Over object into the world when either condition on line 58 is true, then add one into the world. You do not have to go to the Beach class to do that. Remove the 'touching' field from the Beach class and all codes dealing with it (including the act method in the Beach class). Change line 60 to:
getWorld().addObject(new GameOver(),500,300);
You may want to follow that line with:
Greenfoot.stop();
Nosson1459 Nosson1459

2016/12/8

#
In beach I think just do:
public boolean touching=false;
Then put this instead of lines 58 through 61 of Turtle:
if (isTouching(Seagull.class) || isTouching(Crab.class))
{
    getWorld().touching=true;
}
In beach add in
Greenfoot.stop();
by line 45 under the addObject. danpost beat me to it!
Wasupmacuz Wasupmacuz

2016/12/8

#
danpost wrote...
Line 60 is outrageous. 'getInstance' does not have any parenthesis after is, so it is treated as a field (a static field because a Class name is used to refer to it) which does not exist. There also is no 'getInstance' method in the Beach, Actor or Object class; so, even as a method, it will not be found. If all you are doing is adding a Game Over object into the world when either condition on line 58 is true, then add one into the world. You do not have to go to the Beach class to do that. Remove the 'touching' field from the Beach class and all codes dealing with it (including the act method in the Beach class). Change line 60 to:
getWorld().addObject(new GameOver(),500,300);
You may want to follow that line with:
Greenfoot.stop();
Oh yeah, my bad. I just kinda threw line 60 out there to see if it worked. Did I mention that I'm very new to java? Thanks guys!
Nosson1459 Nosson1459

2016/12/8

#
First post:
Wasupmacuz wrote...
I've tried multiple things but I'm very new to java and haven't found a way yet.
Last post:
Wasupmacuz wrote...
Did I mention that I'm very new to java? Thanks guys!
Yes you mentioned it. Glad to be of assistance (but somebody else was first).
Nosson1459 Nosson1459

2016/12/8

#
To Wasupmacuz, thanks for following me (What does following me do to us? (I don't mean you should stop!)).
danpost danpost

2016/12/8

#
@Nosson1459, when member A follows member B, member A is considered a "fan" of member B and is counted in the "fans" count on the members page; then, member A will be notified whenever member B uploads or updates a scenario. Sidenote: line 3 of your given code above will not work. The 'getWorld' method returns a World type object and the boolean field is not in the World or Object class. It is in a subclass of World and the compiler will not look there for that field with that code. You will get a 'Cannot find symbol -- variable touching' (or something like that).
Nosson1459 Nosson1459

2016/12/8

#
danpost wrote...
Sidenote: line 3 of your given code above will not work. The 'getWorld' method returns a World type object and the boolean field is not in the World or Object class. It is in a subclass of World and the compiler will not look there for that field with that code. You will get a 'Cannot find symbol -- variable touching' (or something like that).
Rushing mistake. Originally, I put
Beach beach=(Beach)getWorld();
in the act then MY line 3 (line 60) as
beach.touching=true;
but I edited it after I posted it because I thought that if you could do what you did (I looked at it too fast to see exactly what you did) then you'll tell Wasupmacuz not to do what I did because you could just use getWorld(), but he can't as you just told me.
HungryHide HungryHide

2016/12/8

#
@danpost, @Nosson1459 when I used that code in my second world, it made it so the "Game Over" text doesn't appear every time I die. Any suggestions on what should happen?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fish here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fish extends Actor
{
    /**
     * Act - do whatever the Fish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeypress(); 
        move(2);
         if(isTouching(Hand.class))
        {
          World myWorld2 = getWorld();
          World myWorld = getWorld();
          GameOver gameover = new GameOver();
          myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
          myWorld2.addObject(gameover, myWorld2.getWidth()/2, myWorld2.getHeight()/2);
          myWorld.removeObject(this);
          Greenfoot.stop();
        }
    }
     /**
      * Checks to see if command is given. 
      */
      public void checkKeypress()
      {
        if (Greenfoot.isKeyDown("down"))
        { 
            turn(4);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            turn(-4);
        }
      }
   }
    
danpost danpost

2016/12/8

#
When fish touches hand, your code will place one GameOver actor in the world. Both lines 21 and 22 are assigned the same world and lines 24 and 25 adds the same actor into the same world at the same place. Lines 21 through 27 can be simplified to:
World world = getWorld();
world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
world.removeObject(this);
Greenfoot.stop();
HungryHide wrote...
when I used that code in my second world, it made it so the "Game Over" text doesn't appear every time I die
What is the code to your second world?
HungryHide HungryHide

2016/12/8

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

/**
 * Write a description of class MyWorld2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld2 extends World
{
   private int timer;
    /**
     * Constructor for objects of class MyWorld2.
     * 
     */
    public MyWorld2()
    {    
        super(600, 400, 1);
        prepare(); 
        
    }
    /**
     * Type description here
     */
    private void prepare()
    {
        Fish fish = new Fish();
        addObject(fish, 100, 400);
        Hand hand = new Hand();
        for(int i = 0; i < 6; i++) 
        {
            int x = Greenfoot.getRandomNumber(getWidth()/2); 
            int y = Greenfoot.getRandomNumber(getHeight()/2);
            addObject(new Hand(), x, y); 
        }
    }
    public void setTimer(int value)
    {
        timer=value; 
    }
    public void act()
    {
        int aps = 28;
        if (++timer%aps == 0)showText("Score:  "+(timer/aps), 125, 40);
    }
}
danpost danpost

2016/12/8

#
You can remove line 29 (that hand is not being used anywhere). Nothing in these two classes appear to be able to cause what you say is happening. What code do you have for the 'Hand' class?
HungryHide HungryHide

2016/12/8

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

/**
 * Write a description of class Hand here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hand extends Actor
{
  /**
   * 
   */
  public Hand()
    {
        GreenfootImage image = getImage();  
        image.scale(50, 50);
        setImage(image);
    }
  /**
     * Act - do whatever the Hand wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turnAtEdge();
        randomTurn();
        move(1);
        lookForFish();
    }
  
  /** 
   * Describe method here
   */
   public void randomTurn()
   {
       if (Greenfoot.getRandomNumber(100)>90) 
       {
           turn(Greenfoot.getRandomNumber(101)-50);
       }
   }
  /**
  * Describe method here
  */
   public void turnAtEdge()
    { 
        if(isAtEdge())
        {
            turn(20);
        }
    }
    
  /**
  * Describe Method Here
  */ 
  public void lookForFish()
   { 
       if (isTouching(Fish.class))
       {
           removeTouching(Fish.class);
           Greenfoot.stop(); 
       } 
   } 
}
There are more replies on the next page.
1
2