Robin Hud

Learn what a hud is and how to create one. Here is a sample project which you can download. Before watching this tutorial you might want to watch: How to make a scene.

Thanks

This entry was posted in Cocos2d and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

11 Responses to Robin Hud

  1. evanlws12 says:

    This tutorial is great! But i can’t seem to get it to work the other way. I have a timer in my HUD layer that needs to run a method on my game scene when it’s finished, but when I try to import my GameScene.h it gives me an error saying “Unknown type name HudLayer” where it says HudLayer *hud. Is there any way I can call a method the other way?

  2. Chen says:

    Love it, this is brilliant!

  3. JG says:

    Hello Mr. Ueland :)

    Thanks you so much for the explanations!
    I´m learing cocos all over the internet, and besides RayWenderlich´s tutorials, yours are very good to watch someone write the code and have it explained slowly in a different manner.

    Keep it up Bob, awesome stuff :)

    Greetings from luxemburg

  4. emir says:

    hi Bob,

    How would I add to the score from a different object? lets say when I press on the missles from your missle tutorial, the score goes up?

    Well the actual question is: how do I add a pointer to the hudlayer from the missle object?
    thanks for the great tutorials..

    Emir

    • Bob says:

      Hi Emir, You have to make changes in three files: HelloWorldLayer.m, HudLayer.h and HudLayer.m. Try something like this:

      // HelloWorldLayer.m
      #import "HelloWorldLayer.h"
      @implementation HelloWorldLayer
      @synthesize hud;

      +(CCScene *) scene
      {
      CCScene *scene = [CCScene node];
      HelloWorldLayer *layer = [HelloWorldLayer node];
      [scene addChild: layer];

      //add another layer to the scene
      HudLayer *anotherLayer = [HudLayer node];
      [scene addChild: anotherLayer];
      layer.hud=anotherLayer;
      anotherLayer.hello=layer; // the new line

      return scene;
      }

      // HudLayer.h
      #import "cocos2d.h"

      @class HelloWorldLayer; // new line
      @interface HudLayer : CCLayerColor
      {
      HelloWorldLayer *hello; // new line
      }
      @property (nonatomic,retain) HelloWorldLayer *hello; // new line

      @end

      // HudLayer.m
      #import "HudLayer.h"
      #import "HelloWorldLayer.h" // new line
      @implementation HudLayer
      @synthesize hello; // the new line

      • emir says:

        Thank you for the reply Bob, I’m sure I couldn’t get it right but after I put these lines, how do I add to the score from another layer than the “HelloWorldLayer”? there are 3 missles that are called from HelloWorldLayer.m that calls the missles from missile.m with the function from your tutorial:

        -(void) fire
        {
        missile *fuze = [[missile alloc] initWithLayer:self];
        [fuze release];
        counter++;
        And more code…
        }

        And I can add to the hud score from HelloWorldLayer too..

        But what I can’t do is;

        in missle.m there is a :

        -(void) removeme
        {
        //here I want to add to the score of hudlayer.m
        //but I can’t add a pointer to hudlayer..
        }

        I’m sorry If you already answered my question with you answer..

        • Bob says:

          HelloWorldLayer knows the pointer to HudLayer, so it can send it to the Missile object, for instance as a parameter when it creates the missiles. Hope that helps. Bob.

  5. Joey says:

    Hi Bob,

    How would you do this for multiple lives. For instance I got 4 players they all have lives.


    livesPlayer1 = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%d",_livesLeft] fntFile:@"livefont.fnt"];
    livesPlayer1.position = ccp(s.width/6,s.height/17);
    livesPlayer1.anchorPoint = ccp(0,0.5);
    [self addChild:livesPlayer1];

    An addlife method when the player touches an extra life item (using SpaceManager chipmunk to handle the collision)

    I’ve got an player class (cpCCSprite) all the players are subclassed from that class.

    In my Hudlayer I got this :

    Player *player1;
    Player *player2;
    Player *player3;
    Player *player4;

    How would I keep track of multiple players?

    -(void) addLife:(int)number
    {
    player1 = (Player *)player1;
    if (player1) {
    _livesLeft++;
    [livesPlayer1 setString:[NSString stringWithFormat:@"%d", _livesLeft]];
    }
    }

    Thanks,

    Joey

  6. Ralphus Royds says:

    Bob these tutorials are absolutely brilliant.

  7. Bob says:

    Thanks to Joey (apixxx@gmail.com) for suggesting this tutorial.

Leave a Reply