iPhone Cocos2d game change to iPad game - iphone

I have spent 1000's of hours into one of my games and just started looking at iPad stuff. Yeah... didn't realize what the whole CGSize size = [[CCDirector sharedDirector] winSize]; thing was until now.
So I'm pretty much screwed right? There's no sense in changing literally 10,000 ccp coordinates?
Is there a way:
if iPad
add or subtract to the ccp to position to put everything in the center
I could add it to every scene.

Yes no worries. I believe you just need to change your assets and just define:
float deviceScale
And you can easily check in your appDelegate, what device is the program running on:
NSString *deviceType = [UIDevice currentDevice].model;
NSLog(#"deive name is %#",deviceType);
if([deviceType isEqualToString:#"iPad"] || [deviceType isEqualToString:#"iPad Simulator"] ){
[[CCDirector sharedDirector] setContentScaleFactor:1];
} else {
if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(#"scale")])
{
if ([[UIScreen mainScreen] scale] < 1.1){
[[CCDirector sharedDirector] setContentScaleFactor:1];
}
if ([[UIScreen mainScreen] scale] > 1.9){
//retina display
[[CCDirector sharedDirector] setContentScaleFactor:2];
}
}
else {
[[CCDirector sharedDirector] setContentScaleFactor:1];
}
}
As I said before you might need to have a variable 'deviceScale', for retina/ ipad make it deviceScale = 1 for all others deviceScale = 0.5. Then scale down the images if device is non retina or not ipad.

I hope your app is using version 0.99.5. 0.99.5 is using by pixels or something.. So you wont b screwed. It will auto scale by ratio.. No worries..

Related

How to scale Cocos2d app to iPhone 5 screen?

I have a Cocos3d v1.01 app and I am currently making it try to fit the iPhone 5 full screen.
By simply stretching the background is enough for most screens as it is just a pattern that fills the screen and no need to change any UI parts, however on just one of the screens I am struggling to change.
This is the code part I believe to set the background for that;
-(void)animations
{
AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
CGSize size = [[CCDirector sharedDirector] winSize];
NSMutableArray *bodyanimframeBuddyBack =[[NSMutableArray alloc]init];
for(int j = 1;j<=4;++j)
{
[bodyanimframeBuddyBack addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:[NSString stringWithFormat:#"shark_sea_waves5%d.png",j]]];
}
CCAnimation *BuddyAnimBack = [CCAnimation animationWithFrames:bodyanimframeBuddyBack delay:0.3f];
self.backgroundAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:BuddyAnimBack restoreOriginalFrame:NO]];
CCSprite *bg=[CCSprite spriteWithSpriteFrameName:#"shark_sea_waves51.png"];
bg.position=ccp(size.width/2, size.height/2);
[self addChild:bg];
[bg runAction:backgroundAction];
Is there a way of adjusting that ? I am guessing it is the bg.position part that would need changing to tell it whether it is iPhone5 or not?
Thanks in advance,
Chris
bg.scaleX = size.width / bg.contentSize.width;
This will stretch the bg width to match the screen width.
Why can't you place seperate background for iPhone5..just like you did for iPad.
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define TEX_MM_BG (IS_IPHONE5) ? ( #"shark_sea_waves51-whd.png") : ( #"shark_sea_waves51.png")
-(void)setupBackground
{
CCSprite *bg = [CCSprite spriteWithFile:TEX_MM_BG];
bg.position = ccp(mS.width*0.5f, mS.height*0.5f);
[self addChild:bg z:-3 tag:kTagBackground];
}
//Also put these image in hard disk.
shark_sea_waves51.png //480x320
shark_sea_waves51-hd.png //960x640
shark_sea_waves51-whd.png //1136x640
shark_sea_waves51-ipad.png //1024x768
shark_sea_waves51-ipadhd.png //2048x1536

Cocos2D - Why is my FPS so low? Single scene with tilemap

I wrote a simple game with Cocos2D where I have an animated sprite walking around a plain green field.
The walking sprite is 64x64px, the joystick comes from the Sneakyjoystick class, and the green field is composed of a 5-6 32x32 pixels made into a tilemap using Tiled. Using Tiled, I created a 50 x 50 tile map.
I'm getting 20-30 FPS and I'm not sure why. I decreased the tilemap size to 10x10 tiles and it shot up to 50-60 FPS. I don't know what to do?
Edit: I've added the initialization block for the gameplay layer. I'm testing on the simulator.
-(id) init
{
if( (self=[super init] ))
{
self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:#"TileMap.tmx"];
self.background = [_tileMap layerNamed:#"Background"];
self.meta = [_tileMap layerNamed:#"Meta"];
self.topLayer = [_tileMap layerNamed:#"TopLayer"];
_topLayer.visible = YES;
_background.visible = YES;
_meta.visible = YES;
[self addChild:_tileMap z:-1];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"male_walkcycle.plist"];
sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:#"male_walkcycle.png"];
[self addChild:sceneSpriteBatchNode z:0];
CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:#"Objects"];
NSAssert(objects != nil, #"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:#"SpawnPoint"];
NSAssert(spawnPoint != nil, #"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:#"x"] intValue];
int y = [[spawnPoint valueForKey:#"y"] intValue];
Caster *casterPlayer = [[[Caster alloc] init] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"male_walkcycle_19.png"]];
casterPlayer.position = ccp(x, y);
casterPlayer.joystick = nil;
[sceneSpriteBatchNode addChild:casterPlayer z:1000 tag:kCasterTagValue];
[casterPlayer release];
CCSprite *sprite = [CCSprite node];
CGSize size = CGSizeMake(50,50);
GLubyte *buffer = malloc(sizeof(GLubyte)*4);
for (int i=0;i<4;i++) {buffer[i]=255;}
CCTexture2D *tex = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB5A1 pixelsWide:1 pixelsHigh:1 contentSize:size];
[sprite setTexture:tex];
[sprite setTextureRect:CGRectMake(0, 0, size.width, size.height)];
free(buffer);
sprite.position = ccp(x, y);
sprite.visible = NO;
[self addChild:sprite z:5 tag:debugBoxTagValue];
[self setViewpointCenter:casterPlayer.position];
[self scheduleUpdate];
}
return self;
}
I'm testing on the simulator.
I can't believe how often I need to repeat this:
Ignore the Simulator!
The Simulator is not an actual iOS device. It runs on your Mac. It has more memory and CPU power available. At the same time its rendering performance is severely limited because it's not hardware accelerated rendering, it's a software renderer.
Even the fastest 2012 iMac can't run scenes at 60 fps on the iPad or iPhone Retina Simulator. On the iPad Retina Simulator you'll be lucky to get 20 fps.
Moreover, developers are the only people ever to run their Apps on the Simulator. Your users, not even your testers, will be using the Simulator to run your app. They'll be using an actual device.
Any time spent optimizing performance on the Simulator is nothing but a complete waste of time. This generally applies to any issue that only occurs on the Simulator.
Test on a device! Every issue only observed on the iOS Simulator is moot, void, invalid, and utterly irrelevant, especially performance.

Cocos2D 2.0 screenshots on iOS 6

I have an application that takes a screenshot of a scene and saves it to a file. I have this working and the application is on the store. Today, I have downloaded iOS 6 and the method I am using is not working anymore. I tested all I know to make it work, googled around and found this:
http://www.cocos2d-iphone.org/forum/topic/37809?replies=22#post-180983
Users seem to agree that this is working on iOS 5, but I have tested this on iOS 6 and it is producing black screenshots.
I am not a specialist in Cocos2D so, I cannot say exactly what is wrong with this guy's code. the author has a sample project on github and even his project is producing black screenshots on iOS 6.
Any clues? Thanks.
thanks
I am not sure what the GitHub version does but this code will take a screenshot and I just tested it on iOS 6 and it works fine.
+(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:winSize.width
height:winSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
You can call it like this
CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *img = [AppController screenshotWithStartNode:n];
This here works for Cocos2d V3.
+(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize viewSize = [[CCDirector sharedDirector] viewSize];
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:viewSize.width
height:viewSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
Above 2 answers not worked in Cocos2d 3.2.1
Here is Solution for Cocos2d 3.2.1 +
-(UIImage*) takePresentScreenshot
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize size = [[CCDirector sharedDirector] viewSize];
CCRenderTexture *renderTxture = [CCRenderTexture renderTextureWithWidth:size.width
height:size.height];
[renderTxture begin];
[[[CCDirector sharedDirector] runningScene] visit];
[renderTxture end];
return [renderTxture getUIImage];
}
UIImage* screenshot = [self takePresentScreenshot];
The top answer works for iPads (I have tested on iPads v1 thru 4).
It does NOT work for the actual device: iPhone5, iOS7.
However, it does work for simulators of such an iPhone 5!
That inconsistency is driving me crazy!

iOs (cocos2d) screenshot cached or CCRenderTexture buffer? It doesn't change

I using tip from this ask stackoverflow.com/questions/12413460
It's work. My problem is the image not refresh while application not restart or not rebuilding.
I use method for create screenshot. I what have same functional: use touch scree, and screenshot will save into documents directory. User touch second, second screenshot save. And so. But in my app every time only first touch screenshot created, as it cached or buffer ed.
-(UIImage *) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:winSize.width
height:winSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
I call it when user touch screen (for example)
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CCNode *n = [[[CCDirector sharedDirector] runningScene].children objectAtIndex:0];
UIImage *img = [self screenshotWithStartNode:n];//this image always same by one, same when user touch screen first time
CCSprite *spriteTemp = [CCSprite spriteWithCGImage:img.CGImage key:#"image"];
[self addChild:spriteTemp];
}
On the screen items have different position in time.
First touch on the screen get right image.But all other touch create screenshots as first image and dose not change.
I think maybe it's buffer CCRenderTexture? or image cache? What i will do?
i try [img release] and try CGImageRelease(img.CGImage) after touch, but it crash app.
You're right, the previous information is being cached. Try a
[[CCTextureCache sharedTextureCache] removeTextureForKey:#"image"];
before you try to overwrite.
Or use difference key names if you need more than one image at once.

app frame rate is unstable

Hi all
writing an app for iphone using cocos2d and objective c. i have my frame rate set at 30 fps with
[[CCDirector sharedDirector] setAnimationInterval:1.0/30];
this is fine for awhile but at 1 point in the app the frame rate increases to between 60-90 fps on simulator when swapping between 2 scenes. i put a break point in to get the animationInterval value and it always says it is 0.033 so why would the frame rate be spiking like this?
i have been using
[[CCDirector sharedDirector] stopAnimations]; and
[[CCDirector sharedDirector] startAnimations]; when swapping layers and scenes but i always reset the the interval value when i start it again.
any help would be appreciated
thanks
this is the exact point when the fps goes nuts. when the gamescene is loaded
[[CCDirector sharedDirector] stopAnimation];
GameScene *gameScene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:gameScene];
[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] setAnimationInterval:1.0/30];
CCDirector *director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
Try this to get the exact interval in frames.