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

2013/12/5

Avoider Game: method

1
2
w9ndel w9ndel

2013/12/5

#
I am working on a school assignment, where I am making the game called Avoider. As I've never worked with Greenfoot before, I am following this tutorial: http://users.csc.calpoly.edu/~mhaungs/projects/AvoiderGame/AvoiderTut1.html Though I can't seem to figure out what I am doing wrong. In the tutorial it says: "Remove our hero from the game if it touches one of the enemies. A prerequisite to this tutorial is that you have completed these short Greenfoot Tutorials (http://www.greenfoot.org/doc/tut-3). The third one tells you how to do what I'm asking you to do here. However, instead of using the method getOneObjectAtOffset() use the method getOneIntersectingObject();" I've got no clue what that means. This is how my coding looks like:
danpost danpost

2013/12/5

#
Please copy/paste your coding into the input box supplied by the 'code' tag below the 'Post a reply' box.
w9ndel w9ndel

2013/12/5

#
   public void eat()
    {
        Actor avatar;
        avatar = getOneIntersectingObject(0, 0, Avatar.class);
        {
            World world;
            world = getWorld();
            world.removeObject(Avatar);
        }
    }
danpost danpost

2013/12/5

#
The problem is not the coding of the method; it is where you placed it. Move the method from the world class to your hero class.
w9ndel w9ndel

2013/12/5

#
Thanks for the quick reply, danpost. Though it still doesn't work. http://img546.imageshack.us/img546/6527/h3vn.png
w9ndel w9ndel

2013/12/5

#
A new problem has occured. i tried mixing the tutorial with another tutorial.
    public void act() 
    {
        setLocation(getX(), getY() + 1);
        
        Actor avatar = getOneIntersectingObject( Avatar.class );
        if (avatar == null) {
           return false;
        } else {
            return true;
        }
           
    }
When I try to compile it, an error occurs saying "cannot return a value from method whose result type is void"
shrucis1 shrucis1

2013/12/6

#
w9ndel, the act method is a method with a void return type, as is specified by public void act() { ... } This means that you don't return a value. if what you want to do is create a method that returns true if you're touching Avatar.class, then you would create it like this:
public void act()   
{  
    setLocation(getX(), getY() + 1);  
    if(amITouchingAvatar()) {
        //Do stuff
    }
}

public boolean amITouchingAvatar() {
    Actor avatar = getOneIntersectingObject( Avatar.class );  
    if (avatar == null) {  
       return false;  
    } else {  
        return true;  
    }  
         
}
The method amITouchingAvatar() here returns a true or false value, whether or not it is touching Avatar.class. The reason you can return true; or return false; here is because the method is a boolean result type (true or false value). However, this application is not useful, because you can use the isTouching(Avatar.class) method built into Greenfoot. If you need more help with methods and return types, you might want to look for a tutorial on them. I found a pretty basic one here: http://www.homeandlearn.co.uk/java/java_methods.html Hope this helps you.
w9ndel w9ndel

2013/12/7

#
Hi shrucis1 Thanks for the help. It seems that the code you sent is working, but it was supposed to make the enemies eat the hero (whenever the avatar/hero touched an enemy, the hero should be destroyed/get eaten by the enemies) In the tutorial it says:
Tutorial wrote...
Remove our hero from the game if it touches one of the enemies. A prerequisite to this tutorial is that you have completed these short Greenfoot Tutorials. The third one tells you how to do what I'm asking you to do here. However, instead of using the method getOneObjectAtOffset() use the method getOneIntersectingObject();
The method getOneIntersectingObject(); works in the codes you've sent, but the hero doesn't get removed.
w9ndel w9ndel

2013/12/7

#
I figured out what the problem was. - thank you for the help :-) Though I've bumped into another problem. It says in the tutorial:
Hmmm. There's a part missing....curious! Oh well, why don't you lookup the documentation for the Greenfoot class and see if you find a method that will work. (yup, fill in the blank!)
I guess I'm going to use the Greenfoot.stop(go); - but when I compile this code, the game suddenly ends. Link to the tutorial
w9ndel w9ndel

2013/12/7

#
Sorry, it's not the Greenfoot.stop(); method. I've written Greenfoot.setWorld(go);, but the game suddenly ends after a few seconds
w9ndel w9ndel

2013/12/7

#
Why doesn't setColor(Color.gray) work? The tutorial tells us to write that, but it doesn't work when I type the exact same thing. Screenshot of the tutorial and my coding.
public class ScoreBoard extends Actor
{
    private GreenfootImage board;
    private GreenfootImage sb;
    private int score = 0;
    private String label = "Score: ";
    
    public ScoreBoard()
    {
        super();
        
        int boardWidth = 150;
        int boardHeight = 30;
        int boardTransparency = 125;
        
        // Draw translucent background
        board = new GreenfootImage(boardWidth, boardHeight);
        board.setColor(Color.gray);
        board.setTransparency(boardTransparency);
        board.fillRect(0, 0, boardWidth, boardHeight);
        this.setImage(board);
        
        //Draw scoreboard
        update();
    }    
    
    /**
     * Act - do whatever the ScoreBoard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // The scoreboard has no independent actions.
    }    
    
    public int getScore()
    {
        return score;
    }
    
    public void addScore (int pts)
    {
        score += pts;
        update();
    }
    
    /**
     * update - redraw the scoreboard using current score.
     */
    
    private void update()
    {
        //Reuse scoreboard background and then draw "text" portion into it.
        sb = new GreenfootImage(board);
        sb.drawImage(new GreenfootImage(label + score, 18, Color.WHITE, new Color(0,0,0,0)),25,5);
        this.setImage(sb);
    }    
}
shrucis1 shrucis1

2013/12/7

#
Glad I could help, w9ndel, and, as for this bug, it seems that it doesn't recognize the Color class. Color isn't imported automatically, so you need to do it. At the beginning of your code, you should see something like this:
import greenfoot.*;
What this does is it tells java to get the class known as Greenfoot so you can use its methods in your code. Whenever you use Greenfoot.start(), Greenfoot.stop(), Greenfoot.setWorld(World world), or any other method where you type 'Greenfoot.', you're telling the program to use that Greenfoot class you imported at the beginning. Java comes with many of these classes for you to use, you just have to tell it to import them. In the case of Color, try adding this to the beginning of your code, right after 'import greenfoot.*;'
import java.awt.Color;
Once you have this, it should recognize the Color class because you imported it. another method of doing this would be like so:
board.setColor(java.awt.Color.gray);
It accomplishes the same thing, without needing to have the import at the top. Although I prefer the former method, it's up to you which to use, as I don't really believe it makes a difference.* *I don't know for sure whether it does or does not make a difference, as I'm not an expert in java imports
w9ndel w9ndel

2013/12/8

#
Thank you! The color of the scoreboard now works perfect. Though I don't really know what to type in this code: Right now, I've written...
    public void endGame()
    {
        AvoiderGameOverScreen go = new AvoiderGameOverScreen();
        Greenfoot.setWorld(go);
    
    }
... but the game ends after a couple of seconds. What do I do? I've uploaded my scenario in case that'd be easier to help, if you see all my classes.
http://speedy.sh/HymmK/AvoiderGame.zip
danpost danpost

2013/12/8

#
w9ndel wrote...
I've uploaded my scenario in case that'd be easier to help, if you see all my classes.
http://speedy.sh/HymmK/AvoiderGame.zip
Why don't you upload it on this site checking the 'Publish source code' checkbox' to make it easier for us?
shrucis1 shrucis1

2013/12/8

#
w9ndel, I cannot open your source code using that downloader, as it requires me to install software I'd rather not have on this computer. What danpost suggests is the easiest way for us to get the source code and help. The bug you have with the game ending in only a few seconds likely isn't because of your endGame() method, it's probably when you call the method.
There are more replies on the next page.
1
2