Adding a scene above Uiview on CCDirector - iphone

I want to add a UiView on the CCDirector and a scene on that UIView.
Actually i have some custom Cocoa touch control and that i want to use in my game and above that i need a scene.
Currently i am doing like this.
[[[CCDirector sharedDirector] openGLView] addSubview:gridViewController.view];
and after that line i do
[[CCDirector sharedDirector] runWithScene:[MYScene scene]];
[MYScene scene] returns a CCScene instance.
But what happens that on my custom cocoa touch control is visible but my scene is not visible at all.
Can any one guide me for that how i can accomplish my task.

You should try the opposite approach and have the UIView handled as a CCNode, then add the node to the scene.
There is even a class that makes this easily possible: CCUIViewWrapper.
If you use it, you can do the following:
CCUIViewWrapper* wrapper = [CCUIViewWrapper wrapperForUIView:view];
wrapper.contentSize = view.frame.size;
[self addChild:wrapper z:1 tag:LayerTagUILayer];
and you will get both the UIView and the scene added to CCDirector.glView. The UIView can be any, you can even define it in IB.

Related

How to add UITextView in CCLayer or CCScene in cocos2d-iphone

I want to add an instruction page where instruction written in UITextView. How to do it in Cocos2d. Because this is a game page and I want to take menu transaction effects of cocos2d like CCTransitionRadialCCW.
I wrote this code
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,60, 300,360)];
textView.backgroundColor = [UIColor clearColor];
textView.text = #"I am First enemy";
[textView setEditable:NO];
[[[CCDirector sharedDirector]openGLView]addSubview:textView];
But there is problem that it is added to the main CCDirector page, while I am writing this code
// [[[CCDirector sharedDirector]openGLView]addSubview:textView];
// replaced by :-
[self addChild:textView];
It gives me error. Please tell me how to add UITextView in CCScene or CCLayer.
I know that [self addChild:(CCNode *)node];
addChild: method needs CCNode so please tell me that how can typecast or convert UITextView object to CCNode.
If there is some alternate option for that please tell me .Like CCLabelTTF is alternate for UILabel, is there alternate for UITableView in Cocos2d-iphone.
Thanks in advance
Have a look at CCTableView: http://www.cocos2d-iphone.org/archives/943.
Combining UIKit and Cocos2D in such a way that you have the interleaving you have mentioned is likely to be very difficult, because everything done in Cocos2D is drawn in one OpenGL view. When you add a UIView, you can only add it in front of or behind Cocos2D's view.
No matter which way you're adding the UIView to cocos2d, it will not be animated by the cocos2d transition effects because simply speaking the UIView objects exist outside the OpenGL view that cocos2d uses (and animates in the case of transitions).
Adding UIViews to a cocos2d view as in your first example code is correct. But in order to position the view like a node, you should consider using the CCUIViewWrapper.

Textures not displaying - Cocos2D 2.0 Alpha

I know Cocos2d 2.0 is in alpha and will obviously have certain issues with it. I am trying to convert several projects to use OpenGL 2.0 for multiple reasons, but am having one very strange issue.
When I start up a scene, my textures load as desired and everything works great. I then display some Apple UI elements that allow me to quit the scene. The scene and its view controller are deallocated, the director is told to end, and I return to another view. Then I press a UIButton that launches the scene again. The scene begins again and runs, but no textures are visible. Fonts load and display, audio loads and plays, but no textures are visible. When dumping info from the CCTextureCache, I can see that the desired textures are available in the cache ( [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo] ). I have also tried clearing the cache before the scene is relaunched. No matter what, I can not see any textures or add nodes to the scene.
There could be something wrong with how I reset the scene, or how the EAGLView is setup. Here is the code for starting said scene from a View Controller:
(void)startGame {
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [EAGLView viewWithFrame:[self.view bounds]];
[director setOpenGLView:glView];
[self setView:glView];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
//[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[Cocos2DChipmunkScene scene]];
}
Then, the scene is quit, and the following is called:
CCDirector *director = [CCDirector sharedDirector];
[[director openGLView] removeFromSuperview];
[director end];
The above startGame method is called again when I try to start the game again. This is the point where no textures are displayed.
Let me know if I need to provide more details on this issue.
My recommendation is to not shut down the director and keep the openGLView in the hierarchy, just set it as hidden: openGLView.hidden = YES;
You can start and stop cocos2d via [[CCDirector sharedDirector] startAnimation] respectively stopAnimation.

hot to disable touch handling in CCLayer in cocos2d

I've got a CCLayer subclass i'm using to display some sprites and to show some animations. Also it has a CCMenu with some items. When user selects some of the menu item i want to run an animation and then to show another scene. But i want user not to be able to touch anything on the screen while animation is running.
Of course, i can just disable handling touches in my callbacks, but maybe there is more simple way - just to disable all touch handling for a while ?
Disable touch dispatcher before animation running and enable touch dispatcher after animation stopped. Here is the code snippet:
[[CCDirector sharedDirector] touchDispatcher].dispatchEvents = NO;
CCAnimation* animation = [CCAnimation animationWithFrame:#"numberexplode" frameCount:5 delay:0.2];
CCAnimate* animate = [CCAnimate actionWithAnimation:animation];
CCCallBlock* completion = [CCCallBlock actionWithBlock:^{
[[CCDirector sharedDirector] touchDispatcher].dispatchEvents = YES;
}];
CCSequence* sequence = [CCSequence actions:animate, completion, nil];
[self runAction:sequence];
You want to look at the CCTouchDispatcher singleton class. If you add a targeted touch handler that swallows touches (and does nothing) then you won't get any touches handled. As far as I can tell there's no way to totally disable touches.
Alternatively you can make a new CCLayer that's on top of everything else (I think z order really high will do this), and make it clear, and have it do nothing with touches.
hope that helps.

How to detect touch on UIImageView inside UIScrollview?

I have a UIScrollview in my app and I populate it with LOTS of UIImageViews approx 900. They are all very small and consist of only two different images over and over again.
Now I am having a lot of trouble detecting a touch on one of these UIImageViews.
I have assigned them all a unique TAG so as to be able to distinguish between them but I am really struggling to detect the touch.
The goal is just to be able to change the image of the touched UIImageView.
Due to the large amount of views involved a simple loop checking touch coordinates against each UIImageViews frame is just hanging my app.
Does anyone have any suggestions?
Thanks in Advance.
Ben
In my case the easiest way to do it was adding a gesture recognizer:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
//Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
singleTap.cancelsTouchesInView = NO;
[myScrollView addGestureRecognizer:singleTap];
Then use this method to capture the touch:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:myScrollView];
}
UIImageView has touch processing turned off by default. To change that set userInteractionEnabled to YES (see the docs here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)
Also if you want to know which view was hit in a view hierarchy you can use hitTest:withEvent: (see docs in UIView) which will be much faster than you looping through the hierarchy.
On a wider note I don't think having 900 UIImageView's on the screen all at once is going to be a good long term strategy. Have you investigated drawing the screen's content via CoreGraphics?
The best way to solve this issue is to subclass UIScrollView, add touchesBegan etc methods to it.
#interface MyScroll : UIScrollView
{
}
You could easily implement this method on the UIImageView object:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
//You clicked inside the object
}
return [super hitTest:point withEvent:event]
}
Use the return to tell the application the next touch responder (such as the uiimageview or the scrollview).

MPMoviePlayerController into a view

I need to play a video into a cocos2d project.. and my question is: how can I put MPMoviePlayerController into my view like this:?
UIView *theView = [[CCDirector sharedDirector] openGLView];
thanks very much!!
You can't put controllers into views. However, you can put the views that they control into a view. Try this:
// instantiate and configure your MPMoviePlayerController here, c
// call it movieController
[theView addSubview:movieController.view];
disclaimer: I'm at the office so I have no way of knowing if this will work.