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

2014/1/10

limit my actors movement

1
2
3
4
5
6
7
Aaron91 Aaron91

2014/1/15

#
Thanks danpost i was able to put together a score counter wich added 100 points each 10 seconds. But now, like NickyL212, iam also trying to be able to 'upgrade' my main actor. First of the 3 upgrades is restoring my main actors health. Could you give me an idea how i can add another heart to my 'hear't class? My 'RestoreHealth' isnt in the same world as my 'heart' class if that should be relevant.
 public class RestoreHeart extends Actor
{
    
     public void restoreHealth()
    {
        Actor health;  
        health = getOneIntersectingObject(heart.class);  
        if (health != null && (Greenfoot.mouseClicked(this)))
        {  
            
              health += ????;   
            
        } 
    }
}
danpost danpost

2014/1/15

#
As I do not know how the heart objects in your scenario work, it would be impossible to have any idea how to deal with them. I do know this, however, line 11 of your code could not possibly work because 'health' is an Actor reference, not a numeric field.
Aaron91 Aaron91

2014/1/15

#
This is my heart and barrel class. and yea. i updated the method above a bit, cause i forgot that i can only do this when my score is the same or specific as a certain number. But i need to change my 'score' actor to an int. Heres the 'sort of updated', but more troublesome, method.
public void restoreHealth()
    {
        Actor health;  
        Actor score;
        health = getOneIntersectingObject(heart.class);  
        score = getOneIntersectingObject(Score.class);  
        if (health != null && (Greenfoot.mouseClicked(this)) && score >= 200)
        {  
            
              health += 1;   
            
        } 
    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class heart here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class heart extends Actor

{

  public static final boolean HORIZONTAL = false;  
  private static final int GAP = 10;  
  private boolean orientation = HORIZONTAL;  
  private GreenfootImage icon;  
  private int count;
  private boolean direction;
  private int initValue;  
  private int value;
  

  
  public heart()  
  {  
    icon = new GreenfootImage("herz.png");  
    count = 3;
    direction = false;
    value = initValue = count;  
    orientation = direction;  
    updateImage();
    
    
  } 
  
  private void updateImage()  
  {  
    int across = value;  
    int down = 1;  
    int xGap = GAP;  
    int yGap = 0;  
    if (orientation == VERTICAL)  
    {  
      across = 1;  
      down = value;  
      xGap = 0;  
      yGap = GAP;  
    }  
    int wide = icon.getWidth()*across+xGap*(across+1);  
    int high = icon.getHeight()*down+yGap*(down+1);  
    GreenfootImage base = new GreenfootImage(wide, high);  
    for (int j=0; j<down; j++) for (int i=0; i<across; i++)  
    {  
      int xOffset =xGap+i*(xGap+icon.getWidth());  
      int yOffset = yGap+j*(yGap+icon.getHeight());  
      base.drawImage(icon, xOffset, yOffset);  
    }  
    setImage(base);  
  }  
  
  public void adjustValue(int change)  
  {  
    if (value+change > initValue || value + change < 0) return;  
    value += change;  
    updateImage();  
  }  
  
  public int getValue()  
  {  
    return value;  
  }  
  
  
    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class barrel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class barrel extends Actor
{
    
     /* Act - do whatever the barrel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.*/
    
        
   public void act()
   
   {  
    if(isGeraakt())    
    {    
        removeHealth();  
        getWorld().removeObject(this);  
        return;         
           
    }    
    int x = Greenfoot.getRandomNumber(2);    
    move(-x);    
    if(atWorldEdge()) getWorld().removeObject(this);    
    
    setSpeed();
   }  
   
    public void setSpeed()
     {
     Greenfoot.setSpeed(60);  
    }
    
   public void removeHealth()
   {
     BotenUpgrade subWorld = (BotenUpgrade) getWorld();   
     heart heartObject = (heart) subWorld.getObjects(heart.class).get(0);   
     heartObject.adjustValue(-1);    
       
     if (heartObject.getValue() == 0) removeLive();  
     
   } 
  
   public boolean isGeraakt()
   {
     boolean raak = false;
     Actor r = getOneObjectAtOffset(0,0,boot.class);
     if ( r !=null)
     {
      raak = true;
     }
     return raak;
   }
       
   public boolean atWorldEdge()  
   {  
     return getX() == 0 ||   
               getX() == getWorld().getWidth() - 1 ||  
               getY() == 0 ||
               getY() == getWorld().getHeight() - 1;  
   } 
   
   public void removeLive()    
    {    
        int lives = getWorld().getObjects(heart.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(heart.class).get(lives-1));  

        if (lives != 1)  
        {  
            //
        }  
        else  
        {     
            Greenfoot.setWorld(new GameOver());
        }  
    }   
    
 }
danpost danpost

2014/1/16

#
Your 'heart' class has a method to adjust its value. To increase its value by one, use 'heart.adjustValue(1);'. The thing is that I do not believe that you want any interplay directly with the 'heart' object. The 'heart' object is an actor that shows information (how many lives you have remaining or how much health you have). It is when your character interacts with something else that causes you to want the value of your heart object to change that you change its value.
Aaron91 Aaron91

2014/1/16

#
Correct me if i misunderstood you, but what iam trying to achieve at the moment is that if i click on this 'RestoreHealth' actor that my depleted health is restored. If my main character interacts with an other actor it can already lose health, but i want to get my health back the moment i click on this 'RestoreHealth' at cost of 200 points. What you're saying is how i can change the amount of hearts i got right?
danpost danpost

2014/1/16

#
public void restoreHealth()
{
    Actor health;  
    Actor score;
    health = getOneIntersectingObject(heart.class);  
    score = getOneIntersectingObject(Score.class);  
    if (health != null && (Greenfoot.mouseClicked(this)) && score >= 200)
    {  
        health += 1;   
    } 
}
You are showing this code, which I understand is in your 'RestoreHeart' class. Lines 3 through 6 are looking for any 'health' and 'Score' objects that collide with 'this' 'RestoreHeart' object. This is was does not make sense. Your 'Score' and 'heart' objects are 'control' actors; they visually supply information to the one(s) playing the game. They do not interact with other object within the scenario. It is the conditions that occur within the playing of the game that require that the state of these 'control' objects to change (you say you already have some of this going on with your main character interacting with other actors to lose health. But what are the conditions that you need to occur to increase the health (or add a heart)...(1) the 'RestoreHeart' object must be clicked on; and (2) the 'value' field of the Score object must contain a value of 200 or more. You may also want that in order to restore a heart that the 'count' field of the 'heart' object have a value less than the 'initCount' value of 3. Therefore, the objects you need referenced are (1) the 'RestoreHeart' object; (2) the 'Score' object; and, of course, (3) the 'heart' object. The 'RestoreHeart' object will be the object whose 'act' method will be running when the click is detected on it; so, no problem there. The other two would depend on whether you have references to them in your world object and 'get' methods to access them or not. Either way, if you have only one of each, one 'heart' object and one 'Score' object) in the world, you can use the following to access them.
Score score;
heart hertz;
score = (Score)getWorld().getObjects(Score.class).get(0);
hertz = (heart)getWorld().getObjects(heart.class).get(0);
Then you can put your condition into action
if (Greenfoot.mouseClicked(this) && score.getValue() >= 200 && hertz.getValue() < 3)
Now, you already have your 'heart' and 'Score' objects referenced; so, we can simply put in the 'if' block what to do:
hertz.adjustValue(1);
score.add(-200);
Aaron91 Aaron91

2014/1/16

#
Oke ive been looking trough it and adjusted the code like you said. It was important to mention that my 'RestoreHeart' is placed in another world and i want it to be used on my main actor wich is in my BotenUpgrade world. I have commented the 'score' conditions cause i dont have a 'add' and 'getValue' in my score actor yet. But at the moment the game stops running after i opened my 'Upgrade' actor. My 'Upgrade' actor is an actor wich i click on the enter the world where my 'RestoreHeart' is in. Why does my game stop running after i hit the 'upgrade' actor. Ill show you my Upgrade (wich i think wont matter), and RestoreHeart
public class Upgrade extends Actor
{
    /**
     * Act - do whatever the Upgrade wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      if(Greenfoot.isKeyDown("u")) 
      {
          
           Greenfoot.setWorld(new MenuWorld((BotenUpgrade)getWorld()));  
         
      }
      imgResize();
    }    
    
    public void imgResize()  
    {      
        GreenfootImage image = getImage();    
        image.scale(120, 100);  
        setImage(image);  
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write h
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RestoreHeart extends Actor
{
    /**
     * Act - do whatever the RestoreHeart wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    
    public void act() 
    {
       imgResize();
       //restoreHealth();
       
    }    
    
    public void imgResize()  
    {      
        GreenfootImage image = getImage();    
        image.scale(65, 85);  
        setImage(image);  
    }
    
    public void restoreHealth()
    {
        Score score;  
        heart hertz;  
        score = (Score)getWorld().getObjects(Score.class).get(0);  
        hertz = (heart)getWorld().getObjects(heart.class).get(0);
        if (Greenfoot.mouseClicked(this) /*&& score.getValue() >= 200 */&& hertz.getValue() < 3)
        {  
            
              hertz.adjustValue(+1);  
              //score.getValue(-200);
            
        } 
    }
    
}
Aaron91 Aaron91

2014/1/17

#
Its somewhere in the line 34/35 of the 'RestoreHeart' actor, cause if i remove those lines and the end of line 36, the game works fine. :S
Aaron91 Aaron91

2014/1/17

#
This is an error i get with it.. about to trow this computer out of the window here... java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at RestoreHeart.restoreHealth(RestoreHeart.java:36) at RestoreHeart.act(RestoreHeart.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
JasonZhu JasonZhu

2014/1/17

#
Just take a break then look back at it when you feel more relieved, it usually does the trick.
danpost danpost

2014/1/17

#
It appears that the Score object is not in the world when you get to that line. When the error occurs, right click on the world, go to Methods inherited from World class and select the 'getObjects(Class cls)' method. When the pop-up shows, type in for the argument 'Score.class' and click 'OK' or press Enter. If you cannot Inspect the first element in the list then the list is empty and your score object was not found in the world.
Aaron91 Aaron91

2014/1/17

#
@JasonZhu, did that but everytime i get back at it........... @danpost, Oke iam a bit confused here. Wich world are you saying i should do this? cause even if i comment line 34 of the 'RestoreHeart' actor, the game also stops running when i hit the 'upgrade' actor. Heres the world actor if that should be relevant.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class BotenUpgrade here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BotenUpgrade extends World
{
    private static final GreenfootImage bgImage = new GreenfootImage("Backgroundje.png");
    private static final int scrollSpeed = 2;

    
    private GreenfootImage scrollingImage;
    private int scrollPosition = 2;

    // instance fields  
    private int maxBarrels = 2;   
    private int maxBomb = 0;  
    private int actCounter; 

    int speed = 60, frames;
    
    private int gameTime = 1000;
    private int timeDelay;
    private Actor timeText = new Actor(){};
    

    
    
   
    /**
     * Constructor for objects of class BotenUpgrade.
     * 
     */
    public BotenUpgrade()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(760, 760, 1); 

        GreenfootImage background = new GreenfootImage(760, 760);
        scrollingImage = getScrollingImage(760, 760);
        background.drawImage(scrollingImage, 500, 500);
        setBackground(background);

        addObject(new boot(), 350, 600);

        setBackground("Backgroundje.png");

        addHeart();


        timeText.setImage(new GreenfootImage("Meters: 1000", 30, java.awt.Color.black, null));  

        addObject(timeText, 660, 50);

        prepare();
    }

    public void act()
    {
        if(scrollSpeed > 0 && scrollPosition <= 0) {
            scrollPosition = getWidth();
        }
        if(scrollSpeed < 0 && scrollPosition >= getWidth()) {
            scrollPosition = 0;
        }
        scrollPosition -= scrollSpeed;
        paint(scrollPosition);
      
        spawnBarrel();
        spawnBomb();
        runCounters();
        runTimer();


    }

    public void spawnBarrel()
    {
        if (Greenfoot.getRandomNumber(200) == 1) 
        {
            if (getObjects(barrel.class).size() < maxBarrels) {  
                addObject(new barrel(), 750, Greenfoot.getRandomNumber(250)+500);  
            }
        }


    }

    public void spawnBomb()
    {
        if (Greenfoot.getRandomNumber(200) == 1) 
        {
            if (getObjects(Bomb.class).size() < maxBomb) {  
                addObject(new Bomb(), 750, Greenfoot.getRandomNumber(250)+500);  
            }
        }

    }

    private void runCounters()  
    {  
        actCounter++;  
        runActCounterBarrel();  
        runActCounterBomb();  
    } 

    private void runActCounterBarrel()  
    {  

        if (actCounter%8000 == 0)   
        {  
            maxBarrels++;  
            //if (maxBarrels == 9) Greenfoot.stop();  
        }   
    } 

    private void runActCounterBomb()  
    {  
        if (actCounter%20000 == 0)  maxBomb++;  
    } 

    /**
     * Paint scrolling image at given position and make sure the rest of
     * the background is also painted with the same image.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(scrollingImage, position, 0);
        bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0);
    }

    /**
     * Returns an image with the given dimensions.
     */
    private GreenfootImage getScrollingImage(int width, int height)
    {
        GreenfootImage image = new GreenfootImage(width, height);
        for(int x = 0; x < width; x += bgImage.getWidth()) {
            for(int y = 0; y < height; y += bgImage.getHeight()) {
                image.drawImage(bgImage, x, y);
            }
        }
        return image;
    } 

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void addHeart()
    {
        heart heart = new heart();
        addObject(heart, 140, 58);    
    }

    GreenfootSound music = new GreenfootSound("Wind Waker The Great Sea (Ocean) Music Extended.mp3");    
    public void started()    
    {    
        music.playLoop();    
    }   


    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    public void prepare()
    {
        Score score = new Score();
        Upgrade upgrade = new Upgrade();
        addObject(upgrade, 458, 206);
        upgrade.imgResize();
        upgrade.setLocation(453, 73);

        addObject(score, 637, 108);

        score.imgResize();
    }
    
    private void runTimer()  
    {    
        timeDelay++; 
        if (timeDelay == 50)  
        {  
            timeDelay = 0;  
            gameTime--;   
            timeText.setImage(new GreenfootImage("Meters: "+gameTime, 30, java.awt.Color.black, null)); 
            if (gameTime == 0)  
            {  
                Greenfoot.setWorld(new GameOver());
            }  
        }  
    }
}
And this is the world where my 'RestoreHeart' is placed. The moment i hit this world, the game stops.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MenuWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuWorld extends World
{
        
  
    public BotenUpgrade botenUpgrade;
    /**
     * Constructor for objects of class MenuWorld.
     * 
     */
    public MenuWorld(BotenUpgrade world) 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(760, 760, 1);
        addObject(new Menu(), 380, 380);  
        addObject(new Back(), 230, 520);  
        addObject(new Shield(), 280,280);
        addObject(new HeartUpgrade(), 280,400);
        addObject(new RestoreHeart(), 470,400);
    }  
}
danpost danpost

2014/1/18

#
Wow. Ok. Your 'heart' and 'Score' objects are in a BotenUpgrade world object and you are trying to access them from a different world (a MenuWorld object). Well, at least you have a reference to the BotenUpgrade world object in the MenuWorld object. Try changing your 'restoreHeart' method to this:
public void restoreHealth()
{
    Score score;  
    heart hertz;  
    score = (Score)botenUpgrade.getObjects(Score.class).get(0);  
    hertz = (heart)botenUpgrade.getObjects(heart.class).get(0);
    if (Greenfoot.mouseClicked(this) && score.getValue() >= 200 && hertz.getValue() < 3)
    {  
         hertz.adjustValue(+1);  
         score.getValue(-200);            
    }
}
Aaron91 Aaron91

2014/1/18

#
Yea but iam kind of stuck at this part. When i tried changing it to what you suggested it says; non-static method getObjects(java.lang.Class) cannot be referenced from a static context?
danpost danpost

2014/1/18

#
That just means you will have to put methods in the BotenUpgrade world class to return the score and heart objects. In the BotenUpgrade class code, add the following methods:
public Score getScoreObject()
{
    return (Score)getObjects(Score.class).get(0);
}

public heart getHeartObject()
{
    return (heart)getObjects(heart.class).get(0);
}
and call them from the MenuWorld class restoreHealth method:
public void restoreHealth()
{
    Score score = botenUpgrade.getScoreObject();
    heart hertz = botenUpgrade.getHeartObject();
    // rest of method
}
Beware, these 'get' methods will fail if you do not have these objects in the BotenUpgrade world.
There are more replies on the next page.
1
2
3
4
5
6
7