- 初始化时都加到一个batchNode中
3- 循环list中每一个object (这里定义了一个抽象类 GameCharacter,实现类有自己的实现方法)
每个object都对list遍历一次,检测是否有交互。
#pragma mark –
#pragma mark Update Method
-(void) update:(ccTime)deltaTime {
CCArray *listOfGameObjects = [sceneSpriteBatchNode children]; // 1
for (GameCharacter *tempChar in listOfGameObjects) { // 2
[tempChar updateStateWithDeltaTime:deltaTime
andListOfGameObjects: listOfGameObjects]; // 3
}
}
- PowerUps 的实现: GameObject 作为一个抽象类
// Mallet.h
// SpaceViking
//
#import <Foundation/Foundation.h>
#import "GameObject.h"
@interface Mallet : GameObject {
CCAnimation *malletAnim;
}
@property (nonatomic, retain) CCAnimation *malletAnim;
@end
// Mallet.m
// SpaceViking
//
#import "Mallet.h"
@implementation Mallet
@synthesize malletAnim;
- (void) dealloc {
[malletAnim release];
[super dealloc];
}
-(void)changeState:(CharacterStates)newState {
if (newState == kStateSpawning) {
id action = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:malletAnim
restoreOriginalFrame:NO]];
[self runAction:action];
} else {
[self setVisible:NO]; // Picked up
[self removeFromParentAndCleanup:YES];
}
}
-(void)updateStateWithDeltaTime:(ccTime)deltaTime
andListOfGameObjects:(CCArray*)listOfGameObjects {
float groundHeight = screenSize.height * 0.065f;
if ([self position].y > groundHeight)
[self setPosition:ccp([self position].x,
[self position].y - 5.0f)];
}
-(void)initAnimations {
[self setMalletAnim:
[self loadPlistForAnimationWithName:@"malletAnim"
andClassName:NSStringFromClass([self class])]];
}
-(id) init
{
if( (self=[super init]) )
{
screenSize = [CCDirector sharedDirector].winSize;
gameObjectType = kPowerUpTypeMallet;
[self initAnimations];
[self changeState:kStateSpawning];
}
return self;
}
@end