I want to run a cocos2D scene on a UIView, called camera_view.
So I try adding the camera_view to the openGLView :
[[CCDirector sharedDirector].openGLView addSubview:cameraView];
And then push my scene, called scene :
[[CCDirector sharedDirector] pushScene: scene];
But after doing that, I can only see the UIView, and the cocos2D scene is no longer visible. However, before adding the camera_view as a subview of the openGLView, the scene was working absolutely fine.
How can I fix this issue ?
Thanks.
My solution is as follows :
I added my camera_view and the openGLView, each as a subview of the UIWindow :
[window addSubview: camera_view];
[window addSubView: [CCDirector sharedDirector].openGLView];
Then I pushed the cocos2D scene :
[[CCDirector sharedDirector] pushScene: scene];
And that solved the problem. Now I can view the cocos2D scene on my UIView.
Related
I'm starting the game with a UIViewController in this way:
navController = [[MyNavigationController alloc] initWithRootViewController:myController];
and when you push the game button the game start the scene in this way:
[[CCDirector sharedDirector] runWithScene:gameScene];
ok, now when I want to quit I just replace the UIViewController (because I did the menu with UIKit and the game with cocos2d) with the starting view controller to do the animation I want, it works fine... but obviously the old running scene still remains in memory, it is not deallocated in any way, I need to remove the scene and make the app as it was at the first running.
The replaceScene didn't work, I just need to stop the running scene and make everything as it was when the app started running, from the [CCDirector sharedDirector] direcly, how can I do it? popScene won't work too.
you can remove the CCDirector from the application.
the director is a simple UIViewController so, you can call
[[CCDirector sharedDirector] removeFromParentViewController];
and after call the [[CCDirector sharedDirector] end]; to remove the singlenton instance
In one of my ViewControllers (ViewController A), I have the following code:
AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:#"AlertViewController" bundle:nil];
[self.view addSubview:[aViewController view]];
[self.view bringSubviewToFront:[aViewController view]];
And in AlertViewController, I have a button and when the user clicks on it, I have:
[self.view removeFromSuperview];
Whenever I click the button, the result is EXC_BAD_ACCESS.
I'm unable to figure out the problem. My project is using ARC and ViewController A is part of a navigation controller stack if that info helps.
The problem here is that the UIView doesn't own its UIViewController. In the first block of code you held the UIView around by adding it to a subview, but let the UIViewController go away. The UIView from a UIViewController is special, you can't let this happen.
Make sure the UIViewController that created the UIView lives as long as the view does.
so i'm currently working on an app that draws something on the screen, like a graph and things like that. while testing it and it's functionality i came across this issue.
i have a button that when pressed goes to a settings scene. i initialize the scene like this
+(id) scenew
{
CCScene *scene = [CCScene node];
Settings *layer = [Settings node];
[scene addChild: layer];
return scene;
}
in that scene i have two more buttons, a done button and a what's new button.
done = [CCMenuItemImage itemFromNormalImage:#"done.png" selectedImage:#"done.png" target:self selector:#selector(done:)];
pressing the what's new button goes instantly but the done button has a 2-3 seconds delay.
the app has some console messages appearing and it seems that when it is pressed it recalculates the whole graph just like when starting the app.
all the buttons call the same CCDirector function which is replaceScene.
-(void) whatNew: (id)sender
{
[[CCDirector sharedDirector] replaceScene: [New scene]];
}
-(void) done: (id)sender
{
[[CCDirector sharedDirector] replaceScene: [Main scene]];
}
is there a way for me to optimize this a little...i mean...an efficient way to use the replaceScene, or maybe something else that doesn't force the recalculation of the graph? because whenever that button is pressed it practically jumps at the top of my graph class implementation which has 4500+ lines O.o
I assume the first button you mentioned above is inside the Main scene, going to the Setting scene.
To retain the Main scene in the memory while transferring the app flow to the Setting scene, all you have to do is to use pushScene: instead of replaceScene::
// in Main scene class
[[CCDirector sharedDirector] pushScene:[Setting scene]];
Then, from inside Setting scene, simply use popScene to return to the Main scene:
-(void) done: (id)sender
{
[[CCDirector sharedDirector] popScene];
}
Basically pushScene: simply push the new scene on top of the current scene while popScene pops it out, reverting the app flow back to the previous scene that still remains in the device memory.
I've been running through a good Cocos2d tutorial to implement iAds and am close to getting it implemented (I get iAd messages from the console)...
I keep coming back to this Warning on:
CCGLView *eaglView = [[CCDirector sharedDirector] openGLView];
"Instance method '-openGLView' not found..."
I think it has something to do with the switch from calling GLView to CCGLView (cocos2d)...
By using type CCGLView, I guess you are using cocos2d-iphone 2.x, while 1.x doesn't have CCGLView but have EAGLView.
In 1.x usually we access the property openGLView to get the OpenGL view object:
EAGLView *eaglView = [[CCDirector sharedDirector] openGLView];
In 2.x, CCDirector class doesn't have such a property. Instead, CCDirector is now a subclass of UIViewController on iOS (and NSObject on Mac OS X). So, if you want to get the OpenGL view object on iOS, just do this:
CCGLView *ccglView = (CCGLView *)[[CCDirector sharedDirector] view];
since view is a property of UIViewController.
Adding Three20 TTPhotoViewController on an empty UIWindow Rotation were working like a charm.
But when I moved the TTPhotoViewController to be created from UINavigationController inside a UITabBarController it does not rotate at all.
I made sure I return YES for every shouldAutorotateToInterfaceOrientation function.
Does Three20 Photo Gallery work in side UITabBarController with rotation?
How am I doing this?
[[TTURLRequestQueue mainQueue] setMaxContentLength:0];
TTNavigator *navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
navigator.window = [UIApplication sharedApplication].keyWindow;
TTURLMap *map = navigator.URLMap;
[map from:#"tt://appPhotos" toSharedViewController:[PhotoViewController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"tt://appPhotos"]];
Update 1:
after reading some posts I can now rotate the images only inside the TTScrollView but the navigation bar is not rotating.
Update 2:
I have subclass-ed both UITabBarController and UINavigationController to override shouldAutorotateToInterfaceOrientation, but it did not help.
I have weirdly solved my issue by removing the TabBarController from the Window object and when going back I add the TabBarController to the window again.
You could just rotate the scrollView.. Anyway if you rotate the navController it would look weird when you pop the photoviewcontroller (if your gallery isn't rootViewController). I've rotated the scrollView and hided the navBar and bottom bar when going into landscape mode and it looks good.