Game Development Reference
In-Depth Information
EnemyCache* enemyCache = [EnemyCache node];
[self addChild:enemyCache z:0];
BulletCache* bulletCache = [BulletCache node];
[self addChild:bulletCache z:1 tag:GameSceneNodeTagBulletCache];
return self;
The Component Classes
Component classes are intended as plug-ins for game logic. If you add a component to
a node, the node will execute the behavior of the component: moving, shooting, anim-
ating, showing a healthbar, and so on. The big benefit is that you program these com-
ponents to work generically and can use them with a great number of game objects.
The components interact with their parent and should make as few assumptions
about the parent class as possible. Of course, in some cases a component requires the
parent to be a CCSprite or even an Enemy class, but then you can still use it with
any type of CCSprite or Enemy class.
You can also configure component classes depending on the class that's using the com-
ponent. As an example for component classes, take a look at the Stand-
ardShootComponent initialization in the Enemy class:
StandardShootComponent* shootComponent = [StandardShootComponent node];
shootComponent.shootFrequency = shootFrequency;
shootComponent.bulletFrameName = bulletFrameName;
[self addChild:shootComponent];
The variables shootFrequency and bulletFrameName were set previously
based on the EnemyType . By adding the StandardShootComponent to the
Enemy class, the enemy will shoot specific bullets at certain intervals. Because this
component makes no assumptions about the parent class, you can even add an instance
of it to the Ship class and have your player's ship shoot automatically at specific in-
tervals! Or by simply activating and deactivating specialized shooting components, you
can create the effect of changing weapons for the player with very little code. You just
program the shooting code in isolation and then add it to a node and add some paramet-
Search Nedrilad ::

Custom Search