如何让我的精灵套件场景出现在 UIView 上方? [英] How to make my sprite kit scene appear above a UIView?
问题描述
我进行了一些搜索,但没有找到任何直接解决我的问题的内容:我有一个 SKScene
和几个 SKNodes
,每个都带有 SKSpriteNode
我的游戏对象,我正在使用背景 UIImageView
(我需要对背景进行一些处理,而精灵套件无法合理完成 - 至少据我所知);我遇到的问题是 UIImageView
出现在一切之上,我看不到我的游戏对象.
I've done some searching but haven't found anything directly addressing my issue: I have an SKScene
with several SKNodes
each with SKSpriteNode
objects for my game, and I am using a background UIImageView
(there are some things I need to do with background that cannot be reasonable done with sprite kit - to my knowledge at least); the problem I'm having is that the UIImageView
appears above everything and I can't see my game objects.
在我的视图控制器(.m 文件)中,我有以下代码来调用我的场景
In my view controller (.m file) I have the following code to call my scene
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
在上面的 GameScene
中,我有几个 SKNodes
,其中我的所有游戏对象都是其子对象(例如 nodeHDU
、nodePlay
、nodePauseMenu
...),我使用 UIImageView
作为我的背景(我随着时间的推移在背景场景之间切换,并使用一些不错的过渡,例如因为 UIViewAnimationOptionTransitionCrossDissolve;
无法使用 SKSpriteNode
作为背景而不使用多个 SKSpriteNodes
和复杂的 SKActions
数组来完成此操作> 所以我在使用 UIImageView
)
In the GameScene
above, I have several SKNodes
where all my game objects are children of (for example nodeHDU
, nodePlay
, nodePauseMenu
...), and I am using a UIImageView
as my background (I am switching between background scenes over time and am using some nice transitions such as UIViewAnimationOptionTransitionCrossDissolve;
couldn't accomplish this with a SKSpriteNode
as background without using multiple SKSpriteNodes
and an intricate array of SKActions
so I"m using UIImageView
)
UIView *backgrounds = [[UIView alloc] init];
[self.view insertSubview:backgrounds belowSubview:self.view];
UIImageView *imageBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
[backgrounds addSubview:imageBackground];
[backgrounds sendSubviewToBack:imageBackground];
[imageBackground setImage:[UIImage imageNamed:@"1-A.png"]];
imageBackground.alpha=1.0;
但是,上面的 ImageBackground
是我在 Xcode 中运行应用程序时唯一看到的内容.我的游戏中的其他对象(其他节点的子节点)存在,并且正在调用各种 NSLog
语句,但它们似乎出现在我拥有的 ImageBackground
后面.belowSubview"和sendSubviewToBack"上面的代码没有帮助.
However, the ImageBackground
above is the only thing I see when running the app in Xcode. The other objects from my game (children of other nodes) exist, and various NSLog
statements are getting called, but they seem to appear behind the ImageBackground
I have. The code above "belowSubview" and "sendSubviewToBack" don't help.
那么如何将 imageBackground
发送到实际作为背景?
So How can I send the imageBackground
to actually be the background?
推荐答案
您的问题是您将 SKView
直接设置为控制器视图.为了克服这个问题,不要让 self.view
成为一种 SKView
.在故事板或 Xib 中执行以下步骤:
Your problem is here that you are directly setting SKView
as a controller view. To overcome from this issue don't make self.view
a type of SKView
. Do the following step in your storyboard or Xib :
- 首先在控制器的视图上添加一个
UIImageView
.还要设置图像. - 然后在
UIImageView
上添加一个SKView
并将UIClearColor
设置为背景色.
- Add an
UIImageView
on the controller's view first. Set the image as well. - Then add a
SKView
over theUIImageView
and setUIClearColor
to the background colour.
所以完整的层次结构就像 UIViewController
->UIView
->UIImageView
->SKView
So the complete hierarchy is like UIViewController
->UIView
->UIImageView
->SKView
这篇关于如何让我的精灵套件场景出现在 UIView 上方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!