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

2014/12/5

How to Display Image for GameOver/YouWin

1
2
3
NikZ NikZ

2014/12/9

#
Anne2403 wrote...
uhmm @danpost sorry If I am always asking for help.. I already change the previous code.. I made a world class called 'Winner' and this is my code for it...
import greenfoot.*;

public class Winner extends World

public Winner()
{
super(990,790,1)
}
public void act()
{
if(getObjects(Enemies.class).isEmpty())
{
Greenfoot.setWorld(new Winner());
}
but the thing that I am confusing about is.. how am I gonna able to make that code excute?? .
You haven't created any enemies in Winner, so the World will be set to Winner every act. That will be laggy. You want Winner to be the current World? Use Greenfoot.setWorld(new Winner()); wherever you want to set it that, like in your lightning class.
NikZ NikZ

2014/12/9

#
Don't use getWorld(), that returns a World, it doesn't set one. As for returning, you have to return a list.
danpost danpost

2014/12/9

#
I believe that the code in the act method shown in your 'Winner' world class should be in your 'Space' world class. It is the Space world that, when no enemies remain in it, should cause the Winner class will be instantiated.
Anne2403 Anne2403

2014/12/9

#
okay @Nikz thank u. and oh by the way the 'enemies' is the class of the animals.. Like this.. public class Snake extends Enemies.. so I thought if I put Enemies in that code the other enemies will execute without me putting the code that I wanted to in one of each enemy class. . Thanks.
Anne2403 Anne2403

2014/12/9

#
okay @danpost. I will do that.
Anne2403 Anne2403

2014/12/9

#
yey! it worked,. ;ast problem @danpost.. hihi.. I've been trying different ways to put the 'removeObject' in different class or method.. there's no syntax error but it work in the wrong way.. What I wanted to happen is after the Lightning and fire was shooted by the enemies or the cat it will remove on the world and will NOT stuck in the upward and down word side of the world.. I wanted to remove them after it has shooted by an actor... Where class or method should I put to make this happen and what is the right code for it.. is it.. getWorld(),removeObjects(this); or getWorld().removeObjects(Lightning/Fire); -----> everytime I tried this it will got an error saying, cannot find symbol/ method.. Please help me on this..
danpost danpost

2014/12/9

#
I think what you are trying to say is you have two conditions that will remove the Lightning and Fire objects -- one is the world edge collision and the other is a actor hit collision. You can combine these into one compound 'if' statement using a calls to methods that return boolean values which are the results of each condition. Like for world edge collision:
private boolean atWorldEdge()
{
    return getX() == 0 ||
           getX() == getWorld().getWidth()-1 ||
           getY() == 0 ||
           getY() == getWorld().getHeight()-1;
}
It returns a true value when at world edge. Another method can be written for actor intersection that returns a true/false value as well. The combined effect would result to:
if (atWorldEdge() || killsIntersector()) getWorld().removeObject(this);
This, or something like it, should be the last line in your act method. The intersector killed can be removed from the world within the 'killsIntersector' method before returning the true/false value.
NikZ NikZ

2014/12/9

#
Isn't there a isAtEdge() method?
danpost danpost

2014/12/9

#
NikZ wrote...
Isn't there a isAtEdge() method?
There is! But, not everyone is running the latest version of greenfoot (I am still on v2.3.0).
Anne2403 Anne2403

2014/12/10

#
@danpost I put the code that you gave to me in my Lightning class.. and I follow your instructions.. however i got an error saying "'void' typer not allowed here" and there red on "()" atWorldEdge..
danpost danpost

2014/12/10

#
You will have to show the class code and the complete error message given.
Anne2403 Anne2403

2014/12/10

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.lang.Object;
/**
 * Write a description of class Lightning here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lightning extends Actor
{
    /**
     * Act - do whatever the Lightning wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private Cat meow;
    
    public void act() 
    {
        int y = getY();
        y = y-5;
        setLocation(getX(),y);
        setRotation(getRotation());
        Kill();
        if (atWorldEdge() || Kill()){
             getWorld().removeObject(this); 
        }
}
    public void Kill()
    {
    Actor Enemies = getOneObjectAtOffset(0,0, Enemies.class);
        if(Enemies != null){ 
            World world = getWorld();
            getWorld().removeObject(Enemies);
    } 
}
public boolean atWorldEdge()  
{  
    return getX() == 0 ||  
           getX() == getWorld().getWidth()-1 ||  
           getY() == 0 ||  
           getY() == getWorld().getHeight()-1;  
}  
}
and the error message given.. 'void' type not allowed here---()
NikZ NikZ

2014/12/10

#
Line 25 you write Kill() and Java is trying to make it return a boolean, but it can't, it's a void.
NikZ NikZ

2014/12/10

#
Line 33 is not necessary, you don't use world and getWorld() doesn't perform anything.
danpost danpost

2014/12/10

#
The 'java.lang.Object' class never needs to be explicitly imported. Remove line 3. You are not creating or declaring any variable or field to hold a List object. Remove line 2, also. Line 23 is saying 'please turn to the direction you are facing'. Remove line 23. Line 24 will be executed within line 25. Remove line 24. The 'Kill' method (this deals with your error) needs to return a true/false value indicating whether a kill was made or not and the return statements need to be added to it to return the appropriate true/false values. Change the method to:
public boolean Kill()
{
    Actor enemy = getOneObjectAtOffset(0, 0, Enemies.class);
    if (enemy != null)
    {
        getWorld().removeObject(enemy);
        return true;
    }
    return false;
}
Avoid using a Class name for a field or variable. Also, fields, variables and methods should, by convention, begin with a lowercase letter. So, your 'Kill' method should be a 'kill' method.
There are more replies on the next page.
1
2
3