作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个包含三个 Sprite 的自定义类“LinkWithNumber”在更新的 GameLayer 上,我正在尝试测试用于碰撞的 CGRectContainsRect 但是我在尝试访问类文件中的 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);
}
额外内容
最佳答案
我看到两个问题...
LinkWithNumber
节点的子节点,而不是场景。要递归搜索整个节点树,使用@"//collide"
LinkWithNumber
指针,但是 collide
是一个 SKSpriteNode
而不是 LinkWithNumber
.关于ios - 尝试从 GameLayer 访问类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997788/
我有一个包含三个 Sprite 的自定义类“LinkWithNumber”在更新的 GameLayer 上,我正在尝试测试用于碰撞的 CGRectContainsRect 但是我在尝试访问类文件中的
我创建了一个 HudLayer 类,它定义了按下按钮时发生的协议(protocol)。 -(id) init { if( (self=[super initWithColor:ccc4(255
所以我在名为 mainMenu 的文件中有以下代码: [[CCDirector sharedDirector] runWithScene:[GameLayer scene]]; 我有#import "
我是一名优秀的程序员,十分优秀!