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

2014/4/5

code error

GamesGrinder1998 GamesGrinder1998

2014/4/5

#
i'm currently making a game and seem to get an error saying "Actor not in world. An attempt was made to use the actor's location while it is not in the world...."Can someone please help asap What does it mean
danpost danpost

2014/4/5

#
It means just what it says -- most of the Actor class methods require that your actor be in the world to execute them (namely, all the collision checking method and 'getX' and 'getY'). If you use 'getWorld().removeObject(this);' before any of these methods, you will get that error message.
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
danpost wrote...
It means just what it says -- most of the Actor class methods require that your actor be in the world to execute them (namely, all the collision checking method and 'getX' and 'getY'). If you use 'getWorld().removeObject(this);' before any of these methods, you will get that error message.
do you know how to fix this problem
danpost danpost

2014/4/5

#
GamesGrinder1998 wrote...
do you know how to fix this problem
Not without seeing what code you are using in the class. That is, I will probably have to see the entire class code. Use the 'code' link below the 'Post a reply' input box to insert your code.
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
Fireball Code
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
50
51
52
53
54
55
56
57
58
import greenfoot.*; 
 
public class Fireball extends Player
{
    private int direction, speed;
 
    public Fireball(int dir)
    {
        direction = 270;
        speed = 15;
    }
 
    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);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            world.removeObject(this);
        }   
 
        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            world.removeObject(this);
        }
 
        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            world.removeObject(this);
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
            }
        }
 
    }  
}   
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
danpost wrote...
GamesGrinder1998 wrote...
do you know how to fix this problem
Not without seeing what code you are using in the class. That is, I will probably have to see the entire class code. Use the 'code' link below the 'Post a reply' input box to insert your code.
import greenfoot.*; public class Fireball extends Player { private int direction, speed; public Fireball(int dir) { direction = 270; speed = 15; } 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); if (BugEnemy != null) { World world; world = getWorld(); world.removeObject(BugEnemy); world.removeObject(this); } if (InsectEnemy != null) { World world; world = getWorld(); world.removeObject(InsectEnemy); world.removeObject(this); } if (Bee != null) { World world; world = getWorld(); world.removeObject(Bee); world.removeObject(this); } else { if(atWorldEdge()) { getWorld().removeObject(this); } } } }
danpost danpost

2014/4/5

#
Replace lines 28 through 55 with:
1
2
3
4
5
6
7
if (BugEnemy != null ||
    InsectEnemy != null ||
    Bee != null ||
    atWorldEdge())
{
    getWorld().removeObject(this);
}
By putting them all together, you can avoid the error. Another way would be to put a 'return;' statement after each occurrence of 'getWorld().removeObject(this);'.
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
danpost wrote...
Replace lines 28 through 55 with:
1
2
3
4
5
6
7
if (BugEnemy != null ||
    InsectEnemy != null ||
    Bee != null ||
    atWorldEdge())
{
    getWorld().removeObject(this);
}
By putting them all together, you can avoid the error. Another way would be to put a 'return;' statement after each occurrence of 'getWorld().removeObject(this);'.
Thank you sooo much :) it does work now do you know how to make the enemies move down at the same time like the game space invaders
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
GamesGrinder1998 wrote...
danpost wrote...
Replace lines 28 through 55 with:
1
2
3
4
5
6
7
if (BugEnemy != null ||
    InsectEnemy != null ||
    Bee != null ||
    atWorldEdge())
{
    getWorld().removeObject(this);
}
By putting them all together, you can avoid the error. Another way would be to put a 'return;' statement after each occurrence of 'getWorld().removeObject(this);'.
Thank you sooo much :) it does work now do you know how to make the enemies move down at the same time like the game space invaders
Do you also know how to make the enmies bullet disappear if it hits the player's bullet
danpost danpost

2014/4/5

#
GamesGrinder1998 wrote...
do you know how to make the enemies move down at the same time like the game space invaders
See this. Read the description.
danpost danpost

2014/4/5

#
GamesGrinder1998 wrote...
Do you also know how to make the enmies bullet disappear if it hits the player's bullet
Same way as the Fireball disappears when hitting a BugEnemy, InsectEnemy, or Bee object.
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
danpost wrote...
GamesGrinder1998 wrote...
do you know how to make the enemies move down at the same time like the game space invaders
See this. Read the description.
Sorry i don't really understand it
danpost danpost

2014/4/5

#
I placed an invisible actor across each row of enemies and had the act method of my world tell each row when and which direction to move. The invisible actor would then report back if any enemies in its row reached the edge of the world, so that the world would know that a drop-down/direction-change procedure is due.
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
danpost wrote...
I placed an invisible actor across each row of enemies and had the act method of my world tell each row when and which direction to move. The invisible actor would then report back if any enemies in its row reached the edge of the world, so that the world would know that a drop-down/direction-change procedure is due.
what would the code look like
danpost danpost

2014/4/6

#
What do you think it should look like? First create a class for the invisible actor. Write two methods in it. One for moving all its intersecting enemies in the direction the world directs by way of an argument to the method:
1
public boolean moveEnemies(int direction)
The direction will be a positive or negative int value. The boolean return is to inform the world when an enemies has moved too close to an edge. The other method is for the drop-down procedure which the world will call also:
1
public void drop()
where the invisible actor and all its enemies will be moved down on the screen a little. The rest of the code will go in your world class act method with fields in the class for the direction of movement and to hold a flag indicator for whether dropping down is needed. A timer field is also needed to control the animation (so all the enemies do not move in tandem -- each row should move independent of another).
You need to login to post a reply.