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

2013/5/28

How do i give my character health?

1
2
Neon147 Neon147

2013/5/29

#
Thats true, haha. I tried doing something like that but if i do setLocation shouldnt that be in my mainShip actor class? I used my enemyLaser to decrement the health, so how should i go about this? it looks like this: //BadLaser class// public void destroy() { spaceShip ship = (spaceShip) getOneIntersectingObject(spaceShip.class); ImageStrip strip = (ImageStrip) getOneIntersectingObject(ImageStrip.class); GreenfootSound explodeAlien = new GreenfootSound("Explode.wav"); explodeAlien.setVolume(75); World newWorld = null; if(ship != null) { unlock = true; explodeAlien.play(); getWorld().removeObject(ship); getWorld().addObject(ship,311,500); getWorld().removeObject(this); lives.adjustValue(-1); if(lives.getValue()<0) { newWorld = new GameOver(); Greenfoot.setWorld(newWorld); } } }
danpost danpost

2013/5/29

#
At the beginning of your 'destroy' method you created a reference to it with 'ship'. Just use 'ship.setLocation...' and remove the 'removeObject(ship)' and 'addObject(ship)' statements in the middle of that method. You should replace them with the 'ship.setLocation' statement.
Neon147 Neon147

2013/5/29

#
Alright thanks.
danpost danpost

2013/5/29

#
This line does not make any sense in your code:
ImageStrip strip = (ImageStrip) getOneIntersectingObject(ImageStrip.class);
The strip is not something that is part of the game play itself and having something intersect it is meaningless. It is more like a control or information object. In other words, it does not interact with other objects in the world; only with the user. Besides that, later in the method, you are referring to that object as 'lives', not 'strip' (either will do, but be consistent). To get a reference to the 'strip', use the following:
ImageStrip strip = (ImageStrip)getObjects(ImageStrip.class).get(0);
But, you do not need to get the reference to that object, unless the laser object intersects the ship object. There are more instances of code that could be better placed within the method. More preferably would be something like this:
public void destroy()
{
    spaceShip ship = (spaceShip) getOneIntersectingObject(spaceShip.class);
    if (ship == null) return; // exit method if not intersecting ship
    // play explosion sound
    GreenfootSound explodeAlien = new GreenfootSound("Explode.wav"); 
    explodeAlien.setVolume(75);
    explodeAlien.play();
    // reset ship
    unlock = true; 
    ship.setLocation(311, 500);
    //  deal with number of lives remaining
    ImageStrip lives = (ImageStrip) getWorld().getObjects(ImageStrip.class).get(0); 
    lives.adjustValue(-1);
    if (lives.getValue() == 0)  Greenfoot.setWorld(new GameOver());
    // remove laser object
    getWorld().removeObject(this);
}
You need to login to post a reply.
1
2