【发布时间】: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