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

2017/5/24

Help

Trystar360 Trystar360

2017/5/24

#
i have this code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Map here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Map extends World
{
    public Maps m = new Maps();
    
    private int map[][]=
    {
        {1,1,1,1,1},
        {1,0,0,0,1},
        {1,0,0,0,1},
        {1,0,0,0,1},
        {1,1,1,1,1}
    };
    int wallss;
    public Map()
    {  
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(5, 5, 201, false);
        Mapp mapp = new Mapp();
        for( int y=0; y!=map.length; y++) for(int x=0; x!=map[y].length; x++)
        {
            //Actor actor = null;
            switch(map [y] [x])
            {
                case 0: break;
                case 1: objectsss(mapp,x, y); break;
            }
            //addObject(actor, x, y);
            
        }
        
        
    }
    
    int x1, y1;
    public void objectsss(greenfoot.Actor actor, int x, int y)
    {
        x1 = x;
        y1 = y;
        addObject(actor, x, y);
        wallss ++;
    }
}
but when run it there is only one object in the top right of the world
danpost danpost

2017/5/24

#
Change both '!=' in line 28 to '<'. Remove lines 11, 21 and 27. Change line 34 to:
case 1:  actor = new Wall(); break;
Uncomment lines 30 and 36. Remove lines 43 through 50.
Trystar360 Trystar360

2017/5/24

#
danpost wrote...
Change both '!=' on line 28 to '<'. Remove lines 11 and 27. Change line 34 to:
case 1:  actor = new Wall(); break;
Uncomment line 36.
I'm getting a null pointer exception at Map.<init>(Map.java:35) (which is
addObject(actor, x, y);
)
danpost danpost

2017/5/24

#
Change that line to:
if (actor != null) addObject(actor, x, y);
Trystar360 Trystar360

2017/5/24

#
That fixed it. Thank you!
You need to login to post a reply.