为什么缩放/放大SKNode迫使视图进入屏幕左侧?

时间:2015-08-06 22:09:20

标签: ios objective-c sprite-kit sknode

我正在关注Ray Wenderlich的'iOS游戏教程'和&我在我的世界设置中得到了一切。工作:整个游戏都处于风景模式和有一个名为_worldNode&的SKNode除了_uiNode(游戏内UI)之外的所有内容都会添加到其中。玩家角色走到一个感人的位置& _worldNode像跑步机一样在他身下移动。然而,像所有功能(或他们称之为“果汁”)上瘾者我想通过缩放_worldNode来增加放大/缩小功能,我做了。但现在每次放大时,“相机”都会向左下方移动。缩小将视图移动到屏幕的右上角。一团糟。我需要视图以玩家角色为中心。我已经尝试了所有我想到的东西&在网上找到。我最接近的是使用来自SKNode scale from the touched point的技术,但我仍然得到血腥的左下角/右上角混乱。我意识到只有当我更新相机/视图(它真的是_worldNode.position)时才会发生这种混乱。因此,'didSimulatePhysics'或'didFinishUpdate'方法无济于事。事实上,即使是一个稍微移动/更新相机视图(_worldNode.position)的按钮仍然会给我左下角/右上角的问题。这是我的代码。我希望有人可以看看&告诉我要修改什么来使事情有效。

@interface GameScene () <SKPhysicsContactDelegate, UIGestureRecognizerDelegate>
{
    UIPinchGestureRecognizer        *pinchGestureRecognizer;
}

//我的GameScene的属性。

@property SKNode        *worldNode;   
@property etc. etc.


//Called by -(id)initWithSize:(CGSize)size method & creates the in-game world.
-(void)createWorld
{
    [_worldNode addChild:_backgroundLayer];

    [self addChild:_worldNode];
    self.anchorPoint        = CGPointMake(0.5, 0.5); //RW tutorial did it this way.
    _worldNode.position     = CGPointMake(-_backgroundLayer.layerSize.width/2, -_backgroundLayer.layerSize.height/2); //Center.

    //Then I add every node to _worldNode from this point on.
}

//Neccessary for gesture recognizers.
-(void)didMoveToView:(SKView *)view
{
    pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleZoomFrom:)];
    [view addGestureRecognizer:pinchGestureRecognizer];
}

//"Camera" follows player character. 'didSimulatePhysics' doesn't help either.
-(void)didFinishUpdate
{
    //IF '_pinchingScreen' == YES then screen pinching is in progress. Thus, camera position update will seize for the duration of pinching.
    if (!_pinchingScreen)
    {
        _worldNode.position = [self pointToCenterViewOn:_player.position];
    }
}

//Method that is called by my UIPinchGestureRecognizer. Answer from: https://stackoverflow.com/questions/21900614/sknode-scale-from-the-touched-point?lq=1
-(void)handleZoomFrom:(UIPinchGestureRecognizer*)recognizer
{
    CGPoint anchorPoint = [recognizer locationInView:recognizer.view];
    anchorPoint         = [self convertPointFromView:anchorPoint];

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // No code needed for zooming...

        _player.movementMode    = 2; //Stop character from moving from touches.
        _pinchingScreen         = YES; //Notifies 'didFinishUpdate' method that pinching began & camera position update should stop for now.
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged)
    {
        //Technique from the above Stack Overflow link - Commented out.
//        CGPoint anchorPointInMySkNode = [_worldNode convertPoint:anchorPoint fromNode:self];
//        
//        [_worldNode setScale:(_worldNode.xScale * recognizer.scale)];
//        
//        CGPoint mySkNodeAnchorPointInScene = [self convertPoint:anchorPointInMySkNode fromNode:_worldNode];
//        CGPoint translationOfAnchorInScene = CGPointSubtract(anchorPoint, mySkNodeAnchorPointInScene);
//
//        _worldNode.position = CGPointAdd(_worldNode.position, translationOfAnchorInScene);
//        
//        recognizer.scale = 1.0;


        //Modified scale: 2.0
        if(recognizer.scale > _previousWorldScale)
        {
            _previousWorldScale = recognizer.scale;

            CGPoint anchorPointInMySkNode = [_worldNode convertPoint:anchorPoint fromNode:self];
            [_worldNode setScale:2.0];
            CGPoint worldNodeAnchorPointInScene = [self convertPoint:anchorPointInMySkNode fromNode:_worldNode];
            CGPoint translationOfAnchorInScene  = CGPointSubtract(anchorPoint, worldNodeAnchorPointInScene);
            _worldNode.position = CGPointAdd(_worldNode.position, translationOfAnchorInScene);


            //[_worldNode runAction:[SKAction scaleTo:2.0 duration:0]]; //This works too.
        }
        //Original scale: 1.0
        if(recognizer.scale < _previousWorldScale)
        {
            _previousWorldScale = recognizer.scale;

            CGPoint anchorPointInMySkNode = [_worldNode convertPoint:anchorPoint fromNode:self];
            [_worldNode setScale:1.0];
            CGPoint worldNodeAnchorPointInScene = [self convertPoint:anchorPointInMySkNode fromNode:_worldNode];
            CGPoint translationOfAnchorInScene  = CGPointSubtract(anchorPoint, worldNodeAnchorPointInScene);
            _worldNode.position = CGPointAdd(_worldNode.position, translationOfAnchorInScene);


            //[_worldNode runAction:[SKAction scaleTo:1.0 duration:0]]; //This works too.
        }
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        // No code needed here for zooming...

        _pinchingScreen         = NO; //Notifies 'didFinishUpdate' method that pinching has stopped & camera position update should resume.
        _player.movementMode    = 0; //Resume character movement.
    }
}

所以有人可以通过查看上面的代码告诉我,为什么在放大时相机/视图会移到左下角?我已经好几天了解这个问题&amp;我还是想不出来。

1 个答案:

答案 0 :(得分:0)

感谢JKallio,他在Zoom and Scroll SKNode in SpriteKit的答案中写了详细的代码,我已经找到了解决问题的一段代码。有一种名为“centerOnNode&#39;那是小巧,优雅的完美地解决了我的问题。这是适合任何需要的人:

-(void) centerOnNode:(SKNode*)node
{
    CGPoint posInScene = [node.scene convertPoint:node.position fromNode:node.parent];
    node.parent.position = CGPointMake(node.parent.position.x - posInScene.x, node.parent.position.y - posInScene.y);
}

然后你在你的'didSimulatePhysics&#39;中调用了这个方法。或者在里面做过新的更新&#39;像这样:

//"Camera" follows player character.
-(void)didFinishUpdate
{
    //IF '_pinchingScreen' == YES then screen pinching is in progress. Thus, camera position update will seize for the duration of pinching.
    if (!_pinchingScreen)
    {
        if (_previousWorldScale > 1.0) //If _worldNode scale is greater than 1.0
        {
            [self centerOnNode:_player]; //THIS IS THE METHOD THAT SOLVES THE PROBLEM!
        }
        else if (_previousWorldScale == 1.0) //Standard _worldNode scale: 1.0
        {
            _worldNode.position = [self pointToCenterViewOn:_player.position];
        }
    }
}

P.S。请记住,问题不是如何放大。但是,一旦世界已经放大,如何解决相机问题。