旋转SKLabelNode后,很难获得touchesBegan中的位置:

时间:2014-04-12 17:03:19

标签: ios objective-c sprite-kit

我在我的应用程序中将SKLabel节点设置为按钮,它工作正常。但是,当我使用此代码旋转SKLabelNode时,我的touchesBegan方法不再正确执行,并且让SKLabelNode正确触摸寄存器变得非常困难:

[_menuButton runAction:[SKAction sequence:@[[SKAction rotateByAngle:M_PI  duration:.2],
                                                [SKAction moveByX:0 y:-15 duration:.2]]]];

这是我的触摸开始方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];

if ([node.name isEqualToString:menuButtonName]) {
    //Touched Menu Button
        [self animateMenuAndShow:YES];

}
}
编辑:另外一条信息是我使用SKTEffects,而不是使用self.scene作为我的基础我使用_worldLayer来添加场景中的所有元素。这有可能导致我的触摸计算丢失吗?如果是这样,我怎么能纠正这个?

self.scaleMode = SKSceneScaleModeResizeFill;    
self.anchorPoint = CGPointMake(0.5, 0.5);

// The origin of the pivot node must be the center of the screen.
_worldPivot = [SKNode node];
[self addChild:_worldPivot];

// Create the world layer. This is the only node that is added directly
// to the pivot node. If you have a HUD layer you would add that directly
// to the scene and make it sit above the world layer.
_worldLayer = [SKNode node];
_worldLayer.position = self.frame.origin;
[_worldPivot addChild:_worldLayer];

此外,在遵循sangony的建议后,我的更新touchesBegan:方法最初运作良好,但在通过M_PI旋转标签后遇到同样的问题

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:_worldLayer];
    CGPoint locationInMenu = [touch locationInNode:_menuBackground];

     if (CGRectContainsPoint(_menuButton.frame, location)) {
     //Touched Menu Button

        if (!self.menuVisible) {
           [self animateMenuAndShow:YES];
       }
       else
       {
         [self animateMenuAndShow:NO];
       }
    }
}

EDIT2:

这是我的animateMenuAndShow:方法

-(void)animateMenuAndShow:(BOOL)show
{
    if (show == YES) {
       // self.paused = YES;
       self.menuVisible = !self.menuVisible;

        [self createMenu];


        SKAction *showMenuAction = [SKAction sequence:@[
                                                    [SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height/2) duration:0.3],
                                                    [SKAction runBlock:^{
                                                    [self jelly:self.menuBackground];
                                                        }],
                                                    [SKAction waitForDuration:1.5]
                                                    ]];
        [self.menuBackground runAction: showMenuAction completion:^{
            self.paused = YES;
        }];


        if (_flipped == YES) {
            [self.menuBackground runAction:[SKAction sequence:@[
                                                            [SKAction moveByX:0 y:0 duration:.2],[SKAction rotateByAngle:M_PI  duration:.2]]]];//   [SKAction rotateByAngle:M_PI  duration:.2]
        }

    }
    else
    {
       self.paused = NO;

        SKAction *removeMenuAction = [SKAction sequence:@[
                                                      [SKAction         moveTo:CGPointMake(self.size.width / 2, -self.size.height + 300) duration:0.3],
                                                      ]];
        [self.menuBackground runAction:removeMenuAction completion:^{
            [self.menuBackground removeFromParent];
        }];

        self.menuVisible = !self.menuVisible;


    }
}

1 个答案:

答案 0 :(得分:0)

请改为使用touchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];

    if (CGRectContainsPoint(myLabel.frame, touchLocation))
        {
            [self animateMenuAndShow:YES];
        }
}