SpriteKit, iOS 9.方法touchesBegan:withEvent:在SKScene中被调用



SKShapeNode是在气泡中创建的,可以通过点击来移动。该项目在iOS 8上启动时能够正常运行,触控操作正在正确进行。当在iOS 9上启动时,气泡正在创建并且物理工作正确(创建时,气泡相互反弹)。但是它们不会在点击时做出反应,touchesBegan:withEvent:不会被触发。编译器不会生成错误。如果有人遇到过这样的问题,并且已经解决了,或者知道解决方法,请告诉我。

这就是我创建气泡的方法:

// при создании изначальное положение по горизонтали выбирается рандомно
SKShapeNode *ballPhysics = [[SKShapeNode alloc] init];
CGPathRef ballPhysicsPath = CGPathCreateWithEllipseInRect(CGRectMake( - ballRadius, - ballRadius, ballRadius * 2.f, ballRadius * 2.f), 0);
ballPhysics.path = ballPhysicsPath;
ballPhysics.position = CGPointMake(self.frame.size.width/2.f - 20.f + (arc4random() % 40), 6.f*self.frame.size.height/5.f);
ballPhysics.zPosition = 0;
// ширина линии, цвет и имя шарика
ballPhysics.lineWidth = 3;
ballPhysics.strokeColor = task.color;
ballPhysics.fillColor = [SKColor whiteColor];
ballPhysics.name = task.ballIdentifier;
// физическое тело
CGPathRef ballPhysicsBodyPath = CGPathCreateWithEllipseInRect(CGRectMake( - ballRadius - 2, - ballRadius - 2, ballRadius * 2.f + 4, ballRadius * 2.f + 4), 0);
ballPhysics.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath: ballPhysicsBodyPath];
ballPhysics.physicsBody.dynamic = YES;
ballPhysics.physicsBody.restitution = 0.0f;
ballPhysics.physicsBody.mass = 0.1f;
ballPhysics.physicsBody.friction = 0.0f;
ballPhysics.physicsBody.categoryBitMask = ballCategory;
ballPhysics.physicsBody.collisionBitMask = ballCategory | funnelCategory | edgeCategory;
ballPhysics.physicsBody.contactTestBitMask = ballCategory | funnelCategory | edgeCategory;
ballPhysics.physicsBody.allowsRotation = NO;
ballPhysics.physicsBody.usesPreciseCollisionDetection = YES;
// добавляем объект на сцену
[self addChild:ballPhysics];

方法touchesBegan: withEvent:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    SKNode *touchedNode = [self nodeAtPoint:touchLocation];
    if ([touchedNode.name hasPrefix:@"ball_"])
    {
        self.touch = touch;
        self.selectedNode = (SKShapeNode *) touchedNode;
        self.selectedNode.physicsBody.mass = 0.3f;
        return;
    }
    // говорим делегату, что было касание между шарами
    if ([self.delegateMovingBalls respondsToSelector:@selector(touchesBetweenBalls)])
        [self.delegateMovingBalls touchesBetweenBalls];
}

怎么了?

可能导致此问题的两个主要因素:

  1. userInteractionEnabled设置为false
  2. 你在顶部有一个视图阻止底层内容接收触摸。

最新更新