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

2017/4/26

Wheres my error?

1
2
danpost danpost

2017/4/27

#
ali18 wrote...
Ok, i have done that, what should i do now?
Show your plane class codes.
ali18 ali18

2017/4/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class plane here. * * @author (Ali Kozkiran) * @version (a version number or a date) */ public class plane extends Actor { /** * Act - do whatever the plane wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); remove(); shoot(); nextLevel(); //When all the Person's are removed from the current level, it will automatically entre level2 //nextLevelThree(); //When all the Person's are removed from the current level, it will automatically entre level3 Actor plane = getOneIntersectingObject(rocket.class); if (plane !=null) { ((HealthBar)getWorld().getObjects(HealthBar.class).get(0)).loseHealth(); //The Plane will lose Health, when the Rocket touches it World myWorld = getWorld(); World world; world = getWorld(); } } public void moveAndTurn() { move(4); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("up")) { move(3); } if (Greenfoot.isKeyDown("down")) { move(-3); } } public void remove() { Actor person = getOneObjectAtOffset(0, 0, person.class); if (person != null) { getWorld().removeObject(person); ((Counter)getWorld().getObjects(Counter.class).get(0)).bumpCount(5); //Level1 level1 = (Level1)myWorld; //score score = level1.getsco(); //counter.addPoints(); } } public void shoot() { if ("space".equals(Greenfoot.getKey())) { bullet shot=new bullet(); getWorld().addObject(shot,getX(),getY()); shot.setRotation(getRotation()); } } private void nextLevel() { if (getWorld().getObjects(person.class).isEmpty()) { if (getWorld() instanceof level1) Greenfoot.setWorld(new level2()); else if (getWorld() instanceof level2) Greenfoot.setWorld(new level3()); else if (getWorld() instanceof level3) Greenfoot.setWorld(new Win()); } } } I have only got this that relates back to my health bar, if you want the whole code for the plane, i could send it..
danpost danpost

2017/4/27

#
Replace the last 9 lines of the act method with the code provided in my last snippet.
ali18 ali18

2017/4/28

#
Alright, i have done that and now everything is working fine, when i hit the rockets with my plane they disappear and one pixel of health is taken away from my health bar. Also, the health bar is working all three levels, thank you very much for your help and the game over message appears, and not only that, the rocket disappears of the world as well when it is game over. That is amazing!! Thanks!
ali18 ali18

2017/4/28

#
Sorry, i just wanted to ask something else. Do you know i am able to make my bullets disappear when it hits the rockets and also make an quick explosion??
   public void shoot()
    {	if ("space".equals(Greenfoot.getKey()))
        {
            bullet shot=new bullet();
            getWorld().addObject(shot,getX(),getY());
            shot.setRotation(getRotation());
        } 
    }
This is the code in my plane
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (Ali Kozkiran) 
 * @version (a version number or a date)
 */
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 void act() 
    {
        move(5);
        kill();
        remove();
    }
    public void kill()
    {
        Actor rocket=getOneIntersectingObject(rocket.class);
        if (rocket != null)
        {
            World myWorld = getWorld();
            getWorld().removeObject(rocket);
        }
}
public void remove()
    {
        Actor rocket = getOneObjectAtOffset(0, 0, rocket.class);
        if (rocket != null)
        {
            World world;
            World myWorld = getWorld();
            getWorld().removeObject(this);
        }
        else
        {
            if(getX()==599||getX()==0||getY()==0||getY()==399)
            {
                getWorld().removeObject(this);
            }
        }
    }
}
This is the code in my bullet class
You need to login to post a reply.
1
2