
增加敌人
敌人出现的位置点


开始创建敌人
-(void)addEnemyAtX:(int)x y:(int)y {
CCSprite *enemy = [CCSprite spriteWithFile:@"enemy1.png"];
enemy.position = ccp(x, y);
[self addChild:enemy];
}
// in the init method – after creating the player
// iterate through objects, finding all enemy spawn points
// create an enemy for each one
NSMutableDictionary * spawnPoint;
for (spawnPoint in [objects objects]) {
if ([[spawnPoint valueForKey:@"Enemy"] intValue] == 1){
x = [[spawnPoint valueForKey:@"x"] intValue];
y = [[spawnPoint valueForKey:@"y"] intValue];
[self addEnemyAtX:x y:y];
}
}

使它们移动
- (void) enemyMoveFinished:(id)sender {
CCSprite *enemy = (CCSprite *)sender;
[self animateEnemy: enemy];
}
// a method to move the enemy 10 pixels toward the player
- (void) animateEnemy:(CCSprite*)enemy
{
// speed of the enemy
ccTime actualDuration = 0.3;
// Create the actions
id actionMove = [CCMoveBy actionWithDuration:actualDuration
position:ccpMult(ccpNormalize(ccpSub(_player.position,enemy.position)), 10)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:@selector(enemyMoveFinished:)];
[enemy runAction:
[CCSequence actions:actionMove, actionMoveDone, nil]];
}
// add this at the end of addEnemyAtX:y:
// Use our animation method and
// start the enemy moving toward the player
[self animateEnemy:enemy];

//rotate to face the player
CGPoint diff = ccpSub(_player.position,enemy.position);
float angleRadians = atanf((float)diff.y / (float)diff.x);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
if (diff.x < 0) {
cocosAngle += 180;
}

忍者飞镖
// because our two layers need to reference each other
@class HelloWorld;
// inside the HelloWorldHud class declaration
HelloWorld *_gameLayer;
// After the class declaration
@property (nonatomic, assign) HelloWorld *gameLayer;
// Inside the HelloWorld class declaration
int _mode;
// After the class declaration
@property (nonatomic, assign) int mode;
@synthesize gameLayer = _gameLayer;
// At the top of the HelloWorld implementation
@synthesize mode = _mode;
// in HelloWorld’s init method
_mode = 0;
// in HelloWorld’s scene method
// after layer.hud = hud
hud.gameLayer = layer;
// in HelloWorldHud’s init method
// define the button
CCMenuItem *on;
CCMenuItem *off;
on = [[CCMenuItemImage itemFromNormalImage:@"projectile-button-on.png"
selectedImage:@"projectile-button-on.png" target:nil selector:nil] retain];
off = [[CCMenuItemImage itemFromNormalImage:@"projectile-button-off.png"
selectedImage:@"projectile-button-off.png" target:nil selector:nil] retain];
CCMenuItemToggle *toggleItem = [CCMenuItemToggle itemWithTarget:self
selector:@selector(projectileButtonTapped:) items:off, on, nil];
CCMenu *toggleMenu = [CCMenu menuWithItems:toggleItem, nil];
toggleMenu.position = ccp(100, 32);
[self addChild:toggleMenu];
// in HelloWorldHud
//callback for the button
//mode 0 = moving mode
//mode 1 = ninja star throwing mode
- (void)projectileButtonTapped:(id)sender
{
if (_gameLayer.mode == 1) {
_gameLayer.mode = 0;
} else {
_gameLayer.mode = 1;
}
}

发射飞镖
// old contents of ccTouchEnded:withEvent:
} else {
// code to throw ninja stars will go here
}
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
}
// code to throw ninja stars will go here
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
// Create a projectile and put it at the player’s location
CCSprite *projectile = [CCSprite spriteWithFile:@"Projectile.png"];
projectile.position = _player.position;
[self addChild:projectile];
// Determine where we wish to shoot the projectile to
int realX;
// Are we shooting to the left or right?
CGPoint diff = ccpSub(touchLocation, _player.position);
if (diff.x > 0)
{
realX = (_tileMap.mapSize.width * _tileMap.tileSize.width) +
(projectile.contentSize.width/2);
} else {
realX = -(_tileMap.mapSize.width * _tileMap.tileSize.width) -
(projectile.contentSize.width/2);
}
float ratio = (float) diff.y / (float) diff.x;
int realY = ((realX - projectile.position.x) * ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far we’re shooting
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:@selector(projectileMoveFinished:)];
[projectile runAction:
[CCSequence actionOne:
[CCMoveTo actionWithDuration: realMoveDuration
position: realDest]
two: actionMoveDone]];

碰撞检测
NSMutableArray *_projectiles;
[_projectiles addObject:projectile];
// at the end of projectileMoveFinished:
[_projectiles removeObject:sprite];
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
// iterate through projectiles
for (CCSprite *projectile in _projectiles) {
CGRect projectileRect = CGRectMake(
projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
// iterate through enemies, see if any intersect with current projectile
for (CCSprite *target in _enemies) {
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);
if (CGRectIntersectsRect(projectileRect, targetRect)) {
[targetsToDelete addObject:target];
}
}
// delete all hit enemies
for (CCSprite *target in targetsToDelete) {
[_enemies removeObject:target];
[self removeChild:target cleanup:YES];
}
if (targetsToDelete.count > 0) {
// add the projectile to the list of ones to remove
[projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
}
// remove all the projectiles that hit.
for (CCSprite *projectile in projectilesToDelete) {
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}
[projectilesToDelete release];
}
// because addEnemyAtX:y: uses these arrays.
_enemies = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
[self schedule:@selector(testCollisions:)];
胜利和失败
The Game Over Scene
@interface GameOverLayer : CCColorLayer {
CCLabel *_label;
}
@property (nonatomic, retain) CCLabel *label;
@end
@interface GameOverScene : CCScene {
GameOverLayer *_layer;
}
@property (nonatomic, retain) GameOverLayer *layer;
@end
#import ”HelloWorldScene.h”
@implementation GameOverScene
@synthesize layer = _layer;
- (id)init {
if ((self = [super init])) {
self.layer = [GameOverLayer node];
[self addChild:_layer];
}
return self;
}
- (void)dealloc {
[_layer release];
_layer = nil;
[super dealloc];
}
@end
@implementation GameOverLayer
@synthesize label = _label;
-(id) init
{
if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.label = [CCLabel labelWithString:@"" fontName:@"Arial" fontSize:32];
_label.color = ccc3(0,0,0);
_label.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_label];
[self runAction:[CCSequence actions:
[CCDelayTime actionWithDuration:3],
[CCCallFunc actionWithTarget:self selector:@selector(gameOverDone)],
nil]];
}
return self;
}
- (void)gameOverDone {
[[CCDirector sharedDirector] replaceScene:[HelloWorld scene]];
}
- (void)dealloc {
[_label release];
_label = nil;
[super dealloc];
}
@end
胜利场景
if (_numCollected == 2) {
[self win];
}
GameOverScene *gameOverScene = [GameOverScene node];
[gameOverScene.layer.label setString:@"You Win!"];
[[CCDirector sharedDirector] replaceScene:gameOverScene];
}

失败场景
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height );
if (CGRectContainsPoint(targetRect, _player.position)) {
[self lose];
}
}
GameOverScene *gameOverScene = [GameOverScene node];
[gameOverScene.layer.label setString:@"You Lose!"];
[[CCDirector sharedDirector] replaceScene:gameOverScene];
}

完整源代码
接下来怎么做?
- 增加多个关卡
- 增加不同类型的敌人
- 在Hud层中显示血条和玩家生命
- 制作更多的道具,比如加血的,武器等等
- 一个菜单系统,可以选择关卡,关闭音效,等等
- 使用更好的用户界面,来使游戏画面更加精美,投掷飞镖更加潇洒。
原创文章,转载请注明: 转载自DEVDIV博客-History