当前位置:编程学习 > wap >>

cocos2d-iphone之魔塔20层第五部分

这一章我们就要开始在Game01.m文件中canMoveTo: 方法中的if循环中添
加相应的事件了,我在制作地图时图块都设置了其属性如图:
这里我就要获取其属性值
[html]  
NSDictionary *props = [self.curtitleMap propertiesForGID:enemy_tileGid];  
NSString *value = [props valueForKey:@"enemy"];  
int type = [value intValue];  
通过获取到的属性值来判断是那一类怪物,这里还得再添加两个类Enemy 和 Monster
为了有重点的讲解这里我就先不介绍这两个类了。代码如下:
Enemy.h代码
[html]  
#import <Foundation/Foundation.h>  
#import "cocos2d.h"  
@interface Enemy : CCSprite<NSCopying>  
{  
}  
//怪物编号  
@property (nonatomic,assign) int enemyID;  
//怪物名  
@property (nonatomic,retain) NSString *name;  
//血量  
@property (nonatomic,assign) int HP;  
//攻击  
@property (nonatomic,assign) int Attack;  
//防御  
@property (nonatomic,assign) int Defense;  
//金币  
@property (nonatomic,assign) int Coin;  
//经验  
@property (nonatomic,assign) int Experience;  
//怪物行走动画  
@property (nonatomic,retain) CCAnimation *walkAction;  
-(Enemy*) initWithEmeny:(Enemy*) copyform;  
@end  
 
Enemy.m代码
[html]  
#import "Enemy.h"  
@implementation Enemy  
@synthesize enemyID;  
@synthesize name;  
@synthesize HP;  
@synthesize Attack;  
@synthesize Defense;  
@synthesize Coin;  
@synthesize Experience;  
@synthesize walkAction;  
-(id) copyWithZone:(NSZone *)zone  
{  
    Enemy *copy = [[[self class] allocWithZone:zone] initWithEmeny:self];  
    return copy;  
}  
-(Enemy*) initWithEmeny:(Enemy *)copyform  
{  
    if ((self = [super init]))  
    {  
        self.enemyID = copyform.enemyID;  
        self.name = copyform.name;  
        self.HP = copyform.HP;  
        self.Attack = copyform.Attack;  
        self.Defense = copyform.Defense;  
        self.Coin = copyform.Coin;  
        self.Experience = copyform.Experience;  
        self.walkAction = copyform.walkAction;  
    }  
    return self;  
}  
@end  
Monster.h代码
[html]  
#import <Foundation/Foundation.h>  
#import "cocos2d.h"  
#import "Enemy.h"  
  
@interface Monster : Enemy  
  
+(id)initWith:(int)typeID;  
@end  
 
Monster.m代码
[html]  
#import "Monster.h"  
@implementation Monster  
+(id)initWith:(int)typeID  
{  
    CGRect Rect;  
    Rect = CGRectMake(0, typeID*32, 32, 32);  
    Monster *enemy = nil;  
    NSString *name;  
    int HP,Attack,Defense,Coin,Experience;  
    if ((enemy = [[[super alloc] initWithFile:@"enemy.png" rect:Rect] autorelease]))   
    {  
        switch (typeID)   
        {  
            case 0:  
                name = @"骷髅兵1";  
                HP = 110;  
                Attack = 25;  
                Defense = 5;  
                Coin = 5;  
                Experience = 4;  
                break;  
            case 1:  
                name = @"骷髅兵2";  
                HP = 150;  
                Attack = 40;  
                Defense = 20;  
                Coin = 8;  
                Experience = 6;  
                break;  
            case 2:  
                name = @"骷髅队长";  
                HP = 400;  
                Attack = 90;  
                Defense = 50;  
                Coin = 15;  
                Experience = 12;  
                break;  
            case 3:  
                name = @"冥队长";  
                HP = 3500;  
                Attack = 1280;  
                Defense = 1000;  
                Coin = 112;  
                Experience = 100;  
                break;  
            case 4:  
                name = @"小蝙蝠";  
                HP = 100;  
                Attack = 20;  
                Defense = 5;  
                Coin = 3;  
                Experience = 3;  
                break;  
            case 5:  
                nam
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,