设置全局变量,即
BOOL _touching;
当您触摸/释放(触摸结束和开始)时,您将该 var 设置为 YES / NO;
在 didbegincontact 中,使用类似
if(_touching == YES) {
// what I want to happen when I am touching
}
else {
// i must not be touching so do this
}
这是基本设置-但是我认为这是问题所在的游戏逻辑,也许可以想一种不同的方法来解决您的问题
@interface XBLMyScene()
@property (strong, nonatomic) SKNode *world;
@property (strong, nonatomic) SKSpriteNode *ball;
@property BOOL touching;
@end
@implementation XBLMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.world = [SKNode node];
[self addChild:self.world];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(500, 0)];
self.ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(40, 40)];
self.ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 40)];
self.ball.position = CGPointMake(200, 300);
[self.world addChild:self.ball];
self.touching = NO;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = YES;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = NO;
}
- (void) didSimulatePhysics
{
if (self.touching) {
NSLog(@"I am touching");
}
else {
NSLog(@"I am not touching");
}
}