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

cocos2d-iphone之魔塔20层第十一部分

这部分我们要添加怪物查看了,首先我们看一下下面这张图:
屏幕快照 2013-02-28 下午3.51.47.png
这就是我们所要实现的效果图,我们很容易就能看到其相似的地方每个怪物显示的内容都差不多。
我们这里需要添加两个类MonsterInformation(查看怪物信息主界面)和MonsterInformationCell(单个怪物显示信息块)
首先我们来看一下MonsterInformation.h文件代码:
#import<Foundation/Foundation.h>
#import"cocos2d.h"
 
@class Enemy;
@class Hero;
@interface MonsterInformation : CCLayer 
{
    //背景
    CCTMXTiledMap *mapBackground;
    Hero *hero;
}
//当前图层怪物数组
@property (nonatomic,retain) NSMutableArray *mosterArray;
//怪物列表数量
@property (nonatomic,assign)int mosterListCount;
//是否移除界面
@property (nonatomic,assign)bool removeView;
//加载怪物列表
-(void)ViewInit:(Enemy*) monster;
//更新损失血量
-(void)updateLossHP;
@end
这个类有三个属性,第一个就是存储当前图层怪物信息的数组,第二个怪物列表的引用计数,第三个就是用于判断界面是否可以移除。
然后我们开始添加MonsterInformation.m文件代码:
#import"MonsterInformation.h"
#import"MonsterInformationCell.h"
#import"Monster.h"
#import"Hero.h"
 
@implementation MonsterInformation
@synthesize mosterArray;
@synthesize mosterListCount;
@synthesize removeView;
-(id)init
{
    if ((self = [super init])) 
    {
        //背景
        mapBackground = [CCTMXTiledMap tiledMapWithTMXFile:@"shopbg.tmx"];
        mapBackground.position = ccp(10,50);
        mapBackground.scale =2.2;
        [self addChild:mapBackground];
        self.mosterArray = [[[NSMutableArray alloc] init] autorelease];
    }
   returnself;
}
-(void)ViewInit:(Enemy*) monster
{
   //获取屏幕大小
   CGSize size = [[CCDirectorsharedDirector]winSize];
    //判断当前怪物信息数组是否空
    if (self.mosterArray.count ==0) 
    {
        //如果是则初始化怪物信息标签并添加到数组中
       mosterListCount =1;
       MonsterInformationCell *cell = [[[MonsterInformationCellalloc]init] autorelease];
        [cell viewInit:monster];
        cell.position =ccp(30, size.height -80*mosterListCount -14);
        [self.mosterArrayaddObject:cell];
        [selfaddChild:cell];
    }
    else
    {
        //如果不是则先判断一下当前怪物信息标签是否已经存在数组中
        bool sa = NO;
       for (MonsterInformationCell *cellinself.mosterArray) 
        {
            if (cell.enemy.enemyID == monster.enemyID) 
            {
                //如果在则怪物标签计数加1
                cell.count ++;
                sa =YES;
            }
        }
        if (!sa) 
        {
            //如果不在则初始化怪物信息标签并添加到数组中
                            mosterListCount +=1;
           MonsterInformationCell *cell = [[[MonsterInformationCellalloc]init] autorelease];
            [cell viewInit:monster];
            cell.position =ccp(30, size.height -80*mosterListCount -14);
            [self.mosterArrayaddObject:cell];
            [selfaddChild:cell];
        }
    }
}
-(void)updateLossHP
{
   for (MonsterInformationCell *cellinself.mosterArray) 
    {
        hero = [Hero getHero];
        int enemyHp,_heroHp;
        enemyHp = cell.enemy.HP;
        _heroHp = 0;
        if (hero.Attack > cell.enemy.Defense) 
        {
            do 
            {
                enemyHp -= (hero.Attack - cell.enemy.Defense);
                if (enemyHp >0) 
                {
                    if ((cell.enemy.Attack -hero.Defense) >0) 
                    {
                        _heroHp += (cell.enemy.Attack -hero.Defense);
                    }
                }
            } while (enemyHp >0);
            
            [cell updateLossHP:_heroHp];
        }
    }
}
@end
我们的查看怪物信息主界面有了,下面我们来讲一下MonsterInformationCell这个类,先看一下头文件:
#import<Foundation/Foundation.h>
#import"cocos2d.h"
#import"Monster.h"
 
@interface MonsterInformationCell :CCLayer 
{
   //勇士损失血量
    CCLabelTTF *label61;
}
@property (nonatomic,retain)Enemy *enemy;
@property (nonatomic,assign)int count;
//初始化怪物信息标签
-(void)viewInit:(Enemy*) monster;
//更新损失生命
-(void)updateLossHP:(int) lossHp;
@end
我们这个类只有一个需要更新的标签就是勇士损失血量标签,还有两个属性,一个是怪物,一个是怪物个数。还有两个方法,一个是初始化标签,一个是用于更新勇士要损失的血量。下面我们来看一下实现文件中的代码:
#import"MonsterInformationCell.h"
 
@implementation MonsterInformationCell
@synthesize enemy;
@synthesize count;
 
-(id) init
{
    if ((self = [superinit])) 
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,