I'm porting a code to ios7 in wich I used old GKPeerPickerController and cocos2d to make a multiplayer game over Bluetooth.
In the new version, I'm using the MCBrowserViewController, but im unable to show it on the scene.
Im calling it with
[[[CCDirector sharedDirector] view] addSubview: browserVC];
where browserVC is an initiallized MCBrowserViewController but as this is only to show UIViews I get the error
Cannot initialize a parameter of type 'UIView *' with an rvalue of
type 'MCBrowserViewController *'
Is there any way to show the MCBrowserViewController on the scene?
Thanks in advance!
Should work
[[CCDirector sharedDirector] presentViewController:browserVC animated:YES completion:nil];
Related
In my HelloWorldLayer code I am pushing a SettingsLayer scene onto the stack.
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5 scene:[SettingsLayer scene]]];
The SettingsLayer needs to call a delegate method implemented in the HelloWorldLayer. But I am at a lost as to how I can set the HelloWorldLayer as the SettingsLayer's delegate. Can someone show me the proper pattern for this? I tried alloc-init of the SettingsLayer, and setting the delegate before the pushscene but this did not work.
In your code:
SettingsLayer *mySettingsLayer = [[SettingsLayer alloc] init];
mySettingsLayer.delegate = self;
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5
scene:[[mySettingsLayer class] scene]]];
The problem is in the last line:
[[mySettingsLayer class] scene]
This creates a new instance of the SettingsLayer class which has no delegate set (it will be nil). The mySettingsLayer you created before isn't actually presented as a scene, it goes out of scope and deallocates (assuming you are using ARC, if not, this would actually leak the mySettingsLayer object).
To fix it pass in your already existing mySettingsLayer instance:
[[CCDirector sharedDirector] pushScene:[CCTransitionFade transitionWithDuration:0.5
scene:mySettingsLayer]];
In cocos2d-x, I cache the animation in CCAnimationCache. But how can I clear all the animation-cache?
You should try
CCAnimationCache::sharedAnimationCache()->purgeSharedAnimationCache();
It releases all the CCAnimation objects and the shared instance.
Else you can also use
removeAnimationByName("youAnimationName");
this will remove particular animation you wish.
Hope this helps you.. :)
In cocos2d, we use these calls...find same api in cocos2d-x
[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[CCTextureCache purgeSharedTextureCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
Based on the use CCDirector makes of CCLabelAtlas, I tried the following:
scoreLabel = [[CCLabelAtlas alloc] initWithString:#"0123456789" charMapFile:#"fps_images.png" itemWidth:8 itemHeight:12 startCharMap:'.'];
[scoreLabel setPosition:ccp(200, 200)];
[self addChild:scoreLabel];
I must be missing something as nothing displays. I'm using the basic .png that's supplied with Cocos2D.
What am I missing? Thanks!
I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.
Thanks!
Check out CCDirector. You can get the running scene like this:
[[CCDirector sharedDirector] runningScene];
From the documentation of cocos2D:
-(CCScene*) runningScene [read, assign]
The current running Scene. Director can only run one Scene at the time
Sandro Meier
I want to restart the current scene i am on. I thought of using replaceScene and replace it with itself. Is that ok to do ?
Level2Scene *scene = [Level2Scene node];
[[CCDirector sharedDirector] replaceScene:scene];
Level2Scene is the current scene I am in.
Thanks
Abhinav
Using replaceScene: is a good way to restart the level, if levels are represented by scenes. Just make sure that if you have any global state (stored outside of the scene) that you reset that, too.