Cocos2D:塔防游戏制作之旅(五)
发布时间:2020-12-14 16:43:31 所属栏目:百科 来源:网络整理
导读:打开HelloWorldLayer.h文件,添加以下实例变量(在@interface行的花括号之后): NSMutableArray *towerBases ; 将HelloWorldLayer.m文件修改为如下内容: //Add a new method -( void )loadTowerPositions{ NSString * plistPath = [[ NSBundle mainBundle] path
打开HelloWorldLayer.h文件,添加以下实例变量(在@interface行的花括号之后): NSMutableArray *towerBases;
将HelloWorldLayer.m文件修改为如下内容: //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中同步属性已经不再需要.(猫猪注) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |