cocos2d: animation stopped. Integrate Cocos2D and UIKit - iphone

I already integrate Cocos2D and UIKit.
I have the navigation among the views and the first time that I open the cocos view, it works.
But when I return to my main menu, the log console displays:
cocos2d: animation stopped
After that, if I try to get in to the cocos2D view again, the animation does not start.
What can I do to solve this?
I followed this tutorial but it donĀ“t help
http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

There have been issues regarding this. There was a similar discussion in another SO Question.
Whenever I want to include UIKit elements, I tend to do it the other way round.
With the CCUIViewWrapper code at: https://github.com/splhack/CCUIViewWrapper

This maybe be different depending on the version of cocos2d you are using, but stopAnimation should be being called on CCDirectorIOS.m:viewDidDisappear and startAnimation should be being called on viewWillAppear. So I would set breakpoints there to make sure it is being called on. And if your -(void) mainLoop:(id)sender is running.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startAnimation];
}
-(void) viewDidDisappear:(BOOL)animated
{
[self stopAnimation];
[super viewDidDisappear:animated];
}
If you want to investigate further the mainLoop calls drawScene and if it is not isPaused, then the CCScheduler will update the CCActionManager which runs all of the animations.
Hope this helps.

Related

iPod Touch iOS 5.1.1 Not calling viewDidDisappear:animated

I've posted this question elsewhere, but as SO is such a great community I'm doing so here as well.
First up, I'm using Cocos2D 2.0-gles20 to put a multiplayer/team oriented game together.
I've been integrating GameKitHelper into the app. To date it's been working just fine on my iPhone4 and iPad2 and in the Simulator, but now when I try to use it on an iPod Touch 4th I'm getting assertions in [CCDirectorIOS startAnimation] because the app is getting a viewWillAppear when it shouldn't and no call to viewDidDisappear when it should.
The reason this matters is that these methods on the CCDirectorIOS class cause Cocos2D to start/stop animation whilst another UIKit view is in front. This is something that I've managed myself with Cocos2D-0.99 but with 2.0 it is handled nicely within the director so that each app doesn't have to handle it specifically.
The GameKitHelper class has the following methods for pushing a GKMatchmakerViewController onto the screen:
-(void) showMatchmakerWithInvite:(GKInvite*)invite
{
GKMatchmakerViewController* inviteVC = [[[GKMatchmakerViewController alloc] initWithInvite:invite] autorelease];
if (inviteVC != nil)
{
inviteVC.matchmakerDelegate = self;
[self presentViewController:inviteVC];
}
}
-(UIViewController*) getRootViewController
{
return [CCDirector sharedDirector];
}
-(void) presentViewController:(UIViewController*)vc
{
UIViewController* rootVC = [self getRootViewController];
[rootVC presentModalViewController:vc animated:YES];
}
-(void) dismissModalViewController
{
UIViewController* rootVC = [self getRootViewController];
[rootVC dismissModalViewControllerAnimated:YES];
}
When I call showMatchmakerWithInvite, on the iPhone4, etc I see a call to viewDidDisappear: on the CCDirectorIOS object which stops animation. This is fine. When the GK view is gone, I see a call to viewWillAppear which restarts the animation. Sweet.
On the iPod Touch however, running exactly the same project, the call to viewDidDisappear is not made, but a call to viewWillAppear is, before the GK view has gone.
I can't fathom why there would be a difference. All devices are running iOS 5.1.1.
It's almost as if the behaviour of UIKit is different on the iPod Touch, but I find that hard to believe. My other thought was that I was looking at a timing issue, but I put some code in to allow the app to keep running even with the problem, but the call to viewDidDisappear never happened.
I can work around this I think by managing the start/stop of animation myself, but I would have preferred not to customise the Cocos2D code.
Does anyone have any ideas?
Thanks
Well, being the impatient person I am, rather than leave it to others and work on something else, I nutted it out.
I turns out that the iPod Touch devices in question had multi player games disabled in the restrictions app. This seems to cause the GK view to not show "properly" and as a result the events like viewDidDisappear: and viewWillAppear: don't occur the way I was expecting.
So I've been able to revert all of my tweaks and instrumentation in the Cocos2D code, and simply apply a correction to the GameKitHelper class to ensure that if features such as multi-player are disabled, the player isn't able to request them.

Cocos2D isTouchEnabled = NO; scene release and crash

I'm working on an iPhone game using cocos2d and I'm new to Objective C and Cocos2d so I'm sorry if this is a really beginner question. I've done a lot of searching and I can't seem to find a solution for this problem. I've found that cocos2d will not call the dealloc function, release a scene, or a layer unless I put -(void) onExit{self.isTouchEnabled = NO;} at the end of the .m file. The problem is that in the next scene the game crashes when the screen is touched, even if I put self.isTouchEnabled = YES; in the init method, onEntermethod, or anywhere else within the second scene.
When the game crashes (gbd) points to:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if( dispatchEvents )
[self touches:touches withEvent:event withTouchType:kCCTouchBegan];
}
as the source of the problem stating "EXC_BAD_ACCESS"
Please help!! Thank you!!
I am new to iPhone as well:-) But for my experiences, the reason you get "EXC_BAD_ACCESS" is not because of your code here. it should be "release" problem. i guess you release a released object...
you can try to use Xcode's Zombies instruments to detect where you get this problem. There are heaps of tutorials about Xcode Zombies online, just google it if you want.
good luck:-)

Cocos2d for this?

I just finished my concept for an iphone app. I have a main program and in that program I want to start a game.
MAIN PROGRAM (BUTTON 1 / BUTTON 2 / START GAME)
|
|
Cocos2d Game
Is this possible? To use cocos2D in a normal "iphone app"???
Thanks so much!!!
It is definitely possible to mix UIKit with Cocos2d. Usually people accomplish this by putting their UIKit views inside a Cocos2d layer. It sounds like you want to embed the Cocos2d game inside a UIKit view instead. This should be possible, but I am unsure of the specifics involved. Try looking at -(BOOL)attachInView:(UIView *) view in CCDirector.
Note that Cocos2d uses OpenGL and there are possible performance issues when mixing OpenGL and UIKit. Pausing Cocos2d when using the other part of your application will help.
I found a vast array of great tutorials on Cocos2D for iPhone at Ray Wenderlich's site. One tutorial you might especially want to check out is
How to integrate Cocos2D and UIKit
Note: the original "Cocos2D" framework was written in Python and not designed for iOS; here's the actual homepage for the iOS port.
It is possible, but you should know that the main core of your app must be controlled by Cocos2D, that means CCDirector needs to be instantiated in AppDelegate.
Start with:
a). install Cocos2D:
Go to Terminal and write (if you save cocos2D file in desktop, if not use the path where you save it)
cd Desktop/cocos2d-iphone-1.1-beta
sudo ./install-templates.sh
and if you couldn't install try again with
./install-templates.sh -f -u
b). Import cocos2d.h inside AppDelegate
c). Declare CCDirector in applicationDidFinishLaunching method inside
AppDelegate (you should check the code for this in Cocos2D template is pretty straight
forward)
I did this in my applicationDidFinishLaunching method:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
CCDirector *director = [CCDirector sharedDirector];
// Create an EAGLView with a RGB8 color buffer, and a depth buffer of 24-bits
EAGLView *glView = [EAGLView viewWithFrame:[window frame]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
[glView setMultipleTouchEnabled:YES];
// attach the openglView to the director
[director setOpenGLView:glView];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(#"Retina Display Not supported");
// make the OpenGLView a child of the main window
[window addSubview:glView];
// make main window visible
[window makeKeyAndVisible];
GameScene *gs = [GameScene node];
[[CCDirector sharedDirector] runWithScene:gs];
//General is part of UIKit where I load the tabbar
General *principal;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
principal = [[General alloc] initWithNibName:#"General-iPad" bundle:nil];
} else {
principal = [[General alloc] initWithNibName:#"General" bundle:nil];
}
self.general = principal;
[principal release];
[self showUIViewController:general];
}
To push a scene from tabbar in UIKit
[[CCDirector sharedDirector] pushScene: [CCTransitionMoveInB transitionWithDuration:0.0f scene:[MyScene scene]]];
Next I recommend you to follow Ray Wenderlich's tutorial How to integrate Cocos2D and UIKit and read this post aswell how we can show UIViewController and UIView by using cocos2d? Maybe you should search for some code in the Cocos2D forums too, always help to see that.
EDIT:
1) You can download a easy example from here: UIKit Cocos2D
You could check out this framework too: Kobol2D "is an extended and improved version of the popular Cocos2D for iPhone game engine. Everything you know about Cocos2D can still be applied, and Kobold2D is easier to get started with, more convenient to use, more powerful and flexible than Cocos2D with all the documentation available online and offline. Use Kobold2D to develop iPhone, iPod touch, iPad and Mac OS X games for both Apple App Stores. And it has some examples using UIKIT and Cocos2D".
You can certainly create a game with your requirements in Cocos2D. You might want to ask some follow-up questions over at the Cocos2D forums and read through their documentation.
You can probably do this easily......
You have to only make the (UIView *) view in CCDirector.
after attaching the view.....
Yes.It is very easy and handy to implement Cocos2D with normal Iphone FrameWork.Refer Ray Wenderlich UIKit and Cocos2D Integration Tutorial here.It will helpful to you.
Yes! Is very possible, only install the Cocos2d and his templates.
Inclusive, I used Cocos2d to create simple apps with a lot of effects :)

How to force screen update for UIScrollView from non-UI thread/function (iPhone)

I am drawing a set of images on the uiscrollview from a non-ui thread/function. But its only displayed after all the images are done drawing. For drawing all the images, I have written a function and that is what is being called as the non-ui thread. I did write this line inside the function
[self performSelectorOnMainThread:#selector(updateUI) withObject:nil waitUntilDone:NO];
And have written the function as given below
- (void)updateUI
{
[myScrollView setNeedsDisplay];
}
But its having no effect even when I can see the control being passed to that function. What to do now?
In my experience all drawing operations must be performed on the UI thread, even if they are not visible, or even added to the view hierarchy, when you are doing them.
If you find a way around that I'd be keen to know, though :-)
Ok, I got a solution. Its working perfectly even though I dont like the solution. I changed my function like this.
- (void)updateUI
{
for(UIView *subview in [myScrollView subviews])
{
[myScrollView bringSubviewToFront:subview];
}
}
Maybe you should also call setNeedsLayout and/or layoutIfNeeded?
Try something like performSelectorOnMainThread:withObject:waitUntilDone:

Combining OpenGL ES template and Utility template

I would like to have an OpenGL view with an utility application.
What I do:
I create an OpenGL ES application template and a Utility Application template.
I copy the files EAGLView.m and h, and the five ESrenderer. files from the Opengl project to the utility project.
I copy these lines from the OpenGl project to the Utility project: (in utilityAppDelegate.m)
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
[glView startAnimation];
}
- (void) applicationWillResignActive:(UIApplication *)application
{
[glView stopAnimation];
}
- (void) applicationDidBecomeActive:(UIApplication *)application
{
[glView startAnimation];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[glView stopAnimation];
}
- (void)dealloc {
...
[glView release];
...
}
And in the utilityAppDelegate.h I add:
#class EAGLView;
EAGLView *glView;
#property (nonatomic, retain) IBOutlet EAGLView *glView;
I go to the view's identity inspector, and change the class identity to EAGLview.
I open the mainview.xib and mainwindow.xib and drag the app_delegate from the mainwindow.xib, to be able to connect the glView outlet to the view.
(Don't know if this is the way to do it, but that's the only way I could connect the glView variable to the view)
When I build and run, the multi colored square show up on the screen, but it does not animate!
When I debug, the glView variable is 0 in applicationDidFinishLaunching, and the startAnimation is not called. I suspect this has something to to with the way the outlet is connected to the view? Probably not connected. :-)
The mistake is probably obvious, but I have only been 5 weeks on this platform.
What is missing? What have I done wrong?
Thank you!
I don't know if you've provided enough information to tell exactly what's going wrong in your case. However, I'd suggest that you look at the source code for my iPhone application Molecules.
While it is a little more complex, the application is based on those two templates. It uses an OpenGL ES main view (where a 3-D molecular rendering is presented), and a table view / navigation controller on the flipside (for switching molecular structures, examining their properties, and downloading new ones). You could review that example to see what you might be doing wrong, or even just replace my rendering and view code with your own.
I got the solution in another forum:
http://iphonedevbook.com/forum/viewtopic.php?f=25&t=3192&sid=9cf79468b81a8fd6c9d9020958d33388
Basically, just add the glView outlet to the MainViewController instead of the AppDelegate.