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

2012/4/4

removing objects at the left of screen

1
2
3
4
SPower SPower

2012/4/6

#
I'm sorry, I typed instead of . That's why my normal text now also is displayed as code. I hope it isn't annoying for you.
davmac davmac

2012/4/6

#
SPower, I fixed your message.
e_e13 e_e13

2012/4/7

#
I can understand what you're trying to do, and to me it makes a lot of sense. However, totalCount is still 1. Here is what I changed in the bullet code for it to work (but I don't think this is the problem- see below):
int y = 0;
        spaceinvaders spi = (spaceinvaders) getWorld();  
        Actor invader = getOneIntersectingObject(Invader.class);  
        if (invader != null)  
        {  
            spi.removeObject(invader);  
            spi.removeObject(this);  
            Counter counter = spi.getCounter(); 
            if (y==0)
            {
               counter.Counter();
               y = 1;
            }
             
            counter.bumpCount();  
            
            return;  
        }  
I think the problem must be from the getCounter method, since everything else makes sense. Here is the code for getCounter:
public Counter getCounter()
        {
            Counter counter = new Counter(); 
            return counter;
        }
I also have an older version like this:
private Counter cc;
public Counter getCounter()
        {
            
            return cc;
        }
This version follows the tutorial, however it gives me a huge error message. I could post it here if needs be, but it literally is hundreds of lines long. EDIT: Ah, hold on- I think they're all the same. Here it is: java.lang.NullPointerException at Bullet.act(Bullet.java:35) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
SPower SPower

2012/4/7

#
I thinking I now know what's wrong with Your code. In getCounter(), you're always creating a new counter. That counter will always start as 0. So you always see 1. You need to do this:
private Counter counter;
To create your counter. You also have to set a value to it:
public yourWorld()
{
... Create your world here...
counter = new Counter();
}
And then, you only have to return that counter:
public Counter getCounter()
{
return counter;
}
@davmac thank you for fixing my message!
SPower SPower

2012/4/7

#
PS you got that error because you didn't give your counter a value. And then, you're returning and doing things with nothing. That's not allowed in Java. That's called null pointer exception. With my version, it should work fine.
danpost danpost

2012/4/7

#
A major problem is here
public Counter getCounter()
{
    Counter counter = new Counter(); 
    return counter;
}
You are creating a 'new Counter()' (a different Counter object) each time this method is called. You should be creating your Counter objects in the world constructor or a method it calls, saving a reference to it as such (in your world code)
Counter counter = new Counter(); // sets up an instance world Counter variable with the counter
// Your world constructor
public spaceinvaders()
{
    super (600, 400, 1);
    prepare();
}
// add your objects to the world
public void prepare()
{
    // add some objects here
    addObject(counter, 100, 10);
    // maybe, add some more objects here
 }
// The following method return a reference to the already created counter
public Counter getCounter()
{
    return counter;
}
Mind you, this code is just to show you how to set the counter object up -- DO NOT copy/paste this into your code. With something like this, using 'Counter counter = spi.getCounter();' followed by 'counter.bumpCount()' would increment the already created counter.
danpost danpost

2012/4/7

#
A little pointer as far as the error message you got: if you notice the second line (though, it is not always in that position, but will be the first recognizable line), it is telling you where your code failed (in the 'Bullet' class, at line number '35').
e_e13 e_e13

2012/4/7

#
Ah, finally! totalCount is now counting up :) I changed the code as suggested by SPower, and also removed the bit of code in bullet which appeared to cause the error. Now I just have one more problem on my hands. As far as I know, the act() method will continue to loop as long as run is pressed. However, I tried to change the act() method a little to check if that was working properly (as the picture still doesn't show), and it turns out it is not being processed at all. In this code I have for the counter now, I expected text to show the numbers increasing. However, nothing comes up- despite this check being in the act() class.
public class Counter extends Actor
{
private int totalCount = 0;  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
        World world;  
        world = getWorld(); 
        System.out.println("The value of the counter is: " + totalCount); 
        if (totalCount == 4 && world.getObjects(GameWon.class).isEmpty())  
        {  
            
            world.addObject(new GameWon(), 450, 300);  
        }
        
    }        
    
    public void bumpCount()
    {
        totalCount = totalCount+ 1;
        //System.out.println("The value of the counter is: " + totalCount); 
    }
    
    
}
Nothing is being printed out- where have I made a flaw?
SPower SPower

2012/4/7

#
I think you're not changing the value of the counter on the screen.
e_e13 e_e13

2012/4/7

#
I don't have a counter on the screen- I'm using that term as a facade for checking the increase of totalCount
SPower SPower

2012/4/7

#
Sorry, misunderstood. I usually do this:
System.out.println("The value of counter is: " + Integer.toString(totalCount) );
instead of:
System.out.println("The value of counter is: " + totalCount);[.code]

And another tip, I see you doing this:
[code]World world;    
world = getWorld();
world.dosomething;
It's better to do this:
getWorld().dosomething;
This uses less memory, because we're not creating a variable. Probably it's not that much memory, but it's always better to don't use more memory than you need to.
e_e13 e_e13

2012/4/7

#
At the moment, while the code works I don't want to fiddle around with it too much incase I break something, but afterwards I'll go through and reduce all the memory :) I've worked out why act() wasn't working- I hadn't added it to the world yet. When I added it to the world I found out it continued to think totalCount was 0, so I moved that section to bumpCount(). I also added a method in the world class since adding it manually wouldn't work:
public class Counter extends Actor
{
private int totalCount = 0;  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
        
        
    }        
    
    public void bumpCount()
    {
        totalCount = totalCount+ 1;
        //System.out.println("The value of the counter was: " + totalCount); 
        World world;  
        world = getWorld(); 
        if (totalCount == 4)  
        {  
            spaceinvaders spaceWorld = (spaceinvaders) getWorld();  
            spaceWorld.finish();
            return;
        }
    }
    
    
}
with the code in the world that causes the picture to come up being:
public void finish()
    {
        GameOver go;
        go = new GameOver ();
        
        addObject(go, 900, 500);
    }
However- now I get this error: java.lang.NullPointerException at Counter.bumpCount(Counter.java:33) at Bullet.act(Bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) The lines in question effected by the bug are: spaceWorld.finish(); in Counter class and counter.bumpCount(); in the Bullet class
SPower SPower

2012/4/7

#
Can you post the ENTIRE(includes comment) code of the following classes: Counter Bullet spaceinvaders then I can take a look at what's wrong in your code
e_e13 e_e13

2012/4/7

#
Thanks for your help :) I'm sorry about this counter business taking so long to work out :/ Counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 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 = 0;  
private int totalLife = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
        
        
    }        
    
    public void bumpCount()
    {
        totalCount = totalCount+ 1;
        //System.out.println("The value of the counter was: " + totalCount); 
        World world;  
        world = getWorld(); 
        if (totalCount == 4)  
        {  
            spaceinvaders spaceWorld = (spaceinvaders) getWorld();  
            spaceWorld.finish();
        }
    }
    
    //this is another bit of code I'm working on to remove lives. It's not working yet (hence the comments) but I'll be able to work it out after I get bumpCount worked out :)
    public void bumpLife()
    {
        totalLife = totalLife +1;
        World world;  
        world = getWorld(); 
        Actor invader = getOneIntersectingObject(Invader.class);  
        if (totalCount == 1)  
        {  
            //world.removeObject(heart1);
        }
    }
    
    
}
Bullet:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Rocket
{
    /**
     * Act - do whatever the Bullet 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() + 30, getY() );
        
        
        
        
        spaceinvaders spi = (spaceinvaders) getWorld();  
        Actor invader = getOneIntersectingObject(Invader.class);  
        if (invader != null)  
        {  
            spi.removeObject(invader);  
            spi.removeObject(this);  
            Counter counter = spi.getCounter(); 
             
            counter.bumpCount();
            
            return;
        }  
if (this.atWorldEdge()==true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
        }
    
    }  
    // this code for removing the bullet from the edge of the screen was found here: http://www.greenfoot.org/topics/1088/15
    public boolean atWorldEdge()
    {
        if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())    
    
        {    
            return true;    
        }    
        else    
        {    
            return false;
        }    
    }
    
}
spaceinvaders(world):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class spaceinvaders here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class spaceinvaders extends World
{

    /**
     * Constructor for objects of class spaceinvaders.
     * 
     */
    private Counter counter;
    private Counter li;
    //spaceinvaders gw = new spaceinvaders();
    public spaceinvaders()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
        
        startscreen ss;
        ss = new startscreen();
        
        addObject(ss, 450, 300);
        
    }

    public void game()
    {
        Rocket r;
        r = new Rocket ();
        
        addObject(r, 100, 100);
        
        //Creepinginvader
        Creepinginvader c;
        c = new Creepinginvader ();
        
        addObject(c, 900, 300);
        
        //spasmInvader
        spasmInvader s;
        s = new spasmInvader ();
        
        addObject(s, 900, 150);
        
        //ZigZagInvader
        ZigZagInvader z;
        z = new ZigZagInvader ();
        
        addObject(z, 900, 450);
        
        //CustomInvader
        CustomInvader u;
        u = new CustomInvader ();
        
        addObject(u, 900, 100);
        
        
        speed sp;
        sp = new speed ();
        
        shots sh;
        sh = new shots ();
        
        addObject(sh, 250, 250);
        
        addObject(sp, 150, 150);
        
        counter = new Counter();
        li = new Counter();
        
        additionalinvaders ai;
        ai = new additionalinvaders();
        
        addObject(ai,100,100);
        
        Counter cblah;
        cblah = new Counter();
        
        addObject(cblah,200,200);
        
        heart1 h1;
        h1 = new heart1();
        
        addObject(h1,400,50);
    }
            
    public void add()
    {
        Creepinginvader c2;
        c2 = new Creepinginvader ();
        
        addObject(c2, 900, 300);
    }
    
    public void add2()
    {
        ZigZagInvader z2;
        z2 = new ZigZagInvader ();
        
        addObject(z2, 900, 20);
    }
       
    public void add3()
    {
        CustomInvader u2;
        u2 = new CustomInvader ();
        
        addObject(u2, 900, 500);
    }
    
    public void finish()
    {
        GameOver go;
        go = new GameOver ();
        
        addObject(go, 900, 500);
    }
    
        public Counter getCounter()
        {
            return counter;
        }
        
        public Counter getLife()
        {
            return li;
        }
Thanks for your help :)
SPower SPower

2012/4/7

#
I saw you doing this:
public spaceinvaders()  
{
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.  
        super(900, 600, 1);   
          
        startscreen ss;  
        ss = new startscreen();  
          
        addObject(ss, 450, 300);  
}
Does that startscreen create a Counter? Otherwise, you need to do this:
public spaceinvaders()  
{
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.  
        super(900, 600, 1);   
          
        startscreen ss;  
        ss = new startscreen();  
          
        addObject(ss, 450, 300);
        counter = new Counter();
        li = new Counter();
}
Or you'll get Null pointer exception. You really need to give your variables a value.
There are more replies on the next page.
1
2
3
4