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

2020/10/12

Add obj

1
2
3
Roshan123 Roshan123

2020/10/13

#
Max:-not a class Counter Blocks Help Crate Health kit1 Health kit 2 shield1 Shield2 Score Slow Fast Tank 1 Tank2 Bot1 Bot2 rotate1 Rotate2 Bullet1 Bullet2 Smoke Explosion Min:-//not a class Counter Blocks Crate Slow Fast Tank1 Tank2 Bot1 Bot2 rotate1 Rotate2 Smoke My processor speed is 2.20GHz And it lags on minimum classes in site
Roshan123 Roshan123

2020/10/13

#
import greenfoot.*;  
public class MyWorld extends World
{
    Counter counter=new Counter();
    public Tank mainTank=new Tank(counter);
    public B mainB=new B(counter);
    int timer,bottimer,bottimer1;
    int r[]={680,120};
    int r2[]={120,480};
    public MyWorld()
    {    
        super(800, 600, 1);
        setPaintOrder(Help.class,EnemyCannon2.class,EnemyTank.class,EnemyCannon.class,
        EnemyTank1.class,Counter.class,BSheild.class,Sheild.class,Tank.class,B.class
        ,Magn_feild.class,Stone.class);
        showText("press 1 for back \npress 2 for help \npress 3 for P v.s. P \n press 4 for Computer", 96, 46);
    }
    
    public void act()
    {
        showText("press 1 for back \npress 2 for help \npress 3 for P v.s. P \n press 4 for Computer", 96, 46);
    
        int rand=Greenfoot.getRandomNumber(r.length);
        int rand2=Greenfoot.getRandomNumber(r2.length);
        if(Greenfoot.isKeyDown("4"))
        {
            
        addObject(counter, 410, 15);
        
        mainB=new B(counter);
        addObject(mainB, 120,480);

        addObject(new EnemyTank(true,4), 365, 300);
        addObject(new EnemyCannon2(20), 365, 300);
        
        addObject(new Stone(), 300,300);
        addObject(new Stone(), 248,300);
        addObject(new Stone(), 196,300);
        addObject(new Stone(), 196,353);
        addObject(new Stone(), 196,406);
        
        addObject(new Stone(), 500,300);
        addObject(new Stone(), 552,300);
        addObject(new Stone(), 604,300);
        addObject(new Stone(), 604,248);
        addObject(new Stone(), 604,196);
        
        addObject(new Speed(), 607, 80);
        addObject(new Speed(), 195, 520);
        addObject(new Magn_feild(), 85, 400);
        addObject(new Magn_feild(), 715, 200);
        
        if(timer>0 && --timer ==0) addObject(new B(counter),r[rand],r2[rand2]);
        if(timer==0 && getObjects(B.class).isEmpty())timer =100;
        
        if(bottimer>0 && --bottimer ==0) addObject(new EnemyTank(true,4),355,355);
        if(bottimer==0 && getObjects(B.class).isEmpty())bottimer =100;
        
        if(bottimer>0 && --bottimer ==0) addObject(new EnemyCannon2(20),355,355);
        if(bottimer==0 && getObjects(EnemyCannon2.class).isEmpty())bottimer =100;
        
        }
        if(Greenfoot.isKeyDown("3"))
        {
        addObject(counter, 410, 15);
        
        mainTank=new Tank(counter);
        addObject(mainTank, 680,120);

        mainB=new B(counter);
        addObject(mainB, 120,480);
        
        addObject(new Crate(), 145,300);
        addObject(new Crate(), 655,300);
        
        addObject(new Stone(), 300,300);
        addObject(new Stone(), 248,300);
        addObject(new Stone(), 196,300);
        addObject(new Stone(), 196,353);
        addObject(new Stone(), 196,406);
        
        addObject(new Stone(), 500,300);
        addObject(new Stone(), 552,300);
        addObject(new Stone(), 604,300);
        addObject(new Stone(), 604,248);
        addObject(new Stone(), 604,196);
        
        addObject(new Speed(), 607, 80);
        addObject(new Speed(), 195, 520);
        addObject(new Magn_feild(), 85, 400);
        addObject(new Magn_feild(), 715, 200);
        
        if(timer>0 && --timer ==0) addObject(new B(counter),r[rand],r2[rand2]);
        if(timer==0 && getObjects(B.class).isEmpty())timer =100;
        
        if(timer>0 && --timer ==0) addObject(new Tank(counter),r[rand],r2[rand2]);
        if(timer==0 && getObjects(Tank.class).isEmpty())timer =100;
        
        if(counter.min==0 && counter.sec==2700)
        {
          addObject(new Crate(), 196,250);
          addObject(new Crate(), 604,350);
        }
        }
        
        if(Greenfoot.isKeyDown("2"))
        {
            addObject(new Help(), 400, 300);
        }
        if(Greenfoot.isKeyDown("1"))
        {
            Greenfoot.setWorld(new MyWorld());
        }
        
     }
}
Roshan123 Roshan123

2020/10/13

#
Plz tell me that which line should i remove and where will i add ur code after removing the stuff
danpost danpost

2020/10/13

#
Add:
private boolean initialized;

private void initialize()
{
    addObject(left, 300,300);
    addObject(new Stone(), 248,300);
    addObject(new Stone(), 196,300);
    addObject(new Stone(), 196,353);
    addObject(new Stone(), 196,406);
     
    addObject(right, 500,300);
    addObject(new Stone(), 552,300);
    addObject(new Stone(), 604,300);
    addObject(new Stone(), 604,248);
    addObject(new Stone(), 604,196);
     
    addObject(new Speed(), 607, 80);
    addObject(new Speed(), 195, 520);
    addObject(new Magn_feild(), 85, 400);
    addObject(new Magn_feild(), 715, 200);
    initialized = true;
}
Then replace both lines 43 thru 58 and lines 83 thru 98 with the following line:
if (!initialized) initialize();
To see why, before changing code, run scenario pressing "3" and "4"; next, pause scenario and pick a specific stone and drag it away from where placed. Continue dragging stones from same place. Note the excessive number of actors in your world. Another thing you can do is right-click on world and execute the int numberOfObjects() method. After changing code, execute the method again and note the difference. With tanks and bots, have them all referenced in fields (as in line 5). DO NOT use "new ..." to create more elsewhere. Just use the referenced names.
danpost danpost

2020/10/13

#
Line 21 is excess code being executed every act frame (remove it).
Roshan123 Roshan123

2020/10/13

#
Recently i updated the scenario and fixed the lag and i got to know that explosion class was the reason for it(60%sure)
Roshan123 Roshan123

2020/10/13

#
I have a question Why i should not use new... What demerits does it have F f = new f(); //why its better
Roshan123 Roshan123

2020/10/13

#
You have written 43 and 83 Do u wanted to write 42 and 82 or u have written correct
danpost danpost

2020/10/13

#
Roshan123 wrote...
I have a question Why i should not use new... What demerits does it have F f = new f(); //why its better
That was just for tanks, in particular, as you did not want to have them added if their type was already in the world.
danpost danpost

2020/10/13

#
Roshan123 wrote...
You have written 43 and 83 Do u wanted to write 42 and 82 or u have written correct
Strange, looks now like 36 thru 51 and 76 thru 91. I think you may have edited the post. Also adding crates should probably be included within the initialize method.
Roshan123 Roshan123

2020/10/14

#
danpost wrote...
Roshan123 wrote...
You have written 43 and 83 Do u wanted to write 42 and 82 or u have written correct
Strange, looks now like 36 thru 51 and 76 thru 91. I think you may have edited the post. Also adding crates should probably be included within the initialize method.
Sorry for that I edited it 3 timea and posted it
You need to login to post a reply.
1
2
3