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

2013/5/18

Enemy lifes

Miikku Miikku

2013/5/18

#
Can someone say how to add lifes to enemy? Please comment here!
Barra84 Barra84

2013/5/18

#
What exactly are you trying to do and how will the lifes be removed? By creating a variable within the enemy class you can set a value for lifes ofc these are worthless until you create a method which would either add or remove from this total. Making use of the lifes. New to programming myself but hope this helps.
danpost danpost

2013/5/19

#
Normally, the lives value of an enemy is not displayed; so, using an instance int field in the Enemy class should be sufficient. One way to implement it is to have it set initially to the number of 'hits' the actor can take before dying and subtract one from it each time the actor is 'hit'. After the subtracting, add a check to see if the value is zero (no lives remaining) and, if so, remove the enemy from the world.
Miikku Miikku

2013/5/19

#
So I mean: Zombies didn't death one bullet. Here is the game were I need it: http://www.greenfoot.org/scenarios/8390
GreenGoo GreenGoo

2013/5/19

#
Every time a bullet hits a zombie, decrease a variable -"health"- by a certain amount. If health is 100, for example, subtracting 50 every time the zombie gets hit means that it can survive two shots. When health is 0, simply remove the zombie.
Miikku Miikku

2013/5/19

#
Ya but can you give me code here is my zombie: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Zombie extends Actor { private GreenfootImage zombieAnim1 = new GreenfootImage("Zombieanim.png"); private GreenfootImage zombieAnim2 = new GreenfootImage("Zombieanim2.png"); private GreenfootImage zombieAnim3 = new GreenfootImage("Zombieanim3.png"); private int frame = 1; private int animationCount = 0; public void act() { AI(); if(animationCount % 12 == 0) animationWalk(); animationCount ++; eat(); } public void AI() { turnTowards(Soldier.currentX, Soldier.currentY); move(8); } public void animationWalk() { if(frame == 1) { setImage(zombieAnim1); frame = 2; } else if(frame == 2) { setImage(zombieAnim2); frame = 3; } else if(frame == 3) { setImage(zombieAnim3); frame = 1; } } public void eat() { Actor soldier; soldier = getOneObjectAtOffset(0, 0, Soldier.class); if (soldier != null) { World ZombieWorld; ZombieWorld = getWorld(); ZombieWorld.removeObject(soldier); setImage("gameoverr.png"); Greenfoot.stop(); Greenfoot.stop(); } } }
GreenGoo GreenGoo

2013/5/19

#
int health = 100; Actor bullet = getOneIntersectingObject(Bullet.class) if (bullet != null) { health = health - 50; getWorld().removeObject(bullet); } if (health <= 0) { getWorld().removeObject(this); }
Miikku Miikku

2013/5/19

#
Thanks for Help it works! :)
DSP512 DSP512

2014/1/19

#
If I do the same and shoot 2 times at the enemy (the number needed for deleting it), then I get this Message: 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.
danpost danpost

2014/1/19

#
@DSP512, you need to copy/paste the entire error message and supply the code in question around where the error occurs (if not in the act method, then possibly the act method and a run-down of what each call in the act method does).
DSP512 DSP512

2014/1/19

#
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 Asteroid1.collisionWithShip(Asteroid1.java:64) at Asteroid1.act(Asteroid1.java:30) 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) Here's the collisionWithShip - method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void collisionWithShip(){
      if(getX()==0)
            {
                getWorld().removeObject(this);
                return;
            }
        else{
            Actor Explorer;
            Explorer=getOneObjectAtOffset(0, 0, Explorer.class);
            if(Explorer !=null)
            {
                getWorld().removeObject(Explorer);
                getWorld().removeObject(this);
                }
                }
      }
    }
If i click on the ethod in the terminal window, the 2nd line of this code will be highlited by the way, the Explorer class is the actor, who is shooting bullets towards an enemy. This method should only work, if the enemy touches the Explorer-actor. The act-method (of the enemy) looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
    {
        if(atLeftEdge()==true){
            getWorld().removeObject(this);
            return;
        }
        else {
             
            fly();
            collisionWithBullet();
            collisionWithShip();
            return;
        }
    }
danpost danpost

2014/1/19

#
If the 'collisionWithBullet' method has the actor removed from the world, the 'collisionWithShip' method will cause this exception when 'getX' is attempted. You cannot get an x-coordinate of an object that is not in the world. Change line 11 to:
1
if (getWorld() != null) collisionWithShip();
Also, line 12 is moot, as you have reached the end of the method and it will be exited anyway. Same with line 5 (no possible code you execute after that line).
You need to login to post a reply.