【问题标题】:iOS SKSpriteNode Error: assigning to 'skspritenode *const __strong' from incompatible type 'CGRect' (aka 'struct CGRect')iOS SKSpriteNode 错误:从不兼容类型“CGRect”(又名“struct CGRect”)分配给“skspritenode *const __strong”
【发布时间】:2014-07-16 13:54:35
【问题描述】:

我正在使用 sprite kit (objective-c) 开发一个 Xcode 项目 我收到了这个错误,我不明白如何解决它,但我试图让两个节点在它们发生冲突时记录下来。

iOS SKSpriteNode 错误:

assigning to 'skspritenode *const __strong' from incompatible type 'CGRect' (aka 'struct                   CGRect')

我的代码在这里:

//This is the .m file

-(void) Clouds{
SKSpriteNode* character = [BBCharacter  spriteNodeWithImageNamed:@"character1"];

[self enumerateChildNodesWithName:@"cloud1" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.x < -380 || node.position.y < 0){ //node.position.x < -380
        [node removeFromParent];
        NSLog(@"DELETE");
    }
    else{
        node.position = CGPointMake(node.position.x - 1, node.position.y);

    }

    if (CGRectIntersectsRect (character, cloud1)) {
        NSLog(@"Intersection");
    }


}];
}
//This is the .h file
@interface{
}
@property (nonatomic, strong) BBCharacter *playerSprite;
@end

我跳过了一堆东西,可能拼错了…… 谢谢! 迈克尔

【问题讨论】:

  • CGRectIntersectsRect 参数需要为CGRect。您似乎在传递字符和云,它们似乎是SKSpriteNode
  • 有什么我可以做的吗?我需要改变什么?
  • 如果我所说的对您来说没有 100% 的意义,这是本末倒置的情况。您需要退后一步,更好地理解变量类型。如果你愿意花时间学习,网上有很多教程和参考资料。
  • 我明白你的意思,我只是不明白我能做些什么......
  • 试试 character.frame 和 cloud1.frame

标签: objective-c xcode sprite-kit collision skspritenode


【解决方案1】:

我同意您需要从基础开始,但看起来 cmets 已指示您使用框架来获取 CGRects。因此,使用上面的代码,您似乎有两个问题:

  1. 您没有将 CGRects 传递给 CGRectIntersectsRect
  2. 一旦您更正了 1,您就会遇到问题,您似乎正在检查“字符”的矩形,这是您在方法云中创建的一个节点,但从不将该节点添加到场景中。因此它的矩形大小为 0,0。 (除非您的自定义 BBCCharacter 类覆盖 spriteNodeWithImageNamed 并自动将其添加到场景中)。

所以,要么在场景中添加角色:

[self addChild: character];

或者让角色成为指向场景中已经存在的其他东西的指针:

character = [self childNodeWithName:@"character"];
character = self.playerSprite; //etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多