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/12

#
Its not adding the actor again to the world when its null
//world
//Globally
int timer;
//act method
if(Greenfoot.isKeyDown("4"))
{
 addObj(tank,40,40);
if(timer>0 && --timer ==0) addObj(new tank(),40,40);
if(timer==0 && getObjects(tank.class).isEmpty())timer=100;
}
Super_Hippo Super_Hippo

2020/10/12

#
Line 9 will only add a new one if no tank is in the world. So you can’t add more than one with this. If this timer is used as a reloading time, it should probably reload regardless of the state of the ‘4’ key. What is line 7 doing?
Roshan123 Roshan123

2020/10/13

#
Sorry i posted half of the code If tank2 destroys tank then tank is removed from the world and its added again after 1.20 seconds But its not adding tank after tank2 destroys
//world
//Globally
int timer;
Tank tank = new Tank();
Tank2 tank2=new Tank2();
//act method
if(Greenfoot.isKeyDown("4"))
{
 addObj(tank,40,40);
addObj(tank2,40,80);
if(timer>0 && --timer ==0) addObj(new Tank2(),40,80);
if(timer==0 && getObjects(Tank2.class).isEmpty())timer=100;
if(timer>0 && --timer ==0) addObj(new Tank(),40,40);
if(timer==0 && getObjects(Tank.class).isEmpty())timer=100;
}
if(Greenfoot.isKeyDown("3"))
{
addObj(tank,40,40);
addObj(new Bot(2,true),40,80);
if(timer>0 && --timer ==0) addObj( new Bot(2,true),40,80);
if(timer==0 && getObjects(Bot.class).isEmpty())timer=100;
if(timer>0 && --timer ==0) addObj(new Tank(),40,40);
if(timer==0 && getObjects(Tank.class).isEmpty())timer=100;
}
And here also i m having the same problem This also doesn't add the bot when its .empty
Roshan123 Roshan123

2020/10/13

#
Anybody plz help me
danpost danpost

2020/10/13

#
Roshan123 wrote...
Anybody plz help me
As written, the key ("3" or "4") would need to be held down for the timer to fully run. In other words the running of the timer should not depend on a key being down -- only the initiating of it should.
Roshan123 Roshan123

2020/10/13

#
If u will run the scenario then at 1st glance it will show 1 is for new world i.e. restart 2 is for help(instructions) 3 is for Player vs Computer 4 is for Player vs Player If i will use (Tank.class).isEmpty() outside the condition then with no doubt it will obviously add the tank after 1.20 seconds which i don't want I want if 4 is pressed then it will add certain classes and in that condition is the added class is missing then it will add those class which are written in the condition(if null) I have no idea how will i do it so plz kindly tell me....
danpost danpost

2020/10/13

#
Roshan123 wrote...
at 1st glance
This is only at the start?
Roshan123 Roshan123

2020/10/13

#
In world i have used show text for info(press 1 or press 3 ...etc) After clicking it the certain classes will be added 1st press run button to start the scenario Then read the info After reading it press any no. Then it will give u the output depending upon what u pressed(1,2,3,4)
danpost danpost

2020/10/13

#
Sounds like using getKey would be a better option -- and once a key is accepted, do not accept any more. Also, using separate worlds for each option would help. Passing "this" to Help world will allow a return to this world; and passing a boolean value to Game world will inform of using a Bot actor or a secondary Tank actor. Something like this:
// global
private String key;
private int timer;

// in act
if (key != null)
{
    if (++timer == 100)
    {
        switch ("1234".indexOf(key))
        {
            case 1:  Greenfoot.setWorld(new MyWorld()); break;
            case 2:  Greenfoot.setWorld(new Help(this)); break;
            case 3:  Greenfoot.setWorld(new Game(true)); break;
            case 4:  Greenfoot.setWorld(new Game(false)); break;
        }
    }
    return;
}
key = Greenfoot.getKey();
if (key != null && "1234".indexOf(key) < 0) key = null;
danpost danpost

2020/10/13

#
Oh, wait -- this is in your game world. I just noticed the "i.e. restart" in your above post. That changes things dramatically.
Roshan123 Roshan123

2020/10/13

#
If greenfoot.iskeyfown 1 Greenfoot.setWorld(new MyWorld());
Roshan123 Roshan123

2020/10/13

#
If 2 then it will show help.png
danpost danpost

2020/10/13

#
Okay, you can still use the same setup as just given. However, for case 3 and 4, you would have something like:
case 3:
removeObjects(getObjects(Tank2.class));
if (getObjects(Bot.class).isEmpty()) addObject(new Bot(2, true), 40, 80);
break;

case 4:
removeObjects(getObjects(Bot.class));
if (getObjects(Tank2.class.isEmpty()) addObject(new Tank2(), 40, 80);
break;
Roshan123 Roshan123

2020/10/13

#
I made a surplus actors around 16 which made my scenario lag(i m not fully sure about it) I added all at once So now i m trying to seperate the classes by applying condition (1,2,3) I want to add certain classes to decrease the lag
Roshan123 Roshan123

2020/10/13

#
Do i need to make another world
There are more replies on the next page.
1
2
3