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

2014/1/7

World suddenly creates too much actors

Nooyi Nooyi

2014/1/7

#
Hello, I use this code to create an Level in my game:

/* 
         * 1 = Tree
         * 2 = Player
         * 3 = another Actor called 'Donator' (he places cherrys on the map)
         * 
         * 
         */

int [][] newlvl_1 = {
                    { 3,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
                    { 1,2,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
                    { 1,0,0,0,0,0,0,0,0,0,0,0,0,3,1 },
                    { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};
    for (int y = 0; y < newlvl_1[0].length; y++)    {
        for(int x = 0; x < newlvl_1.length; x++)    {
            int ID = newlvl_1[x][y];
                switch(ID)  {
                    case 1: addObject(new Tree(),x,y);
                    case 2: addObject(new Player(),1,1);
                 }
             }
      } 
When you start the game, everything is working, but sometimes the world creates another player so that there are actually two! Only if you turn back to point (1|1) (where the "2" is), one of them is deleted. I really don't know whythis is happening, so is there anyone who could help me? Thanks. :)
bourne bourne

2014/1/7

#
You are missing a break. Note, a switch statement just jumps the execution to a case and continues execution from there (with the next line within the switch block).
case 1: addObject(new Tree(),x,y); break;
case 2: addObject(new Player(),1,1); 
Nooyi Nooyi

2014/1/7

#
Oh, of course, thanks! I can't belief I haven't seen that! Thanks a lot :)
You need to login to post a reply.