RPG Battle with Cocos2d part three.

Finally part three of the RPG Battle is here. We will make it so the player can only attack after the monster is done attacking, delay the monster attack by two seconds, allow the player to level up and add in saving. If you haven’t already download the code from part two and lets get started.

Lets start by making it so the player can only attack after the monster is done attacking.
In RpgBattel.h add this line.

bool isAttacking;

Now add these lines to RpgBattel.m
These two lines should be added right inside animatePlayerAttack function.

if(isAttacking){return;}
        isAttacking=TRUE;

The first line will return(exit out) from the function if the player or monster is attacking. The second line tells the program that the player and monster will be attacking.
Then right before the end tag of the monsterAttack function add this line.

isAttacking=FALSE;

This line tells the program that the player and the monster are no longer attacking.

On to delaying the monster attack by two seconds.
Ok in RpgBattel.h add

double monsterTimmer;

Now in RpgBattel.m add in the init method.

monsterTimmer=0;
[self schedule:@selector(attackTime:) interval:1.0];

With the second line of code we are starting a timer that will run the code in attackTime every second.
So now we need to create the attackTime function.

-(void)attackTime:(ccTime)dt
{
        double now = [[NSDate date] timeIntervalSince1970];
        if(monsterTimmer != 0 && now – monsterTimmer >= 2)
        {
                [self animateMonsterAttack:self];
                monsterTimmer=0;
        }

}

So attackTime will check every second to see if the monsterTimmer is not 0 and two seconds have past sense the player finished attacking.
So we need to modify the animatePlayerAttack function
Change the sprite runAction to the following.

[sprite runAction:[CCSequence actions:[CCMoveTo actionWithDuration:2 position:_monster.position],
                                           [CCCallFuncN actionWithTarget:self selector:@selector(spriteDone:)],
                                           [CCCallFuncN actionWithTarget:self selector:@selector(PlayerAttack:)],
                                           [CCCallFuncN actionWithTarget:self selector:@selector(startMonsterTimer:)],nil]];

We have added the startMonsterTimer so when the player is done attacking the startMonsterTimer function will run. Now we need to add the startMonsterTimer function.

-(void)startMonsterTimer:(id)sender
{
        monsterTimmer = [[NSDate date] timeIntervalSince1970];
}

Now the player will attack and then the monster will attack two seconds later.

So now we want to allow the player to level up. So we need to create a playerLevel and a playerExp.
In RpgBattel.h add the following.

CCLabel *_labelLevel;
CCLabel *_labelExp;
int playerLevel;
int playerExp;

In RpgBattel.m add the following to the init function.

playerExp=0;
playerLevel=1;

and in the reset function and this.

_labelLevel = [CCLabel labelWithString:@"hello" fontName:@"Helvetica" fontSize:15];
_labelLevel.color = ccc3(0,0,0);
_labelLevel.position = ccp(300, 90);
[self addChild:_labelLevel z:2];
_labelExp = [CCLabel labelWithString:@"hello" fontName:@"Helvetica" fontSize:15];
_labelExp.color = ccc3(0,0,0);
_labelExp.position = ccp(300, 70);
[self addChild:_labelExp z:2];
[_labelLevel setString:[NSString stringWithFormat:@"Player Level: %d", playerLevel]];
[_labelExp setString:[NSString stringWithFormat:@"Player Experience: %d", playerExp]];
monsterTimmer=0;
<a href="http://britneyleejohnson.com/wp-content/uploads/2010/12/RpgGame3.zip">Download the code to RpgGame3 here.</a>[self schedule:@selector(attackTime:) interval:1.0];

When you defeat a monster you will want to change how much experience the player has. So in the PlayerAttack change this

else {
_monster.curHp =0;
[_labelHp2 setString:[NSString stringWithFormat:@"Monster HP: %d/%d", _monster.curHp, _monster.maxHp]];
[_labelMp2 setString:[NSString stringWithFormat:@"Monster HP: %d/%d", _monster.curHp, _monster.maxHp]];
}

to this

else {
_monster.curHp =0;
[_labelHp2 setString:[NSString stringWithFormat:@"Monster HP: %d/%d", _monster.curHp, _monster.maxHp]];
[_labelMp2 setString:[NSString stringWithFormat:@"Monster HP: %d/%d", _monster.curHp, _monster.maxHp]];
playerExp+=_monster.maxHp;
[_labelExp setString:[NSString stringWithFormat:@"Player Experience: %d", playerExp]];
        if(playerExp>=playerLevel*100)
        {
                playerExp=playerExp-playerLevel*100;
                playerLevel++;
                [_labelLevel setString:[NSString stringWithFormat:@"Player Level: %d", playerLevel]];
        }
}

This code is run when the player defeats a monster. It increases the player’s experience by the monster’s starting HP. Then it checks to see if the playerExp is high enough to level up. If it is then the player levels up and the amount of the playerExp that it takes to level up is subtracted from playerExp. The last thing we need to do is add in the ability to save our player’s level and experience.

Add the following to your AppDelegate.m

+ (void)initialize{
        NSDictionary *appDefaults = [NSDictionary
                                                                 dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                                                                [NSNumber numberWithInt:0],
                                                                                                                [NSNumber numberWithInt:0],
                                                                                                                nil]
                                                                 forKeys:[NSArray arrayWithObjects:
                                                                                  @"level",
                                                                                  @"exp",
                                                                                  nil]];
    [[NSUserDefaults standardUserDefaults]
         registerDefaults:appDefaults];
}

Then change the init method from this

playerExp=0;
playerLevel=0;

to this

playerExp=[[NSUserDefaults standardUserDefaults]
                                        integerForKey:@"exp"];
playerLevel=[[NSUserDefaults standardUserDefaults]
                                          integerForKey:@"level"];

Now edit the PlayerAttack function from this

[_labelExp setString:[NSString stringWithFormat:@"Player Experience: %d", playerExp]];
if(playerExp>=playerLevel*100)
{
        playerExp=playerExp-playerLevel*100;
        playerLevel++;
        [_labelLevel setString:[NSString stringWithFormat:@"Player Level: %d", playerLevel]];
}

to this

[_labelExp setString:[NSString stringWithFormat:@"Player Experience: %d", playerExp]];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:playerExp] forKey:@"exp"];
if(playerExp>=playerLevel*100)
{
        playerExp=playerExp-playerLevel*100;
        playerLevel++;
        [_labelLevel setString:[NSString stringWithFormat:@"Player Level: %d", playerLevel]];
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:playerLevel] forKey:@"level"];
[_labelExp setString:[NSString stringWithFormat:@"Player Experience: %d", playerExp]];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:playerExp] forKey:@"exp"];
}
[self removeChild:_labelExp cleanup:YES];
[self removeChild:_labelLevel cleanup:YES];
[self removeChild:_labelHp2 cleanup:YES];
[self removeChild:_labelHp cleanup:YES];
[self removeChild:_labelMp cleanup:YES];
[self removeChild:_labelMp2 cleanup:YES];
RpgGameAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate loadMainScene];

Now we have saved our game, and because once the monsters hp reached 0 we won so we will return to the beginning scene. All we have to do is add one more function to our appDelegate.

-(void)loadMainScene{
HelloWorld * ms = [HelloWorld node];
[[CCDirector sharedDirector] replaceScene:ms];
}

Ok it is not very pretty but it works.
Download the code to RpgGame3 here.

This entry was posted in cocos2d. Bookmark the permalink.

5 Responses to RPG Battle with Cocos2d part three.

  1. chris420 says:

    Awsome Thank you soo much ive been looking very foward to this Post :)

    Any news on the realease of your game?

  2. chris420 says:

    Hi im not sure if im doing somthing wrong but when i load the fight again after i win once , i shoot him and then he stops attacking and i get stuck in that spot. Also im not sure how to save the game, Thanks so much for your time and posts! :)

    • admin says:

      Warning: Missing argument 1 for GeSHi::GeSHi(), called in /home/content/97/6545297/html/wp-content/plugins/codecolorer/codecolorer-core.php on line 155 and defined in /home/content/97/6545297/html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 432

      Warning: Missing argument 2 for GeSHi::GeSHi(), called in /home/content/97/6545297/html/wp-content/plugins/codecolorer/codecolorer-core.php on line 155 and defined in /home/content/97/6545297/html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 432

      Sorry I left out two lines of code at the end of the reset function add this

      monsterTimmer=0;
          [self schedule:@selector(attackTime:) interval:1.0];
  3. CSMS says:

    Good info, thans for your posting

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>