Sprite kit xcode6模拟器中的精灵套件测试
我的问题是,当我想用“SKPhysicsBody”来检测我正在触摸的spritenode时,它在xcode6模拟器中返回“nil”,代码如下 初始化 触摸检测Sprite kit xcode6模拟器中的精灵套件测试,sprite-kit,xcode6,simulator,skphysicsbody,Sprite Kit,Xcode6,Simulator,Skphysicsbody,我的问题是,当我想用“SKPhysicsBody”来检测我正在触摸的spritenode时,它在xcode6模拟器中返回“nil”,代码如下 初始化 触摸检测 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called when a touch begins */ for (UITouch *touch in touches) { CGPoint touchLocation = [touch lo
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
NSLog(@"%f",touchLocation.x);
SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];
NSLog(@"%@",body.node.name);
if ([body.node.name isEqualToString:@"start"]){
NSLog(@"go");
[audioPlayer stop];
[[NSNotificationCenter defaultCenter] postNotificationName:@"nameHide"object:@"Hide"];
SKScene *spaceshipScene=[[SpaceshipScene alloc]initWithSize:self.size];
SKTransition *doors=[SKTransition doorsOpenVerticalWithDuration:0.5];
[self.view presentScene:spaceshipScene transition:doors];
}
}
}
NSLog(@“%@”,body.node.name)代码>返回nil
。
有什么问题,请提前感谢。尝试在节点之间循环。在最后一个Xcode 6 GM中,这与Xcode 5不同。
我也有同样的问题,我就是这样解决的。
在您的情况下,将代码更改为:
NSArray *nodes = [self nodesAtPoint:touchLocation];
for (SKNode *n in nodes){
if ([n.name isEqualToString:@"start"]){
NSLog(@"go");
[audioPlayer stop];
[[NSNotificationCenter defaultCenter] postNotificationName:@"nameHide"object:@"Hide"];
SKScene *spaceshipScene=[[SpaceshipScene alloc]initWithSize:self.size];
SKTransition *doors=[SKTransition doorsOpenVerticalWithDuration:0.5];
[self.view presentScene:spaceshipScene transition:doors];
}
}
我已经在我的设备(ios7.1)上进行了测试,这两种方法都可以工作,也许在ios8中只能循环通过节点?是的,我认为在ios8中必须循环以确保选择正确的节点。
NSArray *nodes = [self nodesAtPoint:touchLocation];
for (SKNode *n in nodes){
if ([n.name isEqualToString:@"start"]){
NSLog(@"go");
[audioPlayer stop];
[[NSNotificationCenter defaultCenter] postNotificationName:@"nameHide"object:@"Hide"];
SKScene *spaceshipScene=[[SpaceshipScene alloc]initWithSize:self.size];
SKTransition *doors=[SKTransition doorsOpenVerticalWithDuration:0.5];
[self.view presentScene:spaceshipScene transition:doors];
}
}