横屏模式在SpriteKit游戏中不能正常工作。

4
我在SpriteKit中开始了一个平台游戏,但横向模式遇到了一点问题。当我的英雄跳跃时,我使用了

(这里缺少内容)。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

当英雄在空中时撞到了墙或其他物体并减缓了速度,那么我就会使用......(接下来的内容不明确)。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];

CGPoint posicionHero = [self.world childNodeWithName:@"hero"].position;
SKSpriteNode *touchHero = (SKSpriteNode *)[self nodeAtPoint:posicionhero];

//pragma mark -hero movement in the air

if ((touchedNode != touchHero)
    &&
    (positionInScene.x > [self.world childNodeWithName:@"hero"].position.x))
{
    [[self.world childNodeWithName:@"hero"].physicsBody applyImpulse:CGVectorMake(5, 0)];
}
if ((touchedNode != touchHero)
    &&
    (positionInScene.x < [self.world childNodeWithName:@"hero"].position.x))
{
    [[self.world childNodeWithName:@"hero"].physicsBody applyImpulse:CGVectorMake(-5, 0)];
}

}

保持那个速度。我一直在测试纵向模式,一切都很正常,但我的游戏是横向模式,所以最后我将游戏锁定为横向模式,现在我正在横向模式下测试,但出现了问题,因为当我向前移动手指跳跃时,什么也不会发生。相反,我必须向上移动手指才能获得那个动作。我确定我错过了一些基本的横向模式配置。任何帮助将不胜感激。

你尝试过通过Y坐标来改变X坐标吗?同时请记住,locationInNode使用的坐标系统的原点在左下角,而不是像UIView中一样在左上角。 - lucaslt89
1个回答

9

这实际上是SpriteKit游戏中常见的问题。这是因为viewDidLoad在应用检测设备方向之前运行。你可以通过将viewDidLoad替换为viewWillLayoutSubviews来解决这个问题,后者会在设备方向检测之后运行。例如,你可以用下面的代码替换默认的SpriteKit viewDidLoad

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

        // Create and configure the scene.
        SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;

        // Present the scene.
        [skView presentScene:scene];
    }
}

要获取更多信息,请查看以下来源: UIViewController返回无效的框架? SpriteKit初学者教程


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接