SKPhysics节点以及如何将BOOL发送到正确的对象

时间:2016-09-13 10:52:25

标签: sprite-kit skphysicsbody

//很可能是一个小小的错误...... 如果拖动磁贴击中TargetNodeCatagoryT1,则冲突有效 但是如何在命中对象上设置BOOL?

if (firstBody.categoryBitMask == DragTileCategory &&
    secondBody.categoryBitMask == TargetNodeCatagoryT1 ){

   TargetNode *hitNode = (TargetNode *) secondBody.node;
    hitNode.hasHitT1 = YES; <<this crashes app
    but I wish to send BOOl back...
}

错误日志: 由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; - [SKSpriteNode setHasHitT1:]:无法识别的选择器发送到实例

Thank's Knight0fDragon You are right, the "hitNode" are SKSpriteNodes 
nested within the TargetNode class - each TargetClass has 3 targets,


 BOOLS are declared in .h and synthesised in .m   
    @interface TargetNode : SKNode 
    BOOL hasHitTarget1;
    BOOL hasHitTarget2;
    BOOL hasHitTTarget3;

 //later
    @property (nonatomic,assign) BOOL hasHitTarget1;
    @property (nonatomic,assign) BOOL hasHitTarget2;
    @property (nonatomic,assign) BOOL hasHitTarget3;

 @implementation TargetNode
 @synthesize hasHitTarget1,hasHitTarget2,hasHitTarget3;

 -(id)initWithImageNamed:(NSString*)imageName labelText:(NSString*)labelText
 other stuff here....then

   hasHitTarget1 = NO;
   hasHitTarget2 = NO;
   hasHitTarget3 = NO;

enter image description here

按照以下方式工作: 将physicsBody附加到我想稍后更改的标签上 (如果我能找到第二个身体,那么physicsBody有点像一个标记。节点我可以改变文本......可能这不是最好的方法 - 但它适用于我的目的:)

//附上physicsBody 例如T1label.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize ...

//找到对象secondBody

在didMakeContact中         secondBody.categoryBitMask == TargetNodeCatagoryT1){

    //Convert second body into useable object 
    SKLabelNode *hitNode = (SKLabelNode *) secondBody.node;

    //change text
    hitNode.text =@"test";

    NSLog(@"TargetNode Name@%@",hitNode.name);

    //Test type of object
    NSLog(@"hitNode isMemberOfClass  TargetNode: %@", [hitNode isMemberOfClass:[TargetNode class]] ? @"YES":@"NO");
    NSLog(@"hitNode isMemberOfClass  SKSpriteNode: %@", [hitNode isKindOfClass:[SKLabelNode class]] ? @"YES":@"NO");  
}

1 个答案:

答案 0 :(得分:1)

这是失败的,因为您hitNode不是TargetNode,即使您声称它是[hitNode isKindOfClass:]。使用[hitNode isMemberOfClass:]hitNode来验证setHasHitT1是什么。

唯一的另一个问题是你没有正确地创建你的属性,或者它没有为你合成,并且hitNode.hasHitT1 = YES;从未被创建,因此{{1}}失败