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

2013/1/10

can somone tell me whats wrong with this code please.

1
2
theuberjew theuberjew

2013/1/10

#
import greenfoot.*; import java.awt.Color; public class enemy extends Actor { public void act() { enemy(); move(); lookForRocket(); } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } private int size=5; public enemy(); { size=Greenfoot.getRandomNumber(274); } public void move() { setLocation(getX()+2,getY()); } public void lookForRocket() { if ( canSee(Rocket.class) ) { eat(Rocket.class); } } }
erdelf erdelf

2013/1/10

#
what is it supposed to do?
bbwf bbwf

2013/1/10

#
What is clss?
theuberjew theuberjew

2013/1/10

#
be an enemy kill the player when it touches the but it is saying illigal start of expression on all public voids
bbwf bbwf

2013/1/10

#
Is the clss supposed to be class? Thatcould be your problem.
Gevater_Tod4711 Gevater_Tod4711

2013/1/10

#
you havent closed the mehtod eat. there is a } missing
Gevater_Tod4711 Gevater_Tod4711

2013/1/10

#
and also the method public enemy() is wrong it shoult be public void enemy. @bbwf clss is just a name for a class object. Any other name would be the same.
vonmeth vonmeth

2013/1/10

#
You forgot a bracket to signal the end of the eat class, and you have a semicolon after "public enemy()". You can't call your constructor like that in your act method. Also move your private int size=5; up to the top (common convention and readability). You usually want your constructor as the first method as well (common convention and readability). Use the , around your code when posting it to the boards here. It helps with reading it.
import greenfoot.*;  
import java.awt.Color; 

public class enemy  extends Actor
{
    private int size=5; // move this up here

    public enemy() // had a semicolon here
    {
        size=Greenfoot.getRandomNumber(274);
    }
    
    public void act()
    {
        // enemy(); - you can't call your constructor
        move();
        lookForRocket();
    }

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    } // forgot this bracket   

    public void move()
    {
        setLocation(getX()+2,getY());
    }

    public void lookForRocket()
    {
        if ( canSee(Rocket.class) ) 
        {
            eat(Rocket.class);
        }
    }
}
theuberjew theuberjew

2013/1/10

#
thank you you guys are awesome
theuberjew theuberjew

2013/1/11

#
how would i make this enemy disapear when it reaches the end of the screen?
theuberjew theuberjew

2013/1/11

#
and a game over when rocket hits enemy?
Gevater_Tod4711 Gevater_Tod4711

2013/1/11

#
To remove your enemy when it reaches the world use this:
public void act() {
    //write this at the end of your act method; otherwhile there might be exceptions;
    if (atWorldEdge()) {
        getWorld().removeObject(this);
    }
}


    public boolean atWorldEdge() {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
            return true;
        }
        if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
            return true;
        }
        else {
            return false;
        }
    }
To check whether your rocket hit's an enemy use this:
//first you have to import java.util.List;

//in your rocket class;
public void act() {
    if (enemyHit()) {
        //whatever should happen if the game is over;
    }
}

public boolean enemyHit() {
    List<Enemy> enemiesInWorld = getWorld().getObjects(Enemy.class);//Enemy has to be the classname; If this is not the right classname you have to change it;
    for (Enemy enemy : enemiesInWorld) {
        if (intersects(enemy)) {
            return true;
        }
    }
    return false;
}
theuberjew theuberjew

2013/1/17

#
Thanks for the help so far can I ask what the list statement means because i think that is preventing me from compiling my code.
Gevater_Tod4711 Gevater_Tod4711

2013/1/17

#
The list is a class from the java.util API It's an interface for a list of objects from one class. That's why it has the parameter <Enemy>.
vonmeth vonmeth

2013/1/17

#
You need to import java.util.List (like how you import java.awt.Color and greenfoot.*).
There are more replies on the next page.
1
2