我有一个自定义类" 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);
}
额外
答案 0 :(得分:3)
我看到两个问题......
LinkWithNumber
节点的子节点而不是场景。要递归搜索整个节点树,请使用@"//collide"
LinkWithNumber
指针,但collide
是SKSpriteNode
而不是LinkWithNumber
。