Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 getHPDAR_253_11845@age];
            [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,1.0));

    ccDrawSolidRect(ccp(myPosition.x+HEALTH_BAR_ORIGIN,ccp(myPosition.x+HEALTH_BAR_ORIGIN + (float)(currentHp * HEALTH_BAR_WIDTH)/maxHp,ccc4f(0,1.0,1.0));
}

@end

大佬总结

以上是大佬教程为你收集整理的Cocos2D:塔防游戏制作之旅(十一)全部内容,希望文章能够帮你解决Cocos2D:塔防游戏制作之旅(十一)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。