Cocos2D:塔防游戏制作之旅(十一)
是时候放一些坏家伙来搅合一下了!
打开HelloWorldLayer.h并且添加以下代码:
// Add these instance variables
int 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
Cocos2D:塔防游戏制作之旅(十一)的更多相关文章
- Cocos2D:塔防游戏制作之旅(十八)
在Enemy.m的getDamaged:方法只给你添加如下1行(在if条件内): [theGame awardGold:200]; 现在运行游戏你将注意到你不能放置超出你资源金币的炮塔了.当然杀死敌人 ...
- Cocos2D:塔防游戏制作之旅(一)
原文地址:http://www.raywenderlich.com/37701/how-to-make-a-tower-defense-game-tutorial 由Pablo Ruiz写的入门教程, ...
- Cocos2D:塔防游戏制作之旅(十六)
编译运行你的app,放置一些炮塔在你的地图上吧!你将看到炮塔在敌人移动如攻击范围时如何立即开始攻击,并且敌人的血条将随着攻击不断减少知道它们被人道毁灭!胜利即将来临了! 哦!Okay,这里只有少数细节 ...
- Cocos2D:塔防游戏制作之旅(二)
一个象牙塔的视图 如果你并不熟悉此类型的游戏,塔防游戏是一个战略游戏,你需要购买和将武装塔放置在战略位置,去阻止一波又一波的敌人到达并摧毁你的基地 每一波敌人都更强,这些更强的对手有着更快的速度和对于 ...
- Cocos2D:塔防游戏制作之旅(十七)
getHpDamage方法当敌人到达基地时被调用.你需要添加该方法到Enemy.m的update:方法中去,以便检查当敌人到达基地是会发生什么.幸运的是,你已经在之前的代码中实现这些了,你可以接着往下 ...
- Cocos2D:塔防游戏制作之旅(八)
如果所有东西通过检查,则创建一个新炮塔,将它放置在基座上,然后添加到towers数组中. 注意:在方法最后的bridge语法需要做一些解释.你下载的初始项目已经为一 些文件打开ARC,但不是Cocos ...
- Cocos2D:塔防游戏制作之旅(四)
让我们看一下项目的结构.在TowerDefense文件夹,你将找到: 含有Cocos2D文件的libs文件夹 含有所有图片和声音的资源文件夹 现在,你已经准备就绪准备开始建造炮台之旅了 ;) 放置炮塔 ...
- Cocos2D:塔防游戏制作之旅(三)
整合炮塔资源 为了快速开始,我们为你创建了开始的项目.它包括了一个空白的Cocos2D项目以及大多数你将在教程中使用到的资源. 所以首先下载该 开始项目 并且解压缩到你指定的位置中去. 注意:该项目的 ...
- Cocos2D:塔防游戏制作之旅(十四)
塔之战:炮塔的攻击 炮塔就位了?检查.敌人前进中?再次检查 - 它们看起来就是如此!看起来到了击溃这些家伙的时候了!这里我们将智能置入炮塔的代码中去. 每一个炮塔检查是否有敌人在其攻击范围.(炮塔一次 ...
随机推荐
- 两个对象用equals方法比较为true,它们的Hashcode值相同吗?
两个对象用equals方法比较为true,它们的Hashcode值相同吗? 答:不一定相同.正常情况下,因为equals()方法比较的就是对象在内存中的值,如果值相同,那么Hashcode值也应该相同 ...
- 去掉textarea和input在ios下默认出现的圆角
-webkit-appearance:none;/*清除ios默认圆角*/ border-radius:0;
- Linux下安装 mysql 5.7
安装环境:系统是 centos6.5 1.下载 下载地址:https://dev.mysql.com/downloads/file/?id=467556 下载版本:我这里选择的57.17,通用版,li ...
- SQL注入原理及绕过安全狗
1.什么是SQL注入攻击 SQL注入攻击指的是通过构造特殊的输入作为参数插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令 http://www.xxx.com/list. ...
- python脚本文件传参并通过token登录后爬取数据实例
from bs4 import BeautifulSoup import requests import sys class Zabbix(object): def __init__(self, he ...
- Cloudera: Start Impala service by cloudera manager in docker quickstart image
How to start Impala service in docker quickstart image docker run --hostname=quickstart.cloudera --p ...
- [CSDN_Markdown] 使用LaTeX写矩阵
简介 LaTeX 的公式功能非常强大,一次性讲全不是件容易的事情.将LaTeX 的这些功能分成较小的相互独立的部分来讲,一方面方便大家单独查阅:另一方面,所有[CSDN_Markdown]相关的文章都 ...
- Spring常用配置(二)
OK,上篇博客我们介绍了Spring中一些常见的配置,上篇博客中介绍到的都是非常常见的注解,但是在Spring框架中,常见的注解除了上篇博客提到的之外,还有许多其他的注解,只不过这些注解相对于上文提到 ...
- VBA find方法
Sub Sample() Dim sfzs As New Collection Dim ws, wbs, dbs As Worksheet Dim r As Long Set ws = ThisWor ...
- python 3 dict函数 神奇的参数规则
>>> dict({1:2},2=3)SyntaxError: keyword can't be an expression>>> dict({1:2},**{2: ...