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

2015/4/27

How to get objects to fly in formation

1
2
danpost danpost

2015/5/7

#
What code are you using to create the Wingman objects (show how the parameter values are created as well).
wabuilderman wabuilderman

2015/5/8

#
Here is the creation of the wingmen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void FighterFlight(Fighters obj)
    {
        int i=0;
        int x=0;
        while(getWorld().getObjects(Fighters.class).size()>i)
        {
            Fighters f = (Fighters) getWorld().getObjects(Fighters.class).get(i);
            if(i==0&&AllSquadronsFull())
            {
                getWorld().addObject(new SquadronLeader(f),f.getX(),f.getY());
            }else if (i>0 && f.isFollower() == false)
            {
                getWorld().addObject(new Wingman(f,(Ships) ((SquadronLeader)getWorld().getObjects(SquadronLeader.class).get(0)).me),f.getX(),f.getY());
                f.setFollower(true);
            }
            i++;
        }
    }
Also, FYI, the "me" variable is simply the object that is linked to the SquadronLeader. What I am trying to do is pretty much make the SquadronLeader and Wingman be controllers, and then simply associate an uncontrolled fighter with them.
danpost danpost

2015/5/8

#
I think you are overcomplicating things by adding the linked objects.
wabuilderman wabuilderman

2015/5/8

#
lol... I at first was going to say why I needed them to be linked, but then I realized just how overly complicated my response was. How would you suggest I implement this?
danpost danpost

2015/5/8

#
You could have each SquadronLeader keep an array of its assigned Wingman objects:
1
2
3
4
5
6
7
8
9
10
11
12
13
private static final int maxWingmen = 4; // change as needed
 
private Wingman[] wingmen = new Wingman[maxWingmen];
private int wingmanCount;
 
public boolean addWingman(Wingman wingman)
{
    if (wingmanCount == maxWingmen) return false;
    wingmen[wingmanCount] = wingman;
    wingman.setID(wingmanCount);
    wingmanCount++;
    return true;
}
With the code above, you should be able to create individual squadrons; you will have to adjust the way the 'id' field is assigned its value in the Wingman class, however (removing the static counter and adding the 'setID' method). Also, the Wingman class act method should be renamed and the squadron leader should call the new named method on all of its wingmen when it acts.
You need to login to post a reply.
1
2