【问题标题】:Why is scaling/zooming-in an SKNode forcing the view into the left of the screen?为什么缩放/放大 SKNode 会强制视图进入屏幕左侧?
【发布时间】:2015-10-30 05:50:57
【问题描述】:

我正在关注 Ray Wenderlich 的“iOS Games by Tutorials”,我的世界设置和工作都得到了:整个游戏处于横向模式,有一个名为 _worldNode 的 SKNode 和所有内容,除了 _uiNode(游戏内 UI) , 被添加到它。玩家角色走到一个触摸的位置,_worldNode 像跑步机一样在他身下移动。然而,像所有功能(或者他们称之为“果汁”)的瘾君子一样,我想通过 UIPinchGestureRecognizer 通过缩放 _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.
    }
}

那么任何人都可以通过查看上面的代码告诉我,为什么相机/视图在放大时会移动到左下角?我已经在这个问题上坐了几天,但我仍然无法弄清楚。

【问题讨论】:

  • 所以我尝试通过将其添加到 didFinishUpdate 方法中来减轻左下角的影响:CGPoint thePoint = [self pointToCenterViewOn:_player.position]; thePoint.x += -self.size.width/2; thePoint.y += -self.size.height/2;所以现在当我用这个放大时:[_worldNode setXScale:1.5]; [_worldNode setYScale:1.5];它可以工作,但玩家角色并没有严格地停留在屏幕中间。他很“松散”,一段时间后最终可以跑到镜头外……有什么想法可以将角色位置稳定到屏幕中间吗?

标签: ios objective-c sprite-kit sknode


【解决方案1】:

感谢 JKallio,他在对 Zoom and Scroll SKNode in SpriteKit 的回答中编写了详细的代码,我已经能够找到解决问题的一段代码。有一种名为“centerOnNode”的方法,它小巧、优雅,完美地解决了我的问题。这里适合任何需要的人:

-(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”或“didFinishUpdate”中调用该方法,如下所示:

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

附:请记住,问题不是如何放大。而是如何在世界已经放大后解决相机的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2021-03-31
    相关资源
    最近更新 更多