【问题标题】:SpriteKit, iOS 9. Method touchesBegan:withEvent: is invoked in SKSceneSpriteKit,iOS 9。方法 touchesBegan:withEvent: 在 SKScene 中调用
【发布时间】:2015-10-16 07:32:32
【问题描述】:

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];
}

怎么了?

【问题讨论】:

  • userInteractionEnabled = YES ?
  • @Astoria 我保留默认值。但即使我规定 userInteractionEnabled = YES,球仍然没有反应......
  • 当您在scene 内部触摸时不会触发touchesBegan
  • 您尝试更改哪个对象的userInteractionsEnabled-property?球或场景视图?
  • @Astoria 抱歉,实际上它是 userInteractionEnabled = NO 的超级视图之一。奇怪的是,在 iOS 8 上它不会引起问题。非常感谢你的帮助,你拯救了我的一天)

标签: sprite-kit ios9 xcode7 touchesbegan


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2016-01-13
    • 2015-06-27
    相关资源
    最近更新 更多