在解释如何有效地实现这个类方法时,Apple的文档是模糊的
+ (SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies; //the error that I'm getting is 2014-04-23 23:25:21.518 Space Monkey[3398:60b] -[SKSpriteNode _shapes]: unrecognized selector sent to instance 0x15d79c502014-04- 23 :25:21.520 SpaceMonkey3398:60B *终止应用程序因“NSInvalidArgumentException”异常而终止,原因:'-SKSpriteNode _shapes:未识别的选择器发送到实例0x15d79c50‘*第一次抛出调用堆栈:(0x2d92ef0x380c3ce7 0x2d92837 0x2d93112f.这是抛出一个例外,我几乎已经研究了到处没有运气。关于同样的问题,这里还有另一个问题,然而,这些建议似乎根本行不通。我提供了代码,以便有人能给我一些启示,希望能给我正确的指导.这是代码
-(NSMutableArray *)platformBars
{
NSMutableArray *array =[[NSMutableArray alloc]init];
//_spriteNode declared in the interface as an instance variable
_spriteNode=[SKSpriteNode node];
for (int i =0; i<5; i++)
{
_spriteNode= [SKSpriteNode spriteNodeWithImageNamed:@"Water Block"];
_spriteNode.physicsBody.mass = 10.0f;
_spriteNode.physicsBody.density = _spriteNode.size.height;
_spriteNode.position = CGPointMake(x , _spriteNode.position.y);
x=x+40;
[array addObject:_spriteNode];
}
return array;
}
-(PlatformNode *)createPlatformsAtPosition:(CGPoint)position
{
NSArray *arrays = [self platformBars];
PlatformNode *node =[PlatformNode node];
node.physicsBody = [SKPhysicsBody bodyWithBodies:arrays];
for (SKSpriteNode *sknode in arrays)
{
[node addChild:sknode];
}
node.position=position;
node.name =@"platform";
node.physicsBody.affectedByGravity = NO;
NSLog(@"bodyofSize %f",[node calculateAccumulatedFrame].size.width);
return node;
}
-(PlatformNode *)createPlatformsAtPosition:(CGPoint)position is called in the
-(void)didMoveToView:(SKView *)view body of the function. 我希望我包括了足够的细节和我的代码片段,所以希望有人会帮助我和其他人来。提前谢谢你。
发布于 2014-04-24 09:09:16
应该是
NSArray *arrays = [[self platformBars] copy];而不是
NSArray *arrays = [self platformBars];因为[self platformBars]是可变数组,而arrays是不可变的。
编辑
而且,数组中的SKSpriteNode,而不是物理体。所以
node.physicsBody = [SKPhysicsBody bodyWithBodies:arrays];应该是
NSMutableArray *mArray=[NSMutableArray new];
for (SKSpriteNode *anySpriteNode in arrays){
[mArray addObject:anySpriteNode.physicsBody];
}
node.physicsBody = [SKPhysicsBody bodyWithBodies:mArray.copy];https://stackoverflow.com/questions/23260219
复制相似问题