【发布时间】: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