我有一个带有三个精灵的自定义类“LinkWithNumber”
在更新的GameLayer上,我正在尝试测试
CGRectContainsRect发生碰撞,但是我在尝试访问类文件中的精灵时遇到了麻烦(我没有太多经验,所以很可能我搞砸了: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

最佳答案

我看到两个问题...

  • 您仅在根节点(在本例中为场景)中搜索名为“碰撞”的节点,但是该节点是LinkWithNumber节点的子级,而不是场景。要递归搜索整个节点树,请使用@"//collide"
  • 您正在将搜索结果转换为LinkWithNumber指针,但是collideSKSpriteNode而不是LinkWithNumber
  • 10-08 02:44