打开HelloWorldLayer.h文件,添加以下实例变量(在@interface行的花括号之后):
NSMutableArray *towerBases;
//Add a new method
-(void)loadTowerPositions
{
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"TowersPosition" ofType:@"plist"];
NSArray * towerPositions = [NSArray arrayWithContentsOfFile:plistPath];
towerBases = [[NSMutableArray alloc] initWithCapacity:10];
for(NSDictionary * towerPos in towerPositions)
{
CCSprite * towerBase = [CCSprite spriteWithFile:@"open_spot.png"];
[self addChild:towerBase];
[towerBase setPosition:ccp([[towerPos objectForKey:@"x"] intValue],[[towerPos objectForKey:@"y"] intValue])];
[towerBases addObject:towerBase];
}
}
//In init,call this new method in section #3
// 3 - Load tower positions
[self loadTowerPositions];
编译运行app,你将看到道路周围的矩形底座,它们充当了玩家炮塔的基座.
现在炮塔基座已经就绪,号召我们建立装备和建立炮塔!
首先,打开HelloWorldLayer.h文件,添加一个属性(在闭花括号之后):
@property (nonatomic,strong) NSMutableArray *towers;
在HelloWorldLayer.m中@implementation行下面同步towers属性:
@synthesize towers;
在高版本的Xcode中同步属性已经不再需要.(猫猪注)