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

2016/5/2

Balloon-Popping Game

1
2
3
IronRod IronRod

2016/5/2

#
It still says NullPointerException. This is what I have in the Bullet class:
private int imageIndex;
public void pop()
    {
        Balloon balloon = (Balloon)getOneIntersectingObject(Balloon.class);
        int balloonColour = balloon.getColourInt();
        int headColour = getHeadColourInt();
        if (balloon!=null)
        {
            if(balloonColour == headColour)
            {
                getWorld().removeObject(this);
                getWorld().removeObject(balloon);
            }
            else
            {
                getWorld().removeObject(this);
            }
        }
    }
public void setHeadColour(int index)
    {
        imageIndex = index;
    }
    public int getHeadColourInt()
    {
        return imageIndex;
    } 
In the Head class:
private GreenfootImage[]images={new GreenfootImage("button-red.png"), new GreenfootImage("button-blue.png"), new GreenfootImage("button-yellow.png")};
    private int imageIndex;
    public Head()
    {
        setColour(0);
    }
    public int getColourInt()
    {
        return imageIndex;
    } 
    public void setColour(int index)
    {
        imageIndex = index;
        setImage(images[index]);
        Bullet bullet = new Bullet();
        bullet.setHeadColour(index);
    } 
    public void act() 
    {
       headChange();
    }
    public void headChange()
    {
       if (Greenfoot.isKeyDown("z"))
       {
           setColour(0);
       }
       if (Greenfoot.isKeyDown("x"))
       {
           setColour(1);
       }
       if (Greenfoot.isKeyDown("c"))
       {
           setColour(2);
       }
    }
And in the balloon class:
private GreenfootImage[]images={new GreenfootImage("balloon1.png"), new GreenfootImage("balloon2.png"), new GreenfootImage("balloon3.png")};
    private int imageIndex; 
    public int getColourInt()
    {
        return imageIndex;
    } 
    public void setColour(int index)
    {
        imageIndex = index;
        setImage(images[index]);
    } 
    public Balloon()
    {
        setColour(Greenfoot.getRandomNumber(images.length));
    }
This is everything relevant to my query.
valdes valdes

2016/5/2

#
The problem is line int balloonColour = balloon.getColourInt(); in Bullet class ballon is null when the bullet is traveling. just move the lines int balloonColour = balloon.getColourInt(); int headColour = getHeadColourInt(); inside the if if (balloon!=null)
IronRod IronRod

2016/5/2

#
Thank you. But now there's one more problem. A balloon only pops if it is red, regardless of the colour of the mans head. Please help! I have tried for so long.
IronRod IronRod

2016/5/2

#
A red balloon is GreenfootImage ("balloon1.png")
danpost danpost

2016/5/2

#
IronRod wrote...
Thank you. But now there's one more problem. A balloon only pops if it is red, regardless of the colour of the mans head. Please help! I have tried for so long.
Change the bullet class code to this:
private int imageIndex;

public void pop()
{
    Balloon balloon = (Balloon)getOneIntersectingObject(Balloon.class);
    if (balloon != null)
    {
        if (balloon.getColourInt() == imageIndex)
        {
            getWorld().removeObject(balloon);
        }
        getWorld().removeObject(this);
    }
}

public void setHeadColour(int index)
{
    imageIndex = index;
}
I think I made the proper adjustments.
IronRod IronRod

2016/5/2

#
Thank you @danpost but the problem is continuing. Is there a way I can show you all my code without posting it all in this discussion? Maybe the problem lies elswewhere.
danpost danpost

2016/5/2

#
For one thing, I could not find any code given where a bullet is added into the world. That is where the problem is probably located. Lines 15 and 16 of the Head class can be removed. You are creating a bullet and setting the color index of the head in it. But that bullet is not being placed into the world and is permanently lost at the end of the method.
IronRod IronRod

2016/5/3

#
what's the code I should use to place the balloon into the world and where should I put it?
danpost danpost

2016/5/3

#
IronRod wrote...
what's the code I should use to place the balloon into the world and where should I put it?
General game play goes in your World subclass.
IronRod IronRod

2016/5/3

#
I already have a bullet being added to the world and a new one added when one is removed.
IronRod IronRod

2016/5/3

#
Also when you say general game play goes in my world subclass does that mean keyboard controls etc.
danpost danpost

2016/5/3

#
IronRod wrote...
I already have a bullet being added to the world and a new one added when one is removed.
Please show the code around this (and specify which class and its type).
IronRod wrote...
Also when you say general game play goes in my world subclass does that mean keyboard controls etc.
That depends on what the keyboard input is for.
IronRod IronRod

2016/5/3

#
Ok bare with me. I'll post all my code so you can source the problem.
IronRod IronRod

2016/5/3

#
All my code World:
    private int balloonCounter = 0;
    public int worldSize = getWidth();
    public Background()
    {    
        super(600, 600, 1);    
        addObject(new Man(),getWidth()/2, getHeight()/2);
        addObject(new Head(),getWidth()/2, getHeight()/2-38);
        addObject(new Bullet(),getWidth()/2, getHeight()/2);
    }
    public void act() 
    {
        spawnBalloons();  
        respawnBullet();
        removeHealth();
    }   
    public void spawnBalloons()
    {
        balloonCounter++;
        if(balloonCounter==200)
        {
            addObject(new Balloon(),getWidth()/2, getHeight()/2-worldSize);
        }
        if(balloonCounter==400)
        {
            addObject(new Balloon(),getWidth()/2+worldSize, getHeight()/2);
        }
        if(balloonCounter==600)
        {
            addObject(new Balloon(),getWidth()/2, getHeight()/2+worldSize);
        }
        if(balloonCounter==800)
        {
            addObject(new Balloon(),getWidth()/2-worldSize, getHeight()/2);
            balloonCounter=0;
        }
    }
    public void respawnBullet()
    {
        if(Bullet.isInWorld==false)
        {
            addObject (new Bullet(), getWidth()/2, getHeight()/2);
        } 
    }
Bullet:
public static boolean isInWorld=true; 
    private int imageIndex;
    boolean canMove=false;
    public Bullet()
    {
        scaleDownImage();
        setRotation(270);
    }
    public void scaleDownImage()
    {
        GreenfootImage image = getImage();
        image.scale(20, 20);
        setImage(image);
    }
    public void act() 
    {
        aim();
        shoot();//
        hitAndMiss();
        isInWorld();
    }  
    public void aim()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation (0);
            setLocation(getWorld().getWidth()/2+10, getWorld().getHeight()/2);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation (270);
            setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2-10);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation (180);
            setLocation(getWorld().getWidth()/2-10, getWorld().getHeight()/2);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation (90);
            setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2+10);
        }     
    }
    public void shoot()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            move(50);
        }
    }   
    public void hitAndMiss()
    {           
        pop();           
    }
    public void pop()
    {
        Balloon balloon = (Balloon)getOneIntersectingObject(Balloon.class);
        if (balloon!=null)
        {
            if(balloon.getColourInt() == imageIndex)
            {
                getWorld().removeObject(balloon);
            }
            getWorld().removeObject(this);
        }
    }
    public void isInWorld()
    {
        if (getWorld()==null)
        {
            isInWorld=false;
        }
        else
        {
            isInWorld=true;
        }
    }
    public void setHeadColour(int index)
    {
        imageIndex = index;
    }
Head:
private GreenfootImage[]images={new GreenfootImage("button-red.png"), new GreenfootImage("button-blue.png"), new GreenfootImage("button-yellow.png")};
    private int imageIndex;
    public Head()
    {
        setColour(0);        
    }
    public int getColourInt()
    {
        return imageIndex;
    } 
    public void setColour(int index)
    {
        imageIndex = index;
        setImage(images[index]);
        Bullet bullet = new Bullet();
        bullet.setHeadColour(index);
    } 
    public void act() 
    {
       headChange();
    }
    public void headChange()
    {
       if (Greenfoot.isKeyDown("z"))
       {
           setColour(0);
       }
       if (Greenfoot.isKeyDown("x"))
       {
           setColour(1);
       }
       if (Greenfoot.isKeyDown("c"))
       {
           setColour(2);
       }
    }
Balloon:
private GreenfootImage[]images={new GreenfootImage("balloon1.png"), new GreenfootImage("balloon2.png"), new GreenfootImage("balloon3.png")};
    private int imageIndex; 
    public int getColourInt()
    {
        return imageIndex;
    } 
    public void setColour(int index)
    {
        imageIndex = index;
        setImage(images[index]);
    } 
    public Balloon()
    {
        setColour(Greenfoot.getRandomNumber(images.length));
    }
    public void act() 
    {
       moveTowardsMan();
    }
    public void moveTowardsMan()
    {
        Man Man = (Man) getWorld().getObjects(Man.class).get(0);
        turnTowards(Man.getX(), Man.getY());
        move(1);
    }
danpost danpost

2016/5/3

#
IronRod wrote...
All my code World: < Code Omitted > Bullet: < Code Omitted > Head: < Code Omitted > Balloon: < Code Omitted >
Not all. The Man class is missing.
There are more replies on the next page.
1
2
3