c, obj c enum without tag or identifier

-1

im learning cocos2d [open gl wrapper for objective C on iPhone], and now playing with sprites have found this in a example,

 enum {  
easySprite =   0x0000000a,
mediumSprite = 0x0000000b,
hardSprite =   0x0000000c,
backButton =   0x0000000d,
magneticSprite = 0x0000000e,
magneticSprite2 = 0x0000000f
};

...

 -(id) init
 {...
  /second sprite
    TSprite *med = [TSprite spriteWithFile:@"butonB.png"]; //blue
    [med SetCanTrack:YES];
    [self addChild: med z:1 tag:mediumSprite];
    med.position=ccp(299,230);
    [TSprite track:med];

so the variable defined in the enum is used in the tag name of the created sprite object,

but i don understand

  1. why give values in hexa to the tags to use
  2. the enum with out tags

as I knew this enum in obj C and C

     typedef enum {
     JPG,
     PNG,
      GIF,
     PVR
     } kImageType;

thanks!

objective-c
c
ios
enums
asked on Stack Overflow Dec 7, 2011 by manuelBetancurt • edited Dec 7, 2011 by manuelBetancurt

3 Answers

3

Usually, when you are creating an enum, you want to use it as a type (variable, method parameters etc.).

In this case, it's just a way how to declare integer constants. Since thay don't want to use the enum as type, the name is not necessary.

Edit: Hexadecimal numbers are commonly used when the integer is a binary mask. You won't see any operators like +,-,*,/ used with such a number, you'll see bitwise operators (!, &, |, ^).

Every digit in a hexadecimal number represents 4 bits. The whole number is a 32-bit integer and by writing it in hexadecimal in this case, you are saying that you are using only the last four bits and the other bits can be used for something else. This wouldn't be obvious from a decimal number.

answered on Stack Overflow Dec 7, 2011 by Sulthan • edited Dec 7, 2011 by Sulthan
2

Enums are automatically assigned values, incremented from 0 but you can assign your own values.

If you don't specify any values they will be starting from 0 as in:

typedef enum {
 JPG,
 PNG,
 GIF,
 PVR
 } kImageType;

But you could assign them values:

typedef enum {
 JPG = 0,
 PNG = 1,
 GIF = 2,
 PVR = 3
 } kImageType;

or even

typedef enum {
 JPG = 100,
 PNG = 0x01,
 GIF = 100,
 PVR = 0xff
 } kImageType;

anything you want, repeating values are ok as well.

I'm not sure why they are given those specific values but they might have some meaning related to use.

answered on Stack Overflow Dec 7, 2011 by stefanB • edited Dec 19, 2011 by stefanB
1

Well, you seem to be working off a terrible example. :)

At least as far as enums are concerned. It's up to anyone to define the actual value of an enum entry, but there's no gain to use hex numbers and in particular there's no point in starting the hex numbers with a through f (10 to 15). The example will also work with this enum:

enum {  
easySprite = 10,
mediumSprite,
hardSprite,
backButton,
magneticSprite,
magneticSprite2
};

And unless there's some point in having the enumeration start with value 10, it will probably work without specifying any concrete values.

answered on Stack Overflow Dec 7, 2011 by LearnCocos2D

User contributions licensed under CC BY-SA 3.0