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

2017/3/16

i want to pass ict

lordt lordt

2017/3/16

#
there's a nullpointerexception error when it reaches level 2. please tell me how to fix i want to pass and not fail a subject. and also is there a way to restart the counter but with different total count and actor that gets shot here's the blob (the "bullet" the guardian shoots)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Blob  extends Guardian
{
    //  private Guardian myHero;

    public Blob()
    {
        //this.myHero = myHero;

    }

    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int xpos = getX();

        if (xpos > 0)
        {
            xpos = xpos - 5;
            setLocation(xpos, getY());

            Actor Level1 = getOneIntersectingObject(Level1.class);
            Actor Level2 = getOneIntersectingObject(Level2.class);

            if(isTouching(Level2.class))
            {
                hitAVillain2();
                MyWorld theWorld = (MyWorld) getWorld();  // get a reference to the world
                Counter2 counter2 = theWorld.getCounter2();
                if(counter2.total2()==0)
                {
                    getWorld().removeObject(Level2);
                   
                }
                getWorld().removeObject(this);
            }
            
            if (Level1 != null)
            {
                // We've hit the villain!
                hitAVillain();
                MyWorld theWorld = (MyWorld) getWorld();  // get a reference to the world
                Counter counter = theWorld.getCounter();
                if(counter.total()==0)
                {
                    getWorld().removeObject(Level1);
                    getWorld().addObject (new Level2 (),174, 320);
                    getWorld().removeObject(counter);
                    getWorld().addObject (new Counter2 (),169, 213);

                }
                getWorld().removeObject(this);
            }
            
        }
        else {
            // I reached the top of the screen
            getWorld().removeObject(this);
        }

    }

    private void hitAVillain()
    {
        // get a reference to the counter
        MyWorld theWorld = (MyWorld) getWorld();
        Counter counter = theWorld.getCounter();
        counter.bumpCount(1);

    }
    
    private void hitAVillain2()
    {
        // get a reference to the counter
        MyWorld theWorld = (MyWorld) getWorld();
        Counter2 counter2 = theWorld.getCounter2();

    }
}
here's the first counter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter  extends Actor
{
    private int totalCount = 5;

    public Counter()//constructor
    {
        setImage(new GreenfootImage("Life:     " + totalCount, 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }

    public void act()
    {
        setImage(new GreenfootImage("Life:     " + totalCount, 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount -= amount; 

    }
    
    public int total()
    {
        return totalCount;
    }
}
here's the second counter's code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter2  extends Actor
{
    private int totalcount2 = 10;

    public Counter2()//constructor
    {
        setImage(new GreenfootImage("Life:     " + totalcount2, 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }

    public void act()
    {
        setImage(new GreenfootImage("Life:     " + totalcount2, 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpcount(int amount2)
    {
        totalcount2 -= amount2; 

    }
    
    public int total2()
    {
        return totalcount2;
    }
}
here's the world's code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { private Counter counter; private Counter2 counter2; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } public Counter getCounter() { return counter; } public Counter2 getCounter2() { return counter2; } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { addObject(new Guardian(), 485, 321); counter = new Counter(); addObject(counter, 169, 213); addObject(new Level1(),173, 303); } }
Super_Hippo Super_Hippo

2017/3/16

#
counter2 is null and is not set to anything in your world. Btw, you do not need 2 counter classes when they are doing the same thing.
lordt lordt

2017/3/16

#
i have no idea how to fix, someone pls send a revised code
Super_Hippo Super_Hippo

2017/3/16

#
You are adding Counter1 to the world, why not Counter2? I have a question. What are Level1 and Level2?
You need to login to post a reply.