Cocos2D: Anti-tower game production Tour (14th)

Source: Internet
Author: User

Cocos2D: Anti-tower game production Tour (14th)

Battle of towers: turret attacks

The turret is in place? Check. Is the enemy moving forward? Check again-they look like this! It looks like it's time to defeat these guys! Here we place the intelligence in the code of the turret.

Each turret checks whether an enemy is under attack. (The turret can only attack one enemy at a time. if yes, the turret will start to open fire to the enemy until either of the two things occurs: the enemy moves out of the attack range or the enemy is destroyed. the turret then began to look for other unsquashed guys:]

Put them together and create a new turret! You already have a foundation for defense!

Because the enemy and turret classes depend on each other, you have to update their Class header files first to avoid Xcode display errors when you modify the implementation code.

First, open the Tower. h file, and then complete the following modifications:

// Add some instance variablesBOOL attacking;Enemy *chosenEnemy;// Add method definition-(void)targetKilled;

Open the Enemy. h file and modify it as follows:

// Add instance variableNSMutableArray *attackedBy;// Add method definitions-(void)getAttacked:(Tower *)attacker;-(void)gotLostSight:(Tower *)attacker;-(void)getDamaged:(int)damage;

Next, go back to Tower. m and make the following changes:

// Import Enemy header at the top of the file:#import "Enemy.h"// Add the following methods-(void)attackEnemy{    [self schedule:@selector(shootWeapon) interval:fireRate];}-(void)chosenEnemyForAttack:(Enemy *)enemy{    chosenEnemy = nil;    chosenEnemy = enemy;    [self attackEnemy];    [enemy getAttacked:self];}-(void)shootWeapon{    CCSprite * bullet = [CCSprite spriteWithFile:@"bullet.png"];    [theGame addChild:bullet];    [bullet setPosition:mySprite.position];    [bullet runAction:[CCSequence actions:                       [CCMoveTo actionWithDuration:0.1 position:chosenEnemy.mySprite.position],                       [CCCallFunc actionWithTarget:self selector:@selector(damageEnemy)],                       [CCCallFuncN actionWithTarget:self selector:@selector(removeBullet:)], nil]];}-(void)removeBullet:(CCSprite *)bullet{    [bullet.parent removeChild:bullet cleanup:YES];}-(void)damageEnemy{    [chosenEnemy getDamaged:damage];}-(void)targetKilled{    if(chosenEnemy)        chosenEnemy =nil;    [self unschedule:@selector(shootWeapon)];}-(void)lostSightOfEnemy{    [chosenEnemy gotLostSight:self];    if(chosenEnemy)        chosenEnemy =nil;     [self unschedule:@selector(shootWeapon)];}

Finally, replace the blank update method that can only be left by the previous version:

-(void)update:(ccTime)dt {    if (chosenEnemy){        //We make it turn to target the enemy chosen        CGPoint normalized = ccpNormalize(ccp(chosenEnemy.mySprite.position.x-mySprite.position.x,                                              chosenEnemy.mySprite.position.y-mySprite.position.y));        mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x))+90;        if(![theGame circle:mySprite.position withRadius:attackRange              collisionWithCircle:chosenEnemy.mySprite.position collisionCircleRadius:1])        {            [self lostSightOfEnemy];        }    } else {        for(Enemy * enemy in theGame.enemies)        {            if([theGame circle:mySprite.position withRadius:attackRange                 collisionWithCircle:enemy.mySprite.position collisionCircleRadius:1])            {                [self chosenEnemyForAttack:enemy];                break;            }        }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.