• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - Sprite Kit - 无法从对象获取自定义类数据

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:13:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试从我的自定义类 SKNode 对象中获取值。问题是当我触摸对象时,无论我触摸什么对象,它都会给我相同的值。 (蓝色按钮),我无法获得之前设置的 buttonID、buttonType。

一切正常,除非我需要获取我触摸或拖动的对象的 buttonType、buttonID。

我不确定我哪里出错了,任何帮助或朝着正确方向前进都会很棒。谢谢。

这是我的自定义类 .h 文件。

 #import <SpriteKit/SpriteKit.h>

  @interface ButtonNode : SKNode {

          SKNode *buttonCustom;

  }

  @property int buttonType, buttonColumn, buttonID, xPos, yPos;

  @property NSString *buttonName;

   -(id)initWithButtonTypeint)buttonType;


  @end

这是我的自定义类 .m 文件

#import "ButtonNode.h"


 #define kBoxSize CGSizeMake(40, 40)


 @implementation ButtonNode

 @synthesize buttonColumn,buttonType,buttonID,xPos,yPos,buttonName;


 static const uint32_t blueCategory = 1 << 0;
 static const uint32_t redCategory = 1 << 1;
 static const uint32_t greenCategory = 1 << 2;
 static const uint32_t yellowCategory = 1 << 3;


-(id)initWithButtonTypeint)buttonType {
     self = [super init];

     if (buttonType == 1) {
        NSLog(@"BLUE BUTTON CREATE");
         [self addButtonBlue];
     }
    if (buttonType == 2) {
       NSLog(@"RED BUTTON CREATE");
        [self addButtonRed];
    }
   if (buttonType == 3) {
      NSLog(@"Green BUTTON CREATE");
       [self addButtonGreen];
   }
   if (buttonType == 4) {
     NSLog(@"Yellow BUTTON CREATE");
      [self addButtonYellow];
   }
     return self;
 }

- (void) addButtonBlue {

    SKSpriteNode *rect;

       //button type 1
        rect = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:kBoxSize];

        int tmpInt = [[NSDate date] timeIntervalSince1970];
        NSString *tmpName = [NSString stringWithFormat"%i", tmpInt];
        rect.name = tmpName; //unique name.

       rect.name = @"1";

       rect.physicsBody.categoryBitMask = blueCategory;
       rect.physicsBody.contactTestBitMask = blueCategory;
       rect.physicsBody.collisionBitMask = blueCategory | redCategory | yellowCategory | greenCategory;

        rect.position = CGPointMake(xPos , yPos );
       [self addChild:rect];
}

- (void) addButtonRed {

    SKSpriteNode *rect;

   //button type 2
   rect = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:kBoxSize];

   int tmpInt = [[NSDate date] timeIntervalSince1970];
   NSString *tmpName = [NSString stringWithFormat"%i", tmpInt];
   rect.name = tmpName; //unique name.

   rect.name = @"2";

    rect.physicsBody.categoryBitMask = redCategory;
    rect.physicsBody.contactTestBitMask = redCategory;
    rect.physicsBody.collisionBitMask = blueCategory | redCategory | yellowCategory | greenCategory;

   rect.position = CGPointMake(xPos , yPos );
    [self addChild:rect];
}

- (void) addButtonGreen {

    SKSpriteNode *rect;

    //button type 3
    rect = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:kBoxSize];

    int tmpInt = [[NSDate date] timeIntervalSince1970];
    NSString *tmpName = [NSString stringWithFormat"%i", tmpInt];

   rect.name = tmpName; //unique name.

   rect.name = @"3";

   rect.physicsBody.categoryBitMask = greenCategory;
   rect.physicsBody.contactTestBitMask = greenCategory;
   rect.physicsBody.collisionBitMask = blueCategory | redCategory | yellowCategory | greenCategory;

   rect.position = CGPointMake(xPos , yPos );
   [self addChild:rect];
}

- (void) addButtonYellow {

   SKSpriteNode *rect;

   //button type 4
   rect = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:kBoxSize];

   int tmpInt = [[NSDate date] timeIntervalSince1970];
   NSString *tmpName = [NSString stringWithFormat"%i", tmpInt];

   rect.name = tmpName; //unique name.

   rect.name = @"4";

   rect.physicsBody.mass = 1;
   rect.physicsBody.categoryBitMask = yellowCategory;
   rect.physicsBody.contactTestBitMask = yellowCategory;
   rect.physicsBody.collisionBitMask = blueCategory | redCategory | yellowCategory | greenCategory;

   rect.position = CGPointMake(xPos , yPos );
   [self addChild:rect];
}

@end

这里是我创建按钮的地方。 (在文件顶部与全局 ivar 的其余部分一起) ButtonNode * newButton;

    for (int i = 1; i <= 6; i++) {

    //create random Int
    int tmpInt = arc4random() %3;

    NSLog(@"tmp %i" ,tmpInt);

    column1.position = CGPointMake(100, self.frame.size.height - 40);

    if (tmpInt == 0) {
        //button type 1
        newButton = [[ButtonNode alloc] initWithButtonType:1];
        newButton.xPos = column1.position.x;
        newButton.yPos = column1.position.y *i;
        newButton.buttonID = 344224351; //unique name
        newButton.buttonColumn = 2;
        newButton.buttonType = 1;
        [column1 addChild:newButton];
        blueTotal++;
        totalButtons++;
        column1Total++;
}
    if (tmpInt == 1) {
        //button type 2
        newButton = [[ButtonNode alloc] initWithButtonType:2];
        newButton.xPos = column1.position.x;
        newButton.yPos = column1.position.y *i;
        newButton.buttonID = 344224351; //unique name
        newButton.buttonColumn = 2;
        newButton.buttonType = 1;
        [column1 addChild:newButton];
        redTotal++;
        totalButtons++;
        column1Total++;
    }

 }

这是无法正常工作的部分。

     - (void) touchesBeganNSSet *)touches withEventUIEvent *)event
{
    for (UITouch *touch in touches) {

       UITouch* touch = [touches anyObject];
       CGPoint loc = [touch locationInNode:self];
       NSArray *nodes = [self nodesAtPoint:loc];

       for (SKNode *nod in nodes) {

          NSString *tmp = nod.name;
             if (tmp.length !=0) {

                NSString * tmpType = nod.name;

               if ([tmpType isEqualToString"1"]) {
                    NSLog(@"Node Type: %@", nod.name);
                    previousButton = @"1";
                    NSLog (@"%d",newButton.buttonType);
               }

               if ([tmpType isEqualToString"2"]) {
                   NSLog(@"Node Type: %@", nod.name);
                   previousButton = @"2";
                   NSLog (@"%d",newButton.buttonType);
               }

              if ([tmpType isEqualToString"3"]) {
                NSLog(@"Node Type: %@", nod.name);
                 previousButton = @"3";
                  NSLog (@"%d",newButton.buttonType);

            }

            if ([tmpType isEqualToString"4"]) {
                NSLog(@"Node Type: %@", nod.name);
                previousButton = @"4";
                NSLog (@"%d",newButton.buttonType);

            }

           }
       }
    }
}



Best Answer-推荐答案


SKNode 没有这些属性。

在你的 for 循环中试试这个:

ButtonNode *myButton = (ButtonNode *)nod;

这会将 nod 正确地转换为 ButtonNode,您可以像这样使用 myButton:

NSLog(@"%d",myButton.buttonType);

那么您应该能够访问您在 ButtonNode 类中定义的属性。

如果您确定它是一个 ButtonNode,您可能只想进行该转换,但只是想帮助您理解为什么在您当前的代码下永远无法访问这些属性。

此外,您在 touchesBegan 的循环中对 newButton 的使用并不是我认为您“认为”的那样。这将是创建的最后一个按钮,而不是当前节点存储在循环中的 nod 中。

关于ios - Sprite Kit - 无法从对象获取自定义类数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506357/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap