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

2013/10/30

keeping and incrementing a score

i_joker123 i_joker123

2013/10/30

#
Im trying increment a score to one when a bullet collides with a baddie. But i get this error when i try to: java.lang.NullPointerException at baddie.pop(baddie.java:34) at bullet.collide_with_ballon(bullet.java:44) at bullet.act(bullet.java:26) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here are my classes: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class character extends Actor { public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse !=null) setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI)); move(speed); if(Greenfoot.mouseClicked(null)) { //shoot(); getWorld().addObject(new bullet(getRotation()),getX(),getY()); turnTowards(mouse.getX(), mouse.getY()); } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; public class scoreBoard extends Actor { public static final float FONT_SIZE=48.0f; public static final int WIDTH=400; public static final int HEIGHT=300; public scoreBoard() { this(100); } public scoreBoard(int score) { makeImage("Game Over", "Score: ",score); } private void makeImage(String title, String prefix,int score) { GreenfootImage image = new GreenfootImage(WIDTH,HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } public void act() { // Add your action code here. } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class baddieWorld extends World { private int score; Counter counter = new Counter("Score: "); /** * Constructor for objects of class balloonWorld. * */ public void countPop() { counter.add(1); } public baddieWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); populate(); } private void populate() { addObject(counter,350,50); } public void act() { if(Greenfoot.getRandomNumber(100) < 3) { addObject(new baddie(), Greenfoot.getRandomNumber(700), 600); } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; public class bullet extends Actor { private int direction; Counter c= new Counter(); baddie b = new baddie(); int score =0; /** * Act - do whatever the firedArrow 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. //setLocation(getX()+5,getY()); move(5); colision(); } public bullet(int dir) { setRotation(dir); } public void collision() { Actor baddie=getOneIntersectingObject(baddie.class); if(baddie !=null) { getWorld().removeObject(baddie); getWorld().removeObject(this); b.pop(); } } public class baddie extends Actor { private int score; public void act() { // Add your action code here. setLocation(getX(), getY()-1); } public void pop() { ((baddieWorld) getWorld()).countPop();((baddieWorld) getWorld()).countPop(); } } Any suggestions?
danpost danpost

2013/10/30

#
In collision of bullet class, try calling 'baddie.pop()' first.
i_joker123 i_joker123

2013/10/30

#
ive tried that but the score doesnt increment
danpost danpost

2013/10/30

#
You need to show more of your bullet class (especially the 'collide_with_balloon' method Oh, and this time ... PLEASE ... use the 'code' tag below the input box for entering your code.
i_joker123 i_joker123

2013/10/30

#
This is all my bullet class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

public class bullet extends Actor
{
    private int direction;
    baddie b = new baddie();
    /**
     * Act - do whatever the firedArrow 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.
        //setLocation(getX()+5,getY());
        move(5);
        go_off();
        collide();
        
        
    }    
    public bullet(int dir)
    {
 
        setRotation(dir);
    }
    
   
    public void collide()
    {
        Actor baddie=getOneIntersectingObject(baddie.class);
        if(baddie !=null)
        {
            b.pop();
            getWorld().removeObject(baddie);
            getWorld().removeObject(this);
      
        }
        
        
    }  
        
    
    public void go_off()
    {
        if(getX()>=getWorld().getWidth()-1)
            getWorld().removeObject(this);
        else if (getX() <=1)
            getWorld().removeObject(this);
        else if (getY() >=getWorld().getHeight()-1)
            getWorld().removeObject(this);
        else if(getY() <=1)
            getWorld().removeObject(this);
    }
}
danpost danpost

2013/10/30

#
Remove line 7 and change line 34 to '((baddie)baddie).pop();'.
i_joker123 i_joker123

2013/10/30

#
one more thing, when my bullet hits the wall i get this error:
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
	at bullet.collide(bullet.java:36)
	at bullet.act(bullet.java:23)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/10/30

#
Change line 18 to 'if (getWorld() != null) collide();'. (when 'go_off' removes the actor, 'collide' cannot get intersectors).
You need to login to post a reply.