UIimageView problem - iphone

Hi Friends'
i am new to using cocos2d application , and i want to build a grid of images using a single square image.
i make that in UIImageview now i want to ask that how can i add that...
if i use [self addchild: (CCNode)square] it show error .
thanks for any help in advance

You do not use UIImageView's with cocos2d.
Try this.
CCSprite *square = [CCSprite spriteWithFile:#"yourimage.png"];
[self addChild:square];
I'm presuming self is a CCLayer.
Sounds like you need up on some basics.
There are some good beginner tutorials here.
http://www.learn-cocos2d.com/

Related

Change CCSprite images at runtime

need to change the CCSprite images at runtime, i have to delete the previous loaded images and display new after a specific inter of NSTimer, i am using [imagesContainer removeChildByTag:0 cleanup:YES]; but not working
any help will be highly appreciated
You could remove the sprite like you're doing and add a new sprite but remember to add it to the layer with addChild:
CCSprite *theNewSprite = [CCSprite spriteWithFile:#"newImage.png"];
[LayerYouWantToAdd addChild:theNewSprite]
Or just change the texture of the sprite. This works for me.
[theSprite setTexture:[[CCSprite spriteWithFile:#"newImage.png"]texture]];

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.

PNG Transparency in cocos2d iphone issue

I've just begun cocos2d development on the iphone. An issue I'm having is that when I add a CCSprite to my scene, instead of it being transparent around the edges of the .png file, its showing up as white.
So for example, I define the CCSprite in my header file -
CCSprite *foo;
Then when I initialise my scene -
foo = [CCSprite spriteWithFile:#"foo.png"];
[self addChild:player z:0 tag:1];
Is there anything wrong with this code?
Thanks for any help.
P.S. I have double checked my .png file in Photoshop and its correctly showing up as transparent where it should be.
Check the bottom portion of this page:
http://www.cocos2d-iphone.org/wiki/doku.php/faq#my_png_doesn_t_look_like_in_photoshop

add animation in cocos2d

how can I add animation in cocos2d in a sprite, the problem is that whenever the use fire the object and hits that object, the object will remove just like an animation .. if you have any tutorial then plz send it to me...
thnx
You can use spritesheets of images and then do animation using the images.
For code examples you can try the following tutorial.
http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d
There are a lot of examples coming with cocos2d. Including animation using a set of images. Take a look at SpriteTest example - it's what you are looking for
just use this
CCSpriteSheet *mgr = [CCSpriteSheet spriteSheetWithFile:#"yourImage.png" capacity:5];
[self addChild:mgr z:0 tag:4]; //set the image and load it to the CCLayer
sprite = [CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0, 0, 30, 30)];
[sprite runAction:[CCFadeTo actionWithDuration: 1 opacity:80]];

How do I texture controls on iPhone Apps?

I would like to apply a texture to my iPhone app similar to the tab bar in GameCenter.app. Is there a good tutorial somewhere that explains this? I am hoping for a method that will translate easily to other controls as well. Thanks!
A simple way to add texture for backgrounds is to use the built in colorWithPatternImage.
UIImage *image = [UIImage imageNamed:#"backgroundPattern"];
[self setBackgroundColor:[UIColor colorWithPatternImage:image]];
If you have a toolbar you should be able to use your "Patternized UIView" with UIBarButtonItem, one of its initializers take a UIView.