【问题标题】:EXC_BAD_ACCESS SKPhysicsWorldEXC_BAD_ACCESS SKPhysicsWorld
【发布时间】:2014-01-19 09:50:51
【问题描述】:

我真的很困惑为什么我会在 [world addJoint:pinJoin]; 获得 EXC_BAD_ACCESS(代码=1,地址=0x1c)。

联合测试.m

#import "JointTest.h"

@implementation JointTest
-(SKNode *)initWithWorld:(SKPhysicsWorld *)pWorld{
    if(self = [super init]){
       world = pWorld;
       [self attachBodies];
    }
    return self;
}
-(void)attachBodies{
    SKSpriteNode * spriteA = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
    SKSpriteNode * spriteB = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];

    SKPhysicsBody * bodyA = [SKPhysicsBody bodyWithRectangleOfSize:spriteA.size];
    SKPhysicsBody * bodyB = [SKPhysicsBody bodyWithRectangleOfSize:spriteB.size];

    spriteA.position = CGPointMake(150, 300);
    spriteB.position = CGPointMake(150, 300 + spriteB.size.height/2);

    [self addChild:spriteA];
    [self addChild:spriteB];

    bodyA.dynamic = NO;

    spriteA.physicsBody = bodyA;
    spriteB.physicsBody = bodyB;

    SKPhysicsJointPin * pinJoin = [SKPhysicsJointPin jointWithBodyA:spriteA.physicsBody bodyB:spriteB.physicsBody anchor:spriteA.position];
    [world addJoint:pinJoin];

}
@end

联合测试.h

#import <SpriteKit/SpriteKit.h>

@interface JointTest : SKNode{
    SKPhysicsWorld * world;
}
-(SKNode *)initWithWorld:(SKPhysicsWorld *)pWorld;
@end

在SKScene中

JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld];
[self addChild:test];

让我感到困惑的是,将 attachBodies 方法中的代码移动到场景中,并使用场景的物理世界调用 addJoint 方法效果很好。例如:

SKSpriteNode * spriteA = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
SKSpriteNode * spriteB = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];

SKPhysicsBody * bodyA = [SKPhysicsBody bodyWithRectangleOfSize:spriteA.size];
SKPhysicsBody * bodyB = [SKPhysicsBody bodyWithRectangleOfSize:spriteB.size];

spriteA.position = CGPointMake(150, 300);
spriteB.position = CGPointMake(150, 300 + spriteB.size.height/2);

[self addChild:spriteA];
[self addChild:spriteB];

bodyA.dynamic = NO;

spriteA.physicsBody = bodyA;
spriteB.physicsBody = bodyB;

SKPhysicsJointPin * pinJoin = [SKPhysicsJointPin jointWithBodyA:spriteA.physicsBody bodyB:spriteB.physicsBody anchor:spriteA.position];
[self.physicsWorld addJoint:pinJoin];

为此,我已经把头发拉了几个小时,所以如果有人有想法,我将不胜感激。谢谢!

【问题讨论】:

    标签: ios ios7 xcode5 sprite-kit


    【解决方案1】:

    在 init 上传递物理世界并不是好的设计。而是正常创建类的实例,然后从您创建 JointTest 实例的任何位置向其发送attachBodies 消息。然后,您可以通过self.scene.physicsWorld 访问物理世界,而不必绕过世界并保持对它的无关引用。

    而不是这个:

    JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld];
    [self addChild:test];
    

    这样做:

    JointTest * test = [JointTest node];
    [self addChild:test];
    [test attachBodies];
    

    请注意,attachBodiesaddChild 之后发送,因为在addChild 之前,JointTest 的scene 属性仍将为nil。

    我打赌这也会解决你的问题,虽然这是一个猜测

    您创建 JointTest 并立即向其中添加两个带有关节的精灵,但此时 JointTest 实例尚未添加到节点图中(通过 addChild)。因此,JointTest 及其两个子节点一开始还没有在物理世界中“注册”,这可能会导致崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2012-07-24
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多