2
\$\begingroup\$

I have a base class called Attacker that has a update method. The update method moves attacker through an array of waypoints as given below:

- (void) update:(ccTime)dt {
    if(state == kMove) {
        // code to move to next waypoint
        // on reaching the next waypoint, update the next waypoint variable
        // if the final waypoint is reached, then stop the movement of attacker
    }
}

So, the base class takes care of simple waypoint movement logic. Now I derive a few classes like Rifleman, MachineGunner, Engineer from the Attacker class.

Now I want each of the derived class to do a specific action on reaching the final waypoint. For eg., I want the Rifleman to change to attack stance, the machine gunner to deploy and setup his gun, the engineer to start constructing a defense.

How do I achieve this? My current idea is to check for final waypoint condition in the update method of each of the derived class and do the corresponding action.

- (void) update:(ccTime)dt {
    [super update:dt];
    if(state == kMove) {
        // if the final waypoint is reached, then do class specific action
    }
}

Is there any better way of doing this?

\$\endgroup\$
5
  • \$\begingroup\$ If by "if the final waypoint is reached, then do class specific action" you mean just calling the "endFunction" then yes, polymorphism (en.wikipedia.org/wiki/Polymorphism_(computer_science)) will do the rest. \$\endgroup\$ Commented Dec 13, 2011 at 10:01
  • \$\begingroup\$ @mikidelux you should make an answer out of this \$\endgroup\$ Commented Dec 13, 2011 at 10:08
  • \$\begingroup\$ @mikidelux well if you think your comment is understandable enough, then you can just copy-paste it. But Jari Komppa has already given basically the same answer as of now. \$\endgroup\$ Commented Dec 13, 2011 at 10:11
  • \$\begingroup\$ I wrote the answer while you were discussing, sorry =) \$\endgroup\$ Commented Dec 13, 2011 at 13:54
  • \$\begingroup\$ @JariKomppa no probs :) u too answered me right... but i couldnt mark 2 right answers !!! \$\endgroup\$ Commented Dec 13, 2011 at 15:36

2 Answers 2

2
\$\begingroup\$

If by "if the final waypoint is reached, then do class specific action" you mean just calling the "endFunction" then yes, polymorphism will do the rest. That is, if you implement "endFunction" in every subclass or have a default one in the superclass.

\$\endgroup\$
3
  • \$\begingroup\$ is calling the endFunction in the update method of base class enough? given the base class have an empty endFunction, which is overridden in the derived classes... am i right? \$\endgroup\$ Commented Dec 13, 2011 at 10:30
  • \$\begingroup\$ I'm not sure if you are saying it right (english is not my main language) but it should go like this: SuperClass obj = new SubClass(); Then, when you call obj.endFunction(), the function in the subclass will be called instead of the superclass one (If there is an overriden endFunction in the subclass). \$\endgroup\$ Commented Dec 13, 2011 at 10:47
  • \$\begingroup\$ yes it works... thx :) \$\endgroup\$ Commented Dec 13, 2011 at 11:07
4
\$\begingroup\$

The simplest solution I can think of is to create a virtual function for whenArrivedAtFinalWaypoint, call that from update, and implement that for the child classes.

\$\endgroup\$
3
  • \$\begingroup\$ I am coding in objective-c which dont have virtual functions. But I suppose in obj-c method calls works the same, without any explicit virtual keyword. \$\endgroup\$ Commented Dec 13, 2011 at 10:32
  • \$\begingroup\$ @saiy2k yes, they do. \$\endgroup\$ Commented Dec 13, 2011 at 11:34
  • \$\begingroup\$ This. Just add a note to make it clear that it should be called from within the baseclass update. \$\endgroup\$ Commented Dec 14, 2011 at 3:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.