c,没有标签或标识符的对象 [英] c, obj c enum without tag or identifier

查看:120
本文介绍了c,没有标签或标识符的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im learning cocos2d [open gl wrapper for objective C on iPhone],现在玩精灵已经在一个例子中发现了这个例子,

  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];

所以在枚举中定义的变量用于创建的sprite对象的标签名称, p>

但我不明白


  1. 为什么要将六进制值赋给标签以使用

  2. 带有标签的枚举

因为我知道这个枚举在obj C和C



  typedef枚举{
JPG,
PNG,
GIF,
PVR
} kImageType;

谢谢!

解决方案

枚举是自动分配的值,从0增加,但您可以分配自己的值。



如果没有指定任何值,他们将开始从0开始,如下所示:

  typedef枚举{
JPG,
PNG,
GIF,
PVR
} kImageType;

但是您可以为其分配值:

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

甚至

 code> typedef枚举{
JPG = 100,
PNG = 0x01,
GIF = 100,
PVR = 0xff
} kImageType;

任何你想要的,重复的值也可以。



我不知道为什么他们被给予这些具体的价值观,但他们可能有一些意义相关使用。


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!

解决方案

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.

这篇关于c,没有标签或标识符的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆