我真的很困惑为什么我要在EXC_BAD_ACCESS (code=1,address=0x1c)上获得一个世界性的附加文件:pinJoin;
JointTest.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];
}
@endJointTest.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方法中的代码移到场景中,并使用场景的physicsWorld调用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];我已经为此拔了几个小时的头发,所以如果有人有主意的话,我会非常感激的。谢谢!
发布于 2014-01-02 00:22:49
在物理世界上通过它不是好的设计。相反,通常会创建类的实例,然后将attachBodies消息从创建JointTest实例的任何地方发送给它。然后,您可以通过self.scene.physicsWorld访问物理世界,而不是通过传递世界并保持对它的无关引用。
而不是这样:
JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld];
[self addChild:test];这样做:
JointTest * test = [JointTest node];
[self addChild:test];
[test attachBodies];注意,attachBodies是在addChild之后发送的,因为在addChild之前,JointTest的scene属性仍然是零。
我打赌这也能解决您的问题,尽管--这是一个猜测的:
您可以创建JointTest并立即向其添加两个带有连接的精灵,但此时还没有将JointTest实例添加到节点图中(通过addChild)。因此,JointTest及其两个子节点还没有在物理世界中“注册”,这可能导致崩溃。
https://stackoverflow.com/questions/20874636
复制相似问题