Cocos2D:塔防游戏制作之旅(十一)
来源:互联网 发布:木工柜门半盖怎么算法 编辑:程序博客网 时间:2024/05/04 08:03
是时候放一些坏家伙来搅合一下了!
打开HelloWorldLayer.h并且添加以下代码:
// Add these instance variablesint wave;CCLabelBMFont *ui_wave_lbl;// Add the following property to the properties section@property (nonatomic,strong) NSMutableArray *enemies;
使HelloWorldLayer.m文件修改如下:
// Synthesize enemies@synthesize enemies;
现在到了创建保存敌人信息并且管理它们如何在屏幕上移动的类了.创建一个新的类,名字为Enemy,继承于CCNode.
将Enemy.h替换为如下内容:
#import "cocos2d.h"#import "HelloWorldLayer.h"@class HelloWorldLayer, Waypoint, Tower;@interface Enemy: CCNode { CGPoint myPosition; int maxHp; int currentHp; float walkingSpeed; Waypoint *destinationWaypoint; BOOL active;}@property (nonatomic,assign) HelloWorldLayer *theGame;@property (nonatomic,assign) CCSprite *mySprite;+(id)nodeWithTheGame:(HelloWorldLayer*)_game;-(id)initWithTheGame:(HelloWorldLayer *)_game;-(void)doActivate;-(void)getRemoved;@end
现在将Enemy.m文件替换为如下内容:
#import "Enemy.h"#import "Tower.h"#import "Waypoint.h"#define HEALTH_BAR_WIDTH 20#define HEALTH_BAR_ORIGIN -10@implementation Enemy@synthesize mySprite, theGame;+(id)nodeWithTheGame:(HelloWorldLayer*)_game { return [[self alloc] initWithTheGame:_game];}-(id)initWithTheGame:(HelloWorldLayer *)_game { if ((self=[super init])) { theGame = _game; maxHp = 40; currentHp = maxHp; active = NO; walkingSpeed = 0.5; mySprite = [CCSprite spriteWithFile:@"enemy.png"]; [self addChild:mySprite]; Waypoint * waypoint = (Waypoint *)[theGame.waypoints objectAtIndex:([theGame.waypoints count]-1)]; destinationWaypoint = waypoint.nextWaypoint; CGPoint pos = waypoint.myPosition; myPosition = pos; [mySprite setPosition:pos]; [theGame addChild:self]; [self scheduleUpdate]; } return self;}-(void)doActivate{ active = YES;}-(void)update:(ccTime)dt{ if(!active)return; if([theGame circle:myPosition withRadius:1 collisionWithCircle:destinationWaypoint.myPosition collisionCircleRadius:1]) { if(destinationWaypoint.nextWaypoint) { destinationWaypoint = destinationWaypoint.nextWaypoint; }else { //Reached the end of the road. Damage the player [theGame getHpDamage]; [self getRemoved]; } } CGPoint targetPoint = destinationWaypoint.myPosition; float movementSpeed = walkingSpeed; CGPoint normalized = ccpNormalize(ccp(targetPoint.x-myPosition.x,targetPoint.y-myPosition.y)); mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x)); myPosition = ccp(myPosition.x+normalized.x * movementSpeed, myPosition.y+normalized.y * movementSpeed); [mySprite setPosition:myPosition];}-(void)getRemoved{ [self.parent removeChild:self cleanup:YES]; [theGame.enemies removeObject:self]; //Notify the game that we killed an enemy so we can check if we can send another wave [theGame enemyGotKilled];}-(void)draw{ ccDrawSolidRect(ccp(myPosition.x+HEALTH_BAR_ORIGIN, myPosition.y+16), ccp(myPosition.x+HEALTH_BAR_ORIGIN+HEALTH_BAR_WIDTH, myPosition.y+14), ccc4f(1.0, 0, 0, 1.0)); ccDrawSolidRect(ccp(myPosition.x+HEALTH_BAR_ORIGIN, myPosition.y+16), ccp(myPosition.x+HEALTH_BAR_ORIGIN + (float)(currentHp * HEALTH_BAR_WIDTH)/maxHp, myPosition.y+14), ccc4f(0, 1.0, 0, 1.0));}@end
0 0
- Cocos2D:塔防游戏制作之旅(十一)
- Cocos2D:塔防游戏制作之旅(一)
- Cocos2D:塔防游戏制作之旅(二)
- Cocos2D:塔防游戏制作之旅(三)
- Cocos2D:塔防游戏制作之旅(四)
- Cocos2D:塔防游戏制作之旅(五)
- Cocos2D:塔防游戏制作之旅(六)
- Cocos2D:塔防游戏制作之旅(七)
- Cocos2D:塔防游戏制作之旅(八)
- Cocos2D:塔防游戏制作之旅(九)
- Cocos2D:塔防游戏制作之旅(十)
- Cocos2D:塔防游戏制作之旅(十二)
- Cocos2D:塔防游戏制作之旅(十三)
- Cocos2D:塔防游戏制作之旅(十四)
- Cocos2D:塔防游戏制作之旅(十五)
- Cocos2D:塔防游戏制作之旅(十六)
- Cocos2D:塔防游戏制作之旅(十七)
- Cocos2D:塔防游戏制作之旅(十八)
- IOS开发之TabBarItem&NavigationBarItem
- LeetCode OJ 系列之283 Move Zeroes --Python
- Ⅰ.2.5 如何使用Qt Signal Handles
- Android 多线程处理之多线程用法大集合
- C# Eval
- Cocos2D:塔防游戏制作之旅(十一)
- Google Java编程风格指南
- 黑马程序员——网络编程(二)
- 实例讲解如何利用jQuery设置图片居中放大或者缩小
- 凸优化--强弱对偶性的几何解释
- [leetcode 249] Group Shifted Strings
- java集合框架集合框架是为表示和操作集合而规定的一种统一的标准的体系结构。任何集合框架都包含三大块内容:对外的接口、接口的实现和对集合运算的算法。
- Evaluate Video Quality
- 拷贝项目文件夹中的文件存放到本地