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

2011/11/27

Adding Objects randomly

1
2
danpost danpost

2011/11/29

#
change my first line to
01.int[] maxObjs = { 2, 6, 4, 3, 10};
kiarocks kiarocks

2011/11/30

#
Simulation freezes at startup-when i add the objects.
danpost danpost

2011/12/1

#
Did you put the code in a method -- called something like 'public void createCurcuit() -- and call it from the World constructor (to start) and can be called from an Actor at a later time? If your still having problems, we may have to take another look at what you have.
kiarocks kiarocks

2011/12/1

#
/**
     * Act - do whatever the CircutBuildAndDisplay wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        if(getWorld() instanceof CircutWorld)
        {
            cw = (CircutWorld) getWorld();
            if(!dothisonce)
            {

                int[] maxObjs = { 2, 6, 4, 3, 10};  
                int[][] objsGrid = {{5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5} };  
                for (int myObj = 0; myObj < 5; myObj++)  
                {  
                    for (int myCt = 0; myCt < maxObjs[myObj]; myCt++)  
                    {  
                        int x = Greenfoot.getRandomNumber(5);  
                        int y = Greenfoot.getRandomNumber(5);  
                        while (objsGrid[x][y] != 5)  
                        {  
                            x = Greenfoot.getRandomNumber(5);  
                            y = Greenfoot.getRandomNumber(5);  
                        }  
                        objsGrid[x][y] = myCt;  
                    }  
                }  
                for (int y = 0; y < 5; y++)  
                {  
                    for (int x = 0; x < 5; x++)  
                    {  
                        switch(objsGrid[x][y])
                        {
                            case 0: cw.addObject(new Light(), (x+2)*64,(y+2)*64); break;
                            case 1: cw.addObject(new Wire(), (x+2)*64,(y+2)*64); break;
                            case 2: cw.addObject(new Diode(), (x+2)*64,(y+2)*64); break;
                            case 3: cw.addObject(new Resistor(), (x+2)*64,(y+2)*64); break;
                            case 4: cw.addObject(new EmptyCircuitArea(), (x+2)*64,(y+2)*64); break;
                        }
                    }
                }
                dothisonce = true;
            }
        }

        GreenfootImage img = new GreenfootImage(Integer.toString(cw.numberOfObjects()),21,Color.BLACK,Color.white);
        setImage(img);
        setLocation(250,50);
    }
danpost danpost

2011/12/1

#
I cannot tell by this why your simulation is freezing (I am not totally sure that it is the code I provided causing this).
kiarocks kiarocks

2011/12/1

#
That may be the case, however, not until i tried your code did it freeze.
kiarocks kiarocks

2011/12/1

#
Wait, just tested it- it no longer freezes, but instead of 27 total objects, there are 21. In other words there is 6 actors missing.
danpost danpost

2011/12/2

#
I only know of the 25 components that we have been discussing. I know nothing of any others that you may have. Did you stop the simulation, right-click on the world and run the 'int numberOfObject()' method? That may give an indication as to what may be going on. EDIT: OK, I found the 26th object (the number of object display), but still missing the last one!
kiarocks kiarocks

2011/12/2

#
Note line of code near bottom. I have two other objects, that is unimportant.
kiarocks kiarocks

2011/12/2

#
The last obj is a button. Sends you to another world.
kiarocks kiarocks

2011/12/3

#
Still no answer?
danpost danpost

2011/12/3

#
Found it! It was in my code. Change line 27 to set objGrid to myObj instead of myCt.
kiarocks kiarocks

2011/12/4

#
YAY! It works!
You need to login to post a reply.
1
2