Cocos2d V3 iOS - 如何从App Delegate访问runningScene [英] Cocos2d V3 iOS - How to access runningScene from App Delegate
问题描述
我想在App Delegate中访问我的运行场景。问题是, [[CCDirector sharedDirector] runningScene]
返回 CCScene
对象,而不是我的场景的实际类 MyMainScene
。如果我尝试调用任何我的自定义方法,我得到:
I’m wanting to access my running scene in the App Delegate. The problem is, [[CCDirector sharedDirector] runningScene]
returns a CCScene
object, rather than the actual class of my scene MyMainScene
. If I try to call any of my custom methods, I get:
-[CCScene customMethod]: unrecognized selector sent to instance 0x156bedc0
我试过这样投资
CCScene *scene = [[CCDirector sharedDirector] runningScene];
MyMainScene *mainScene = (MyMainScene*)scene;
[mainScene customMethod];
但这没有效果。上面的 mainScene
对象仍然返回一个类名为 CCScene
,并且会在运行时崩溃。
But this has no effect. The mainScene
object above still returns a class name of CCScene
, and will crash at runtime.
我也试过动态投射,如这里所建议的 Objective-C dynamic_cast ?。使用动态转换我不会崩溃,但方法总是返回null。
I’ve also tried dynamic casting, as suggested here Objective-C dynamic_cast?. With dynamic casting I don’t get a crash, but the method always returns null.
AppController实施
AppController implementation
#import "cocos2d.h"
#import "AppDelegate.h"
#import " IDFAMainScene.h"
@implementation AppController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// default code here
}
- (CCScene*) startScene {
return [CCBReader loadAsScene:@"IDFAMainScene"];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
CCScene *scene = [[CCDirector sharedDirector] runningScene];
IDFAMainScene *mainScene = (IDFAMainScene*)scene;
[mainScene customMethod];
}
IDFAMainScene标题
IDFAMainScene header
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface IDFAMainScene : CCNode {
}
-(void)customMethod;
IDFAMainScene实现
IDFAMainScene implementation
#import "IDFAMainScene.h"
@implementation IDFAMainScene
-(void)didLoadFromCCB{
[self customMethod];
}
-(void)customMethod{
NSLog(@"custom method called");
}
上面的应用程序将会编译,加载IDFAMainScene文件可以作为 customMethod
c $ c>从 didLoadFromCCB
中调用的自定义方法,但是当它尝试调用 customMethd
从 applicationDidBecomeActive ...
中的投放对象崩溃
The above application will compile. It loads the IDFAMainScene file okay as customMethod
gets called and logs "custom method called"
from didLoadFromCCB
, but when it then tries to call the customMethd
from the cast object in applicationDidBecomeActive...
it crashes with error
-[CCScene customMethod]: unrecognized selector sent to instance 0x175b7e50
推荐答案
loadAsScene方法返回一个CCScene对象,你的自定义类作为其唯一的子对象,因此你需要改变这个代码来获得你的自定义类如下(我也转换为点符号,因为我喜欢传播它, ):
The loadAsScene method returns a CCScene object with your custom class as its only child. Hence you need to change this code to get your custom class as follows (I also converted to dot notation as I like to propagate it whenever possible):
CCScene *scene = [CCDirector sharedDirector].runningScene;
IDFAMainScene *mainScene = (IDFAMainScene*)scene.children.firstObject;
[mainScene customMethod];
这篇关于Cocos2d V3 iOS - 如何从App Delegate访问runningScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!