试图从GameLayer访问该类

时间:2015-08-13 20:25:21

标签: ios sprite-kit

我有一个自定义类" LinkWithNumber"有三个精灵 我正在尝试测试更新中的GameLayer CGRectContainsRect for collisions 但是我在尝试访问类文件中的sprite时遇到了麻烦(我没有太多经验,所以我很可能搞砸了:P)

我尝试了以下内容:

LinkWithNumber.h

@interface LinkWithNumber : SKSpriteNode <SKPhysicsContactDelegate>
{
SKSpriteNode *collide;
}

LinkWithNumber.m

@synthesize collide;

   //add collision object to the class
    collide = [[SKSpriteNode alloc]initWithColor:[SKColor blueColor]
                            ...blah blah as normal
   [self addChild:collide];
   collide.name = @"collide";

GameLayer.h

@class LinkWithNumber;
@interface GameScene : SKScene <SKPhysicsContactDelegate>
{
LinkWithNumber* twoSpritesWithParticlesBridge;
}
@property (nonatomic, strong)LinkWithNumber* twoSpritesWithParticlesBridge;

GameLayer.m

@synthesize twoSpritesWithParticlesBridge;

    -(void)addStaticLinkedSpriteWithParticles
{
    twoSpritesWithParticlesBridge =
    [[LinkWithNumber alloc]initWithlinkSpriteA:@"RoseMine06"
                                       spriteB:@"RoseMine06"
                             andPlistAnimation:@"need to create animations"
                                   distbetween:300
                                  hasParticles:YES
                                ParticlesNamed:@"Fire"];
      [self addChild:self->twoSpritesWithParticlesBridge];


    twoSpritesWithParticlesBridge.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: twoSpritesWithParticlesBridge.frame.size];



}

-(void)update:(CFTimeInterval)currentTime {

LinkWithNumber *currentSprite = 
(LinkWithNumber*)[self  childNodeWithName:@"collide"];

 //NSLog(@"currentSprite Name @%@", currentSprite); //gets nil

if (CGRectContainsRect(myShip02.frame,currentSprite.frame)) {
NSLog(@"Hit barrier can pass");
        }
}

Any help would be appreciated :)

如何找到您的类对象...解决方案,感谢0x141E!

 //name it on setup inside your customCLass 
 //eg yourObject.name = @"collide";

   //Now in Gamelayer locate your object by recursive search
   //it will look for any object named @"//collide"
   //without the slashes it will only look on the game layer
   //but since we need to dig a bit further we need them!

 LinkWithNumber *currentSprite =
(LinkWithNumber*)[self  childNodeWithName:@"//collide"];
NSLog(@"LinkWithNumber is %@",NSStringFromClass([currentSprite class]));


    //do something with your object
    if (currentSprite.position.y >0 ) {
        NSLog(@"currentSprite Position %f",currentSprite.position.y);
    }

额外

Sknode Class ref for other search functions

How to enumerate all nodes

1 个答案:

答案 0 :(得分:3)

我看到两个问题......

  1. 您只搜索名为“collide”的节点的根节点(在本例中为场景),但该节点是LinkWithNumber节点的子节点而不是场景。要递归搜索整个节点树,请使用@"//collide"
  2. 您正在将搜索结果投射到LinkWithNumber指针,但collideSKSpriteNode而不是LinkWithNumber