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

2017/4/26

Wheres my error?

1
2
ali18 ali18

2017/4/26

#
I have used this specific code for my health bar, and it is working, however, i want it to take 1 pixel of health away each time the rocket hits my plane. At the moment, the health bar decreases immediately when the rocket hits me. HELP!!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HealthBar here.
 * 
 * @author  (Baris Yildirim) 
 * @version (Greenfoot version 2.4.0)
 */
public class HealthBar extends bar
{
    int health = 3; //The amount of lifes which the Car has before dying
    int healthBarWidth = 150; //The Width of the Health Bar
    int healthBarHeight = 15; //The Height of the Health Bar
    int pixelsPerHealthPoint = (int)healthBarWidth/health; //This Coding divides the Width by the Health
    public void act()
    {
        update();
        //loseHealth();
    }
    
    public HealthBar()
    {
        update();
    }
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }
    public void loseHealth()
    {
        health--;
        update();
        if (health<1)
        {
            getWorld().addObject(new GameOver(),getWorld().getWidth()/2,getWorld().getHeight()/2);
            getWorld().addObject(new Restart(), 505,365);
            update();
            //Greenfoot.stop();
            //Greenfoot.delay();
        }
    }
}
ali18 ali18

2017/4/26

#
I have used the code below in my rocket class.
public void hitplane()
    {
        Actor plane= getOneIntersectingObject(plane.class);
        if (plane != null)
        {
            World myWorld = getWorld();
            level1 level1 = (level1)myWorld;
            HealthBar healthbar = level1.getHealthBar();
            if(touchingPlane == false)
            {
                healthbar.loseHealth(); 
                touchingPlane = true;
                if(healthbar.health <=0)
                {
                    GameOver gameover = new GameOver();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }
            }
        } else {
            touchingPlane = false;
       }
    }
danpost danpost

2017/4/26

#
ali18 wrote...
I have used this specific code for my health bar, and it is working, however, i want it to take 1 pixel of health away each time the rocket hits my plane. At the moment, the health bar decreases immediately when the rocket hits me.
Do you have any code in the plane class that deals with rocket objects?
ali18 ali18

2017/4/26

#
I have done the code now, and it is working very well, now that every rocket that hits me, they are removed and my health bar is decreased by one, however, i don't know how to make this happen for all the levels as i have got three levels within my game. My new code is shown below:
[b]Plane:[/b]
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();
        }
Rocket public void hitplane() { Actor plane= getOneIntersectingObject(plane.class); if (plane != null) { World myWorld = getWorld(); level1 Level1 = (level1)myWorld; HealthBar healthbar = Level1.getHealthBar(); healthbar.loseHealth(); if(healthbar.health <= 0) { GameOver gameover = new GameOver(); myWorld.addObject(new GameOver(), myWorld.getWidth()/2, myWorld.getHeight()/2); myWorld.addObject(new Restart(), 450, 360); myWorld.addObject(new MainMenu(), 450, 410); } myWorld.removeObject(this); } } Health bar { int health = 3; //The amount of lifes which the plane has before dying int healthBarWidth = 150; //The Width of the Health Bar int healthBarHeight = 15; //The Height of the Health Bar int pixelsPerHealthPoint = (int)healthBarWidth/health; //This Coding divides the Width by the Health public void act() { update(); //loseHealth(); } public HealthBar() { update(); } public void update() { setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.WHITE); myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1); myImage.setColor(Color.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight); } public void loseHealth() { health--; } }
ali18 ali18

2017/4/26

#
The problem is when i get hit by the rocket in level 2 a red message pops
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at plane.act(plane.java:27)
	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)
java.lang.ClassCastException: level3 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.NullPointerException
	at HealthBar.loseHealth(HealthBar.java:40)
	at rocket.hitplane(rocket.java:42)
	at rocket.act(rocket.java:20)
	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)
java.lang.NullPointerException
	at HealthBar.loseHealth(HealthBar.java:40)
	at rocket.hitplane(rocket.java:42)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at plane.act(plane.java:27)
	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)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at plane.act(plane.java:27)
	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)
java.lang.NullPointerException
	at HealthBar.loseHealth(HealthBar.java:40)
	at rocket.hitplane(rocket.java:42)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
java.lang.ClassCastException: level2 cannot be cast to level1
	at rocket.hitplane(rocket.java:38)
	at rocket.act(rocket.java:20)
	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)
up. the message is shown below:
danpost danpost

2017/4/26

#
Okay. You have the plane detecting an intersecting rocket as well as the rocket detecting an intersecting plane. You cannot (well, should not) have both trying to detect the other. Either have one detect the other or the other detect the one. Since the healthbar "belongs" to the plane, I would suggest having the plane doing the detecting. Remove the 'hitPlane' method from the rocket class and the call to it from the act method of the rocket class. You can also remove the 'boolean touchingPlane' field from the rocket class. Now, add a 'boolean touchingRocket' field to the plane class and replace the rocket detecting code you just gave above with the following:
if (touchingRocket != isTouching(rocket.class)) // change in state?
{
    touchingRocket = !touchingRocket; // record new state
    if (touchingRocket) // touching after change?
    {
        HealthBar hb = (HealthBar) getWorld().getObjects(HealthBar.class).get(0); // get healthbar
        hb.loseHealth(); // lose heealth
        if (hb.health == 0) // no health remains?
        {
            getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
            getWorld().addObject(new Restart(), 450, 360);
            getWorld().addObject(new MainMenu(), 450, 410);
            getWorld().removeObject(this);
        }
    }
}
Finally, you can remove lines 38 through 45 from the HealthBar class (see your initial post).
ali18 ali18

2017/4/27

#
Alright, I have done what you have asked, and now it is still functioning thank you. However, when the rocket hits my plane, i want it to take 1 pixel of health away from the health bar, therefore, i want the health bar to have 3 pixels of health overall. I want the rocket to disappear when it hits my plane. How am i able to do that? At the moment when the rocket hits my plane it does take away health however the health bar decreases very fast, and this is working for all my levels now thank you, but now the game over message does not appear.
danpost danpost

2017/4/27

#
ali18 wrote...
I want the rocket to disappear when it hits my plane. How am i able to do that?
Are you saying that now you want any rocket that touches the plane to be immediately removed from the world?
ali18 ali18

2017/4/27

#
Yeah i want any rocket that touches the plane to be removed and the health bar to be decreased by one pixel, and i want this to be compatible for all my 3 levels (level1, level2, and level3). Please Help! sorry for the confusion above
danpost danpost

2017/4/27

#
ali18 wrote...
Yeah i want any rocket that touches the plane to be removed and the health bar to be decreased by one pixel, and i want this to be compatible for all my 3 levels (level1, level2, and level3). Please Help! sorry for the confusion above
Then, you do not need the 'touchingRocket' field at all. Change the given code above to this:
if (isTouching(rocket.class))
{
    removeTouching(rocket.class);
    HealthBar hb = (HealthBar) getWorld().getObjects(HealthBar.class).get(0); // get healthbar
    hb.loseHealth(); // lose heealth
    if (hb.health == 0) // no health remains?
    {
        getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
        getWorld().addObject(new Restart(), 450, 360);
        getWorld().addObject(new MainMenu(), 450, 410);
        getWorld().removeObject(this);
    }
}
ali18 ali18

2017/4/27

#
This doesn't work, because every time the rocket hits the plane, the health bar still decreases immediately and also the rocket doesn't disappear as well. Additionally, the game over message still doesn't appear. The code shown below
public void hitplane()
    {
        Actor plane= getOneIntersectingObject(plane.class);
        if (touchingRocket != isTouching(rocket.class)) // change in state?
       {
       
        if (isTouching(rocket.class))
{
    removeTouching(rocket.class);
    HealthBar hb = (HealthBar) getWorld().getObjects(HealthBar.class).get(0); // get healthbar
    hb.loseHealth(); // lose heealth
    if (hb.health == 0) // no health remains?
    {
        getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
        getWorld().addObject(new Restart(), 450, 360);
        getWorld().addObject(new MainMenu(), 450, 410);
        getWorld().removeObject(this);
    }
}
         }
       }
danpost danpost

2017/4/27

#
Remove the entire method. Collision detection between the rocket and plane is done in the plane class.
ali18 ali18

2017/4/27

#
SO what should i do, i dont understand? I should get rid of the whole code? and then do what? sorry and thanks..
danpost danpost

2017/4/27

#
Remove the entire 'hitplane' method.
ali18 ali18

2017/4/27

#
Ok, i have done that, what should i do now?
There are more replies on the next page.
1
2