PNG Transparency in cocos2d iphone issue - ios5

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

Related

cocos2d background image dimmensions

I'm trying to get started with cocos2d by creating an app with a background image. It's not a texture, just a straight image.
I add the background using:
CCSprite* background = [CCSprite spriteWithFile:#"paris.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0, 0);
[self addChild:background z:0];
The image is 960x640 but when I run it in the iPhone 5 simulator I only see a small part of the image. It's like it's too large for the screen. I was under the impression I would need 960x640. Is this not accurate? What resolution should my image be?
I've tried with and without the anchorPoint being set. Without the anchor I see a smushed image on 1/2 the screen.
Learn about retina display in cocos2d: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:how_to_develop_retinadisplay_games_in_cocos2d
Make an image with 480x320 with name paris.png...
Rename your current image as paris-hd.png...
Enable Retina Display in app delegate.. And you are done.. Hope this helps.. :)

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]];

Transparent layer used in Tiny wings

please can anyone show me how to implement using cocos2d, a half transparent layer that is tapped for game start like that obtained in the game called Tiny wings.
Thanks.
Add a CCSprite to the scene then set the opacity to something less than 255 to make it translucent.
Here is some psuedo-code:
CCSprite* myTranslucentImage = [CCSprite spriteWithFile:#"touchMe.png"];
myTranslucentImage.opacity = 128;
[scene addChild: myTranslucentImage];
I've never played Tiny Wings but I do know how to make a CCSprite transparent.
CCSprite *example = [CCSprite spriteWithImage:#"HelloWorld.png"];
[example setOpacity:160];
If you must use a CCLayer you should look at the Cocos2d documentation.

UIimageView problem

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/

How do I make a side-scrolling iPhone app?

I am working on a side-scrolling iPhone app, and I have every resource, except the actual side scrolling part! How do I make an app so that it side-scrolls? To be more specific, I want the view of the app to scroll when a UIImageview(player) hits the right/left border of the screen.
<:EDIT:>
I am extremely sorry for all of this confusion! All I am making is a SAMPLE iPhone app only to be used in the simulator. In the app, I have a UIImaageview that moves when you touch arrow UIImageviews. PLEASE NOTE THAT ALL INFORMATION SAID BEFORE THIS SENTENCE WORKS PERFECTLY! Now, my problem is that I want the UIView to scroll only ONE picture(this means it doesn't go forever ;)). I have tried using a UIScrollView, but that didn't work. Finally, I am not planning to use Cocos2D or any other game engine besides a basic UIview. This is only a test app, not a coding extravaganza...
If you're writing a game, you may benefit from a lot of the features given to you by engines such as Cocos2D.
Start with a view-based application template so you can get the gist of how this works. Later you can integrate with your existing code.
Create a seamless background image 320 x 960 pixels, named "seamless.png"
Add this ivar to your view controller .h file
UIImageView *bg;
#property (nonatomic, retain) UIImageView *bg;
In your .m file add these #defines before the #implementation line, the #synthesize after.
#define kUpdateRate (1.0 / 60.0)
#define kMoveY (3.0)
#define kTopY (0.0)
#define kBottomY (480.0)
#synthesize bg;
Add this method.
-(void)scrollView; {
float oldY = self.bg.center.y + kMoveY;
float newY = oldY;
if (oldY > kBottomY) {
newY = kTopY;
}
self.bg.center = CGPointMake(self.bg.center.x, newY);
}
Add this to your viewDidLoad method.
self.bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"endless.png"]];
[self.view addSubview:self.bg];
self.bg.center = CGPointMake(self.bg.center.x, kTopY);
[NSTimer scheduledTimerWithTimeInterval:kUpdateRate target:self selector:#selector(scrollView) userInfo:nil repeats:YES];
When you run the app, you should have a nice scrolling background.
To sum up what happens: A timer is set up to repeatedly call scrollView:
There, the image is moved downwards until it reaches a predefined point (kBottomY), at which time its position is jumped back up to the top (kTopY) and it starts over again.
You can customize it to only scroll when you want it to or scroll the other way on your own, right?
If the image being side-scrolled is large, I'd suggest scrolled view while taking advantage of CATiledLayer. See the WWDC10 Session 104 video and the "Tiling" sample code.
BTW, there is nothing in the Apple docs that seem to prejudge the use of scroll views for utility or menus.
Are you trying to use a UIScrollView? If so, just set your scrollView's content size to something wider than the screen. For example:
self.scrollView.contentSize = CGSizeMake(640, 480);
You should use a UIScollView (http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/3393-uiscrollview-tutorial.html). Or am I missing the point?
Just make the main view wider than the screen and change the bounds accordingly to show a different portion of the main view. This assumes your main view does have some kind of a limited width. It's slightly trickier if that view just goes forever. In that case you change the bounds and draw the newly revealed content.
If this doesn't make sense then I think you need to reword your question or do some experimentation to narrow down what you're asking.
Take a look at Cocos2D for the iPhone which also includes a choice of 2 collision engines, chipmunk and box2d
I can also recommend the oreilly book iPhone game development, which if you are new to game development will teach you how to roll a basic game framework