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

2017/5/1

Make actor disappear upon hitting a Wall.class

wowi132 wowi132

2017/5/1

#
Hello I have the following code for a wall:
1
2
3
4
5
6
7
8
9
public class Wall extends Actor
{
    public Wall()
    {
        GreenfootImage img = new GreenfootImage(20,20);
        img.fill();
        setImage(img);
    }   
}
This wall fills up quite a bit of my world, as i am making a maze. I have a Bullet actor, that is being shot from my character, and is supposed to disappear upon hitting the world edges, and upon hitting these walls. The full code for the bullet actor looks like this
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
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 Bullet()
    {
        getImage().scale(65,65);
    }
    public void act()
    {
        Kill();
        WorldEdge();
    }
    public void Kill()
    {
      move(10);
      Actor enemy;
      enemy = getOneObjectAtOffset(0,0, Enemy.class);
      if (enemy != null)
      {
            World world;
            world = getWorld();
            world.removeObject(enemy);
      }
}
     public void WorldEdge()
     {
     if (atWorldEdge())
      {
          getWorld().removeObject(this);
    }
}
public boolean atWorldEdge()
{
    if(getX() < 10 || getX() > getWorld().getWidth()-10)
    {
        return true;
    }
    if(getX() < 10 || getY() > getWorld().getHeight()-10)
    {
       return true;
    }
    else
    {
        return false;
    }
}
}
Now, could anyone here possibly help me, or direct me as to which code i should put where, in order to make the bullets disappear upon hitting the Wall.class? it would be very much appreciated Please note that the bullet is fairly large, so the distance to the wall at which the bullet is removed, needs to be very close to the wall, so that the bullet isnt just removed upon being created ( due to always being close to a wall ) Thanks everyone :)
danpost danpost

2017/5/1

#
You can lower the value used in the 'atWorldEdge' method from '10' anything down to '1' (all four of them). At '1', the bullet would have to be almost half past the wall before it disappears.
wowi132 wowi132

2017/5/1

#
Thanks for your answer The problem is, i need it to stop at the wall.class - not the world edge. I cant use that exact piece of code for when im at one of the walls... I have tried using the InFront command, but since my bullet is so big and the corridors between my walls so small, the bullet just dissapears right away.
danpost danpost

2017/5/1

#
You could reduce the size of your bullet image. I do not think it would even look good to have a bullet that cannot fit in a corridor "ripping" through the corridor. If that is not an option, then maybe you will need to determine the angle of the wall with respect to the direction the bullet is traveling and use that to determine if the bullet should be removed or not.
wowi132 wowi132

2017/5/1

#
I very much appreciate your help. Let me show you the code that i am currently using, and which is crashing my Greenfoot whenever i spawn in a bullet.
1
2
3
4
5
6
7
8
9
10
11
12
13
public void Move()
{
    if(getOneObjectInFront(Wall.class)==null)
    {
        move(10);
    }
    else
    {
        World world;
        world = getWorld();
        world.removeObject(this);
    }
}
which in turn calls for
1
2
3
4
5
6
7
8
private Actor getOneObjectInFront(Class c)
   {
   GreenfootImage myImage = getImage();
   int distanceToFront = myImage.getWidth();
   int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
   int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
   return (getOneObjectAtOffset(xOffset, yOffset, c));
  }
This crashes my game and tells me i am trying to remove an actor which hasnt been spawned in yet...
danpost danpost

2017/5/1

#
You need to show the entire bullet class. It is probably a combination of things you are doing.
wowi132 wowi132

2017/5/1

#
Okay here is the entire class
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Bullet extends Actor
{
public Bullet()
    {
        getImage().scale(65,65);
    }
public void act()
    {
        Kill();
        WorldEdge();
        Move();
    }
public void Move()
{
    if(getOneObjectInFront(Wall.class)==null)
    {
       move(10);
    }
    else
    {
        World world;
        world = getWorld();
        world.removeObject(this);
    }
}
         
public void Kill()
    {
      Actor enemy;
      enemy = getOneObjectAtOffset(0,0, Enemy.class);
      if (enemy != null)
      {
            World world;
            world = getWorld();
            world.removeObject(enemy);
   }
}
     public void WorldEdge()
     {
     if (atWorldEdge())
      {
          getWorld().removeObject(this);
    }
}
public boolean atWorldEdge()
{
    if(getX() < 10 || getX() > getWorld().getWidth()-10)
    {
        return true;
    }
    if(getX() < 10 || getY() > getWorld().getHeight()-10)
    {
       return true;
    }
    if(getX() < 10 || getY() < 3)
    {
        return true;
    }
    else
    {
        return false;
    }
}
    private Actor getOneObjectInFront(Class c)
    {
    GreenfootImage myImage = getImage();
    int distanceToFront = myImage.getWidth();
    int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
    int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
    return (getOneObjectAtOffset(xOffset, yOffset, c));
   }
}
wowi132 wowi132

2017/5/1

#
This gives the following crash 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:711) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913) at Bullet.getOneObjectInFront(Bullet.java:78) at Bullet.Move(Bullet.java:23) at Bullet.act(Bullet.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
wowi132 wowi132

2017/5/1

#
At further investigation - The crash only seems to happen when i hit the edges of the world! presumably because the atWorldEdge and the Move methods are conflicting. How do i keep both lines of code without them conflicting?
danpost danpost

2017/5/1

#
wowi132 wrote...
How do i keep both lines of code without them conflicting?
Change line 11 to this:
1
if (getWorld() != null) Move();
wowi132 wowi132

2017/5/1

#
Thank you, that solved it for me :)
You need to login to post a reply.