我正在开发一款带有SpriteKit的太空射击游戏用于测试目的。 游戏领域是700 x 700像素/点大。显然是不适合iPhone屏幕。这意味着我需要一些影响玩家+敌人+子弹+小行星的滚动。我搜索了谷歌,但滚动的大多数主题是指Cocos2D,几乎总是指Cocos2D的特定功能,不是由精灵工具包提供的。所以事情是这是我的第一个游戏,所以我不确定实现多向滚动的正确方法是什么。我希望你们能给我一个解决方案和/或提示/教程或其他任何有助于我的东西:D
答案 0 :(得分:2)
创建你的"宇宙飞船",创建一个摄像机节点,在更新方法中,让你的摄像机始终以宇宙飞船为中心。
与此问题类似:How to make camera follow SKNode in Sprite Kit?
-(void)centerOnNode:(SKNode*)node {
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
cameraPositionInScene.x = 0;
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
然后你必须弄清楚你自己的游戏机制如何移动,但如果你只是暗示船跟随你的手指,你可以使用SKActions动画手指位置。示例:https://stackoverflow.com/a/19172574/525576
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint diff = CGPointMake(location.x - _myPlayer.position.x, location.y - _myPlayer.position.y);
CGFloat angleRadians = atan2f(diff.y, diff.x);
[_myPlayer runAction:[SKAction sequence:@[
[SKAction rotateToAngle:angleRadians duration:1.0],
[SKAction moveByX:diff.x y:diff.y duration:3.0]
]]];
}
}
}
答案 1 :(得分:0)
解决了它,感谢ray wenderlich
CGSize winSize = self.size;
int x = MAX(player.position.x, winSize.width / 2 );
int y = MAX(player.position.y, winSize.height / 2 );
x = MIN(x, worldSize.width - winSize.width / 2 );
y = MIN(y, worldSize.height - winSize.height/ 2 );
CGPoint actualPosition = CGPointMake(x, y);
CGPoint centerOfView = CGPointMake(winSize.width/2, winSize.height/2);
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);
world.position = viewPoint;