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

2013/4/1

Minimize code

1
2
JetLennit JetLennit

2013/4/1

#
This code is slowing my game down because it is so big... is there an easier way to code this?
public void spawnTank()
    {
        firstRand = (Greenfoot.getRandomNumber(600));
        secondRand = (Greenfoot.getRandomNumber(4));
        northAndSouth = (Greenfoot.getRandomNumber(640));
        eastAndWest = (Greenfoot.getRandomNumber(540));
        tank tank = new tank();
        if(firstRand == 0)
        {
            if(secondRand == 0) 
            {
                //spawn west
                addObject(tank, 5, northAndSouth + 5);
            }
            if(secondRand == 1)
            {
                //spawn south
                addObject(tank, northAndSouth + 5, 645);
            }
            if(secondRand == 2)
            {
                //spawn east
                addObject(tank, 645, northAndSouth + 5);
            }
            if(secondRand == 3)
            {
                //spawn west
                addObject(tank, northAndSouth + 5, 5);
            }
        }
    }
bourne bourne

2013/4/1

#
How often is spawnTank() called?
JetLennit JetLennit

2013/4/1

#
it is the only thing under act() in my world
bourne bourne

2013/4/1

#
So it is called every act cycle. That's a lot of "tank"s being added to the World.
JetLennit JetLennit

2013/4/1

#
Actually, it barely spawns any... however if i increase the number it makes it a bit better but that is still a lot of code
Game/maniac Game/maniac

2013/4/1

#
change firstRand = (Greenfoot.getRandomNumber(600)); to firstRand = (Greenfoot.getRandomNumber(2000));
JetLennit JetLennit

2013/4/1

#
I did that, but i waited for like five minutes and only one spawned
Game/maniac Game/maniac

2013/4/1

#
How often do tanks spawn with firstRand = (Greenfoot.getRandomNumber(600));
JetLennit JetLennit

2013/4/1

#
about 3 for 30 seconds
danpost danpost

2013/4/1

#
Yes, on average, one should spawn every 10 seconds when using 600 for the firstRand generation. An average scenario runs at about 60 frames per second (60*10 = 600). Try moving'Tank tank = new Tank();' inside the following 'if' block.
JetLennit JetLennit

2013/4/1

#
It can not find method tank()
danpost danpost

2013/4/1

#
Please show your code. 'tank()' should probably be 'Tank()' (with a capital T).
danpost danpost

2013/4/1

#
Sorry. You named your class with lowercase (un-conventionally). I should be 'tank tank = new tank();'.
JetLennit JetLennit

2013/4/1

#
the class starts with a lowercase
bourne bourne

2013/4/1

#
By convention, class names are capitalized so to be distinguished from variable names. What does "slowing the game down" mean? Are the tanks that actually appear seem lagish? And what do you mean by "too big code"? The code shown doesn't look "bad", no real performance leak.
There are more replies on the next page.
1
2