How to get winSize in Cocos2d 3.0 - cocos2d-iphone

I used below code in Cocos2d 1.0 and Cocos2d 2.0, but it seems not found in Cocos2d 3.0
CGSize s = [[CCDirector sharedDirector] winSize];
How to get screen size in Cocos2d 3.0 ?

You can use viewSize.
CGSize s = [CCDirector sharedDirector].viewSize;

Related

Application does not respond to touches after running in background Cocos2D iPhone

Currently I'm fixing an old Cocos2D iPhone 1.0 game. There is a strange problem: when the application is being sent to background by the four-finger gesture(iPad) after returning it behaves like the touch began and didn't end, and there no way to end it because the application does not respond to touches. Also when I launch the application and do nothing, the screen becomes locked and after that the application some times does not respond to touches.
I guess that there is something I need to do in the following AppDelegate methods, but I can't figure out exactly:
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
enter code here
- (void)applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
}
The problem was in -(void)ccTouchCanceled method not implemented

How can you show an MCBrowserViewController on a scene in cocos2d?

I'm porting a code to ios7 in wich I used old GKPeerPickerController and cocos2d to make a multiplayer game over Bluetooth.
In the new version, I'm using the MCBrowserViewController, but im unable to show it on the scene.
Im calling it with
[[[CCDirector sharedDirector] view] addSubview: browserVC];
where browserVC is an initiallized MCBrowserViewController but as this is only to show UIViews I get the error
Cannot initialize a parameter of type 'UIView *' with an rvalue of
type 'MCBrowserViewController *'
Is there any way to show the MCBrowserViewController on the scene?
Thanks in advance!
Should work
[[CCDirector sharedDirector] presentViewController:browserVC animated:YES completion:nil];

Dealing with screen resolutions in cocos2d

I’m sure this question has probably been asked before…
Right now i am porting my game over from Android. right now i have cocos2d fully integrated and i am ready to go. But i want to make sure i understand how the scaling works..
In Android you just provide a width and height for the scene and the graphics are scaled up or down based on the device.
From reading, it doesnt seem to work that way on IOS. So how do you design for screen resolutions such as the 3g, 4,5 and ipad devices?
I was reading, and i know its a best practice to not hard code screen coordinates. With this being said, it seems like the only thing that would need to be scaled would be the background image of the game?
Maybe im wrong, Im kinda of confused about how it works.
If someone could explain that would be great!
Thanks.
In Cocos2D, images are selected automatically based on their associated prefix: name.png for non-retina deivces, name-hd.png for retina, name-ipad.png for ipad 1 and mini, and name-ipadhd.png for ipad retina.
For scaling, I have some useful defines that will allow you to alter things based on the particular platform:
#define MIDSCREEN ccp([CCDirector sharedDirector].winSize.width/2, [CCDirector sharedDirector].winSize.height/2)
#define WINSIZE [[CCDirector sharedDirector] winSize]
#define WINHEIGHT [[CCDirector sharedDirector] winSize].height
#define WINWIDTH [[CCDirector sharedDirector] winSize].width
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_RETINA() (CC_CONTENT_SCALE_FACTOR() == 2)
#define REAL_CONTENT_SCALE_FACTOR() (CC_CONTENT_SCALE_FACTOR() * (IS_IPAD() + 1))
#define CONTENT_SCALE_FACTOR() (IS_IPAD() ? 2.0 : CC_CONTENT_SCALE_FACTOR())
#define IPAD_SCALE() (IS_IPAD() ? 2.0 : 1.0)
#define CCP_IPAD(___X___, ___Y___) (IS_IPAD() ? ccp((___X___ + 16.0f) * CONTENT_SCALE_FACTOR(), (___Y___ + 32.0f) * CONTENT_SCALE_FACTOR()) : ccp(___X___, ___Y___))
#define CCP_IPHONE(___X___, ___Y___) (IS_IPAD() ? ccp(___X___, ___Y___) : ccp((___X___ / 2.0) - 16.0f, (___Y___ / 2.0) - 32.0f))
#define IS_IPHONE5() ([[CCDirector sharedDirector] winSize].width == 568 || [[CCDirector sharedDirector] winSize].height == 568)

cocos2dx how to clear CCAnimationCache

In cocos2d-x, I cache the animation in CCAnimationCache. But how can I clear all the animation-cache?
You should try
CCAnimationCache::sharedAnimationCache()->purgeSharedAnimationCache();
It releases all the CCAnimation objects and the shared instance.
Else you can also use
removeAnimationByName("youAnimationName");
this will remove particular animation you wish.
Hope this helps you.. :)
In cocos2d, we use these calls...find same api in cocos2d-x
[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[CCTextureCache purgeSharedTextureCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];

Adding multiple spritesheets in cocos2d

I have the following code to set up my spritesheets and batch node:
CGSize screenSize = [[CCDirector sharedDirector] winSize];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"soldier-test.plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"soldier-running.plist"];
batchNode = [CCSpriteBatchNode batchNodeWithFile:#"soldier-test.png"];
self.player = [Player spriteWithSpriteFrameName:#"shooting s0000.bmp"];
[batchNode addChild:self.player];
[player setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:batchNode];
However, when I try to have player (a subclass of CCSprite) perform an action using frames from the second spritesheet, I get assertion errors related to the texture files. Do I need to combine the sheets into one, or is there a way to span one CCSprite over multiple spritesheets?
A SpriteBatchNode can only have children that all use the same texture. Your player needs to use the texture soldier-test.png if you want to add it to your batchNode.
With a TextureAtlas you can put multiple different textures into one big image.