gpt4 book ai didi

ios - 尝试从 GameLayer 访问类

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:02 25 4
gpt4 key购买 nike

我有一个包含三个 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);
}

额外内容

Sknode Class ref for other search functions

How to enumerate all nodes

最佳答案

我看到两个问题...

  1. 您仅在根节点(在本例中为场景)中搜索名为“碰撞”的节点,但该节点是 LinkWithNumber 节点的子节点,而不是场景。要递归搜索整个节点树,使用@"//collide"
  2. 您正在将搜索结果转换为 LinkWithNumber 指针,但是 collide 是一个 SKSpriteNode 而不是 LinkWithNumber.

关于ios - 尝试从 GameLayer 访问类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com