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

2018/1/10

Bullet Generator

TheSlorrow44 TheSlorrow44

2018/1/10

#
 int array;
    int rotation=0;
    int spawnTimer=0;
    int wave;
    int turn;
    int angle; //angle between first and last 
    int time;
    int angleArray; 
    Plop[] bullet = new Plop[100];
    public generateAttack4(int ARRAY , int ROTATION, int WAVE, int TURN, int ANGLE, int TIME)
    {
        array =  ARRAY;
        rotation =  ROTATION;
        wave =  WAVE;
        turn =  TURN; 
        angle =  ANGLE;
        time =   TIME;
     
    }
    public void act() 
    {
        prepareBullet();
        if(wave>0)
        {
            spawnTimer=(spawnTimer+1)%time;
            if(spawnTimer==0)
            {
                addBullet();
                wave--;
                turn(turn);
            }
        }
         
    } 
    public void prepareBullet()
    {
        for(int i=1; i<=array; i++)
        {
           bullet[i] = new Plop();
        }
    }
    public void addBullet()
    {
        if(array==1) angleArray=angle;
        else angleArray=angle/(array-1);
        int x=0;
        for(int i=1; i<=array; i++)
        {
            getWorld().addObject(bullet[i],getX(),getY());
            bullet[i].setRotation(getRotation()+rotation+x);
            x+=angleArray;
        }
    }
So ,I've made this "Bullet Generator" ,but the problem is that I want to use this "Generator" with more kind of bullets , which I've named Plop1, Plop2 and Plop 3 . Will help me If I pass a class or object as a parameters , and how can I do this?I'm new to object-oriented programming and greenfoot(2 weeks) .So ,sorry if my question is stupid , and sorry for my bad English. BTW for a beginner , how bad is this "bullet generator"?
danpost danpost

2018/1/10

#
First, unless there is a visual need to have the generator as an Actor object, it should not extend the Actor class. Second, better is to use int fields to track number of each type bullets and create each as needed, rather than creating a number of them for later use.
TheSlorrow44 TheSlorrow44

2018/1/12

#
danpost wrote...
First, unless there is a visual need to have the generator as an Actor object, it should not extend the Actor class. Second, better is to use int fields to track number of each type bullets and create each as needed, rather than creating a number of them for later use.
Thanks!!!
You need to login to post a reply.