i have two classes, enemy class and level class.

in level class i am changing the counter value.

note i set up my if statment so it will only goes in onces and never again. so if u reach lvl 2 than it will go in lvl2 if statment onces reset counter to 10. than wait when player reach lvl 3.

if(...)  //if lvl 1
{
   //change counter = 20
}
if(...) //if lvl 2
{
  //change counter = 10
}
if(...) //if lvl 3
{
   //change counter = 5
}

enemy.class
this method just makes enemy to shoot bullet. and counter is the time between every shoot. so enemy shoot a bullet than wait 20 int. when reach 0 than shoot again.

loop/method
counter--;
    if(counter <= 0 &&!shooting)
    {
        //create bullet object / add in arraylist

        shooting = true;
        counter = 20;       //reset
    }
    if(counter != 0)
    {
        shooting= false;
}

the problem is that i am hard coding value of counter=20 in emeny class.but i value of counter change depends on wha level player is on.

i was thinking of making counter1, counter2, counter3, in level class. and in enemy class i can use getter method to get the value and do i test.

the problem with this is i end up making variable for every lvls. is there way so i only use one counter. and dont rewrite code.

Recommended Answers

All 3 Replies

you can pass the level of the shooter to the enemy class , then do a switch case statement like :

        shooting = true;
        switch(level){ // the shooters level

            case 1:
                counter = 20;// reset
                break;
            case 2:
                counter = 10;
                break;
            case 3:
                counter = 5;
                break;
            default:
                break;

        }

thanks

Depending on how many things vary from level to level it may make sense to have a Level class with 4 instances, each instance having its own values for all counter etc etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.