添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
I want to add health so I can make it so that if i hit my actor (seal) it would take 3 shots to full kill it.
public void Shot()
    Actor bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (bullet != null)
        getWorld().removeObject(bullet);
        health--;
        if (health == 0){
            getWorld().removeObject(this);
            World myWorld = getWorld();
            SealWorld sealworld = (SealWorld)myWorld;
            Counter counter = sealworld.getCounter();
            counter.addScore(100);
 
Im not sure why it wont work, it just adds 100 score every time i hit the seal but it wont die. Do I need to put the code into another class?
It seems like there is a } missing for the { in line 8. The code you are showing will never add score because you would receive a runtime error. So probably you are adding the score from somewhere else.
* 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(10.0); * If Bullet hits over the Dolphin it will disappear. Actor Dolphin; Dolphin = getOneObjectAtOffset(0,0, Dolphin.class); if (Dolphin != null) World myWorld = getWorld(); myWorld.removeObject(Dolphin); Space space = (Space)myWorld; Counter counter = space.getCounter(); counter.addScore(1); Actor Seal; Seal = getOneObjectAtOffset(0,0, Seal.class); if (Seal != null) { World myWorld = getWorld(); SealWorld sealworld = (SealWorld)myWorld; Counter counter = sealworld.getCounter(); counter.addScore(100); * Bullet destroyed When hits Wall. Actor Bullet; Bullet = getOneObjectAtOffset(0,0, Bullet.class); if (Bullet != null) World world; world = getWorld(); world.removeObject(Bullet); if (atWorldEdge()) World world; world = getWorld(); world.removeObject(this);
Thats my bullet class (sorry I thought I attached it)
Okay, right now, in the Bullet class you check for intersection with a Seal and the Seal checks for intersection with a Bullet... only check the intersecting in one class. You could do it like this:
//in Bullet
Seal seal = (Seal) getOneIntersectingObject(Seal.class);
if (seal != null)
    if (seal.loseLife(1))
        ((SealWorld) getWorld()).getCounter().addScore(100);
    getWorld().removeObject(this);
//in Seal
private int health = 3;
public boolean loseLife(int amount)
    health -= amount;
    if (health < 1)
        getWorld().removeObject(this);
        return true;
    return false;
            getWorld().removeObject(this);
            World myWorld = getWorld();
            SealWorld sealworld = (SealWorld)myWorld;
            Counter counter = sealworld.getCounter();
            counter.addScore(100);
    else{
        health = health - 1;
   public Seal()
        GreenfootImage image = getImage();  
        image.scale(300, 300);
        setImage(image);
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
    public void act() 
        move(4);
        if (Greenfoot.getRandomNumber(50) < 10)
            turn(Greenfoot.getRandomNumber(3) - 3);
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
            turn(180);
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
            turn(180);
        Actor rocket;
        rocket = getOneObjectAtOffset(0,0, Rocket.class); 
        if (rocket != null)
            World myWorld = getWorld();
            Greenfoot.playSound("not_really_fine.wav");
            //Display game over text 
            GameOver gameover = new GameOver(false);
            myWorld.addObject (gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(rocket);
}
thats the whole Seal class
Super_Hippo wrote...
Okay, right now, in the Bullet class you check for intersection with a Seal and the Seal checks for intersection with a Bullet... only check the intersecting in one class. You could do it like this:
//in Bullet
Seal seal = (Seal) getOneIntersectingObject(Seal.class);
if (seal != null)
    if (seal.loseLife(1))
        ((SealWorld) getWorld()).getCounter().addScore(100);
    getWorld().removeObject(this);
//in Seal
private int health = 3;
public boolean loseLife(int amount)
    health -= amount;
    if (health < 1)
        getWorld().removeObject(this);
        return true;
    return false;
}
Thanks! This worked, although now I have a new problem. The problem is every time I hit the Seal the game just stops and i have to reload every shot it hits, but on the 3rd shot it dies like its supposed to. (The game pauses everytime I shoot a bullet at the seal)
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.act(Bullet.java:52) 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) Thats the error I get, Im looking at line 52 and see nothing wrong though.
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 Mover
     * 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(10.0); 
         * If Bullet hits over the Dolphin it will disappear.
        Actor Dolphin;
        Dolphin = getOneObjectAtOffset(0,0, Dolphin.class);   
        if (Dolphin != null)
            World myWorld = getWorld();
            myWorld.removeObject(Dolphin);
            Space space = (Space)myWorld;
            Counter counter = space.getCounter();
            counter.addScore(1);
        Seal seal = (Seal) getOneIntersectingObject(Seal.class);
        if (seal != null)
    if (seal.loseLife(1))
        ((SealWorld) getWorld()).getCounter().addScore(100);
    getWorld().removeObject(this);
         * Bullet destroyed When hits Wall. 
        Actor Bullet;
        Bullet = getOneObjectAtOffset(0,0, Bullet.class);
        if (Bullet != null)
            World world;
            world = getWorld();
            world.removeObject(Bullet);
        if  (atWorldEdge())
            World world;
            world = getWorld();
            world.removeObject(this);
        
danpost wrote...
Affter line 42, insert the following line:
return;
I noticed that your comment at line 45 does not match what to code following it is doing. Instead of bullet-wall collision detection, the code is checking for bullet-bullet collision, removing any bullet that this bullet hits.
Thanks! Worked like a charm! Last problem is that the seal's hitbox is just as big as a standard seal (pretty small). Is there anyway I can increase this hit box?