ios – 横向模式在spritekit游戏中无法正常工作
发布时间:2020-12-14 18:03:21 所属栏目:百科 来源:网络整理
导读:我在spriteKit中开始了一个平台游戏,我对横向模式有一点问题. 当我和我的英雄一起跳跃时,我会使用 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 而当英雄它在空中并与墙壁或任何物体相撞并错过他的速度时,我就使用了 -(void)touchesMov
|
我在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)];
}
}
保持这种速度. 解决方法
这实际上是SpriteKit游戏中的常见问题.发生这种情况是因为viewDidLoad实际上在应用程序检查设备方向之前运行.您可以通过使用viewWillLayoutSubviews替换viewDidLoad来修复它,该视图将在检测到设备方向后运行.例如,您可以使用以下命令替换默认的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];
}
}
有关更多信息,请查看以下来源: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
