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

2012/3/11

power ups?

5
6
7
8
9
danpost danpost

2014/4/21

#
You probably change something in your world constructor or a method that is used during the running of it that is causing the system to hang up (probably an infinite loop). You will have to troubleshoot to find the cause or post your world class and ask for help.
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
You probably change something in your world constructor or a method that is used during the running of it that is causing the system to hang up (probably an infinite loop). You will have to troubleshoot to find the cause or post your world class and ask for help.
ok, i posted a new discussion and my world class, just hope someone can help...anyways did you answer my question on enemies spawning in mutliplayer mode and the problem that i got?
danpost danpost

2014/4/21

#
You should be able to determine and limit where it respawns by its current location. That is, if on right side of world, respawn (re-locate) on right; if on left side of world, respawn (re-locate) on left.
int halfWorldWidth = getWorld().getWidth()/2;
int x = Greenfoot.getRandomNumber(halfWorldWidth);
if (getX() >= halfWorldWidth) x += halfWorldWidth;
setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight());
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
You should be able to determine and limit where it respawns by its current location. That is, if on right side of world, respawn (re-locate) on right; if on left side of world, respawn (re-locate) on left.
int halfWorldWidth = getWorld().getWidth()/2;
int x = Greenfoot.getRandomNumber(halfWorldWidth);
if (getX() >= halfWorldWidth) x += halfWorldWidth;
setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight());
i input the code and it came up with an error nullpointer exception, the first time i tried it on one type of the enemies, it didn't work but i added an extra bracket at the end and it compiled. I went to try it out and as soon as my iceball hit the bug enemy, it came up with the error. Here is the code for the Iceball
import greenfoot.*;  

public class Iceball extends Actor
{
    private int direction, speed;

    public Iceball(int dir)
    {
        direction = 270;
        speed = 7;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            MyGame2 mygame2= (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = getWorld().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight()))   ;  
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }
    }   
}    
danpost danpost

2014/4/21

#
Remove line 38. If you add the code to any other collision blocks, you will have to do the same -- that is, remove the 'world.removeObject(this)' line. When you are done, repost the new code for the class. I may be able to simply it to something that is more understandable.
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
Remove line 38. If you add the code to any other collision blocks, you will have to do the same -- that is, remove the 'world.removeObject(this)' line. When you are done, repost the new code for the class. I may be able to simply it to something that is more understandable.
well i did delete line 38 but it's doesn't respawn the actual enemy but more iceballs shoot up for some reason, here is the new code...
import greenfoot.*;  

public class Iceball extends Actor
{
    private int direction, speed;

    public Iceball(int dir)
    {
        direction = 270;
        speed = 7;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            MyGame2 mygame2= (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            int halfWorldWidth = getWorld().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight()))   ;  
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            int halfWorldWidth = getWorld().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight()));
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            int halfWorldWidth = getWorld().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            setLocation(x, Greenfoot.getRandomNumber(getWorld().getHeight()));
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }
    }   
}    
danpost danpost

2014/4/21

#
I guess I was not clear on what you were trying to do there. Put line 38 back in and remove line 34 and then change line 41 to 'BugEnemy.setLocation...'.
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
I guess I was not clear on what you were trying to do there. Put line 38 back in and remove line 34 and then change line 41 to 'BugEnemy.setLocation...'.
i did that but now a terminal window comes up with the same message "java.lang.NullPointerException"
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
I guess I was not clear on what you were trying to do there. Put line 38 back in and remove line 34 and then change line 41 to 'BugEnemy.setLocation...'.
it states at iceball.disappear(Iceball.java:38) at Iceball.act(Iceball.java:17) 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)
danpost danpost

2014/4/21

#
Change 'getWorld()' on line 38 to 'world'. Same with line 41. These line numbers may not match up with your code due to recent changes in your code; but, I think they are correct from what I can tell from the error message.
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Change 'getWorld()' on line 38 to 'world'. Same with line 41. These line numbers may not match up with your code due to recent changes in your code; but, I think they are correct from what I can tell from the error message.
now it says cannot find symbol - method world() here is the edited code...
import greenfoot.*;  

public class Iceball extends Actor
{
    private int direction, speed;

    public Iceball(int dir)
    {
        direction = 270;
        speed = 7;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame2 mygame2= (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            BugEnemy.setLocation(x, Greenfoot.getRandomNumber(world().getHeight()))   ;  
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            InsectEnemy.setLocation(x, Greenfoot.getRandomNumber(world().getHeight()));
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world().getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            Bee.setLocation(x, Greenfoot.getRandomNumber(world().getHeight()));
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }
    }   
}    
danpost danpost

2014/4/22

#
Change all occurrences of 'world()' to 'world' (remove the parenthesis).
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Change all occurrences of 'world()' to 'world' (remove the parenthesis).
i did that but it says "Actor not in world. An attempt was made to use the actor's location while it is not in the world..." here is the new code...
import greenfoot.*;  

public class Iceball extends Actor
{
    private int direction, speed;

    public Iceball(int dir)
    {
        direction = 270;
        speed = 7;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame2 mygame2= (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world.getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            BugEnemy.setLocation(x, Greenfoot.getRandomNumber(world.getHeight()))   ;  
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world.getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            InsectEnemy.setLocation(x, Greenfoot.getRandomNumber(world.getHeight()));
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            int halfWorldWidth = world.getWidth()/2;  
            int x = Greenfoot.getRandomNumber(halfWorldWidth);  
            if (getX() >= halfWorldWidth) x += halfWorldWidth;  
            Bee.setLocation(x, Greenfoot.getRandomNumber(world.getHeight()));
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }
    }   
}    
danpost danpost

2014/4/22

#
Move line 38 to before line 42 (before the 'return' statement).
Super_Hippo Super_Hippo

2014/4/22

#
danpost wrote...
Move line 38 to before line 42 (before the 'return' statement).
Line 37 ;) (And same with the others.)
There are more replies on the next page.
5
6
7
8
9