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

2019/1/17

PLEASE HELP ME FOR THE LOVE OF GOD

Swangbee Swangbee

2019/1/17

#
I need to incorporate the while and for loop, even though it is not necessary
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class BabySpawner extends Actor
{
    private int x = 0;
    int counter = 0;
    public boolean SpaceShip = true;   
            public void act() 
            {
                
                counter++;
                while(counter > 50 && deathcheck() = true)
                {
               
                if(counter == 50)
                 {
                    for (int x=0; x<3; x++)
                    {
                        aestroid  aestroid = new aestroid();
                        getWorld().addObject(aestroid,(int)(Math.random()*100),(int)(Math.random()*600));
                    }
                    counter = 0;
                }    
            
        
            }
        }
            
                 public void deathcheck()
                {
                     if ((SpaceShip.class) = null)
                    {
                         return false;
                     }
                }
            }
. Can someone tell me where I'm going wrong, please?
gng gng

2019/1/18

#
Line 12: You can't apply an integer with a boolean in the while condition. Line 31: I think you get there an error from Greenfoot. Check the errors.
danpost danpost

2019/1/18

#
Swangbee wrote...
I need to incorporate the while and for loop, even though it is not necessary << Code Omitted >> Can someone tell me where I'm going wrong, please?
You need to remember to use the conditional operator '==' when comparing two things for equality. Using a single equal sign will cause the expression on the right to be evaluated and assigned to that which is on the left. If that which is on the left cannot be assigned that type of value, an error will ensue. That being said, you cannot set a method call to a value (line 12) and you cannot set a class literal to null (line 31). After you fix the signs, you will still have issues. The deathcheck method is not declared to return anything (line 29); a class cannot be null (line 31 -- I do not think that is what you were trying to check for there); and if the method is to return a boolean value, then there must be no path to the end of the method where a return statement is not lastly encountered (if the condition on line 31 is false, the inner block is not executed and nothing follows afterward).
Swangbee Swangbee

2019/1/18

#
Okay Great! I got it to work. Now I have a new question. I want to get this powerup to add the
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Powerup here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Powerup extends Actor
{
    /**
     * Act - do whatever the Powerup wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    public void givehealth()
    {
        SpaceShip ss = getOneIntersectingObject(SpaceShip.class); 
        if (powerup != null)
        {
            
            get
        }
    }
}


Here's the health bar code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
//import java.awt.Color;
/**
 * Write a description of class HelthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HelthBar extends Actor
{
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15; 
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    public HelthBar()
    {
        update();
    }
    public void act()
    {
        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);
        endgame();
    }    
    public void LoseHealth()
    {
        health--;
    }
    public void endgame()
    {
        if (health == 0)
        {
            ScoreCounter finalscore =(ScoreCounter) getWorld().getObjects(ScoreCounter.class).get(0);
            GG go = new GG(finalscore.getScore());
            Greenfoot.setWorld(go);
            
             //ScoreCounter = (Player)getOneObjectAtOffset(0,0, Player.class);
            //JOptionPane.showMessageDialog(null, "Final Score is: " + ScoreCounter.getScore());
            //Greenfoot.stop();
}}}
health score if the spaceship is touching the power-up(heart). Do you know how to?
danpost danpost

2019/1/18

#
If the HealthBar object is contained by the SpaceShip object, then it would make good sense to have the space ship look for the Powerup object and not the other way around.
You need to login to post a reply.