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

2013/2/15

Dissapear on edge error!?!?!?!!? PLZ HELP ME

1
2
Eli.A Eli.A

2013/2/15

#
I'm trying to make a simple shooting game. The basic idea is that I have a paddle on the left side of the screen that can shoot when I press space, and there are balloons starting on the right side that move across the screen slowly. The balloons get removed when they touch the bullet, and the bullet does too. What I'm trying to do is make it so that if the bullet does not hit a balloon and touches the edge, it goes away. I've looked all over the place and actually did find something but when I plugged it in, Greenfoot is saying the method does not exist. My code for the bullet is attached. Ask if you need code for other parts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
{
        move(5);
        destroy();
         
}
        public void destroy()
{
  Actor ballon;
  ballon = getOneObjectAtOffset(0,0, Ballon.class);
  if (ballon != null)
    {
    World world;
    world = getWorld();
    world.removeObject(ballon);
    world.removeObject(this);
  }
//Everything works but this. GreenFoot is saying that "(this.atWorldEdge()))" is not a method
if  (this.atWorldEdge())
      World world;
      world = getWorld();
      world.removeObject(this);
    
}
}
}
Gingervitis Gingervitis

2013/2/15

#
remove 'this.' at line 33
Eli.A Eli.A

2013/2/15

#
@Gingervitis I did and changed it just to " if(atWorldEdge())" and it still said it wasn't a method. I also tried keeping the period in "if (.atWorldEdge())"
Eli.A Eli.A

2013/2/15

#
Gingervitis wrote...
remove 'this.' at line 33
I did and changed it just to " if(atWorldEdge())" and it still said it wasn't a method. I also tried keeping the period in "if (.atWorldEdge())"
Gingervitis Gingervitis

2013/2/15

#
it is because there is no method called 'atWorldEdge' . you will need to make a method for it
1
2
3
4
5
6
7
8
9
10
11
12
/**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
you will have to remove the '.' from before
Eli.A Eli.A

2013/2/15

#
I coppied and pasted your code, and it is saying that on line 4 (of your code) that it is an illegal start of expression
Gingervitis Gingervitis

2013/2/15

#
I'm going to tell you what I have in my game because I don't know everything about Greenfoot. Make a new class that extends actor and call it "Animal". Use this code below and delete what I told you to do before. Then make the your Bullet class extend Animal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import greenfoot.*;
 
/**
 * Animal. This is the base class for all animals. In addition to the standard Actor
 * methods, it provides methods to eat other animals and check for the edge of the world.
 *
 * @author Michael Kölling
 * @version 2.0
 */
public class Animal extends Actor
{
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
     
     
    /**
     * Return true if we can see an object of class 'clss' right where we are.
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;       
    }
 
     
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}
Eli.A Eli.A

2013/2/15

#
Okay, the bullet flip-flopped. What happens is when the bullet goes to the edge it vanishes but when it hits the balloon this Greenfoot error pops up saying this java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.atWorldEdge(Animal.java:17) at Bullet.destroy(Bullet.java:32) at Bullet.act(Bullet.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.atWorldEdge(Animal.java:17) at Bullet.destroy(Bullet.java:32) at Bullet.act(Bullet.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Gingervitis Gingervitis

2013/2/15

#
This has happened to me before. I'm am not 100% sure on what to tell you. Like I said before, I am still semi-new to Greenfoot. I will try to help you out. Can you re-post your Bullet class?
Eli.A Eli.A

2013/2/15

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullet extends Animal
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
{
        move(5);
        destroy();
         
}
        public void destroy()
{
  Actor ballon;
  ballon = getOneObjectAtOffset(0,0, Ballon.class);
  if (ballon != null)
    {
    World world;
    world = getWorld();
    world.removeObject(ballon);
    world.removeObject(this);
  }
if  (atWorldEdge())
      {World world;
      world = getWorld();
      world.removeObject(this);
    
}
 
}
}
Gingervitis Gingervitis

2013/2/15

#
Try removing line 30.
Eli.A Eli.A

2013/2/15

#
YYYYYYYYEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSS!!!!!!!!! It works!!!!!!! YOU ARE THE BEST MAN!!!!!!!! But, I do have one more question if you don't mind. I want to make it so that if you don't hit the ballon and it get all the way to the left side, it vanishes. How do I do that?
Gingervitis Gingervitis

2013/2/15

#
Wouldn't it disappear at the world edge anyway?
Eli.A Eli.A

2013/2/15

#
No, I'm talking about the balloon you are shooting
Gingervitis Gingervitis

2013/2/15

#
I'm not sure if there is an easier way to do it, but it is was me, I would have a class that looks like the background of your world, and it will eat your balloon when it touches it.
There are more replies on the next page.
1
2