基本上我有四个场景,在viewcontroller上,我想检测我在哪个场景,因为我在viewcontroller中有背景音乐,我想在某些场景中暂停音乐。我找到了如何在viewController中检测场景,但它很快,我只知道客观c的基础知识 的被修改
SKView *skView =(SKView *)self.view;
TitleScene *sceneTitle = [TitleScene sceneWithSize:self.view.bounds.size];
BonusScene *sceneBonus = [BonusScene sceneWithSize:self.view.bounds.size];
GameScene *sceneGame = [GameScene sceneWithSize:self.view.bounds.size];
DifficultScene *sceneDifficult = [DifficultScene sceneWithSize:self.view.bounds.size];
if(skView){
if(sceneTitle){
NSLog(@"Iam in sceneTitle");
}
if(sceneGame){
NSLog(@"Iam in sceneGame");
}
if(sceneDifficult){
NSLog(@"Iam in sceneDifficult");
}
if(sceneBonus){
NSLog(@"Iam in sceneBonus");
}
else{
NSLog(@"else");
}
}
现在,当启动app时,它会遍历每个if语句一次,即使只在TitleScene中使用Iam。 出了什么问题?
答案 0 :(得分:1)
为每个场景命名,然后在viewcontroller类中,执行
(SWIFT):
if let skView = view as? SKView, let scene = skView.scene
{
switch(scene.name)
{
case "name1":
default:()
}
}
else
{
//Something is wrong, we do not have an SKView or a SKScene
}
目标C:
SKView *view = (SKView*)self.view ;
if(view)
{
SKScene *scene = view.scene;
if(scene)
{
NSString *name = scene.name;
//compare names here
}
}