Dealing with screen resolutions in cocos2d - cocos2d-iphone

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)

Related

Zooming in and out of Scene in Cocos 2D-X 3.2

I'm pretty new to Cocos 2D-X but have some decent background in C++. I have a sprite _rocket (tied to a Box2D body) that occasionally moves outside the visible view of my screen. I'd like for the view to automatically zoom out as the sprite approaches the edge of the screen, so that the sprite is always in the view. When the sprite returns to the original view frame, the view should scale back to its original size.
I was able to zoom out with the following code in the update function:
Size winSize = Director::getInstance()->getWinSize();
if ((_rocket->getPosition().x - _rocket->getContentSize().width/2 < 10.0) ||
(_rocket->getPosition().x + _rocket->getContentSize().width/2 > winSize.width - 10.0) ||
(_rocket->getPosition().y - _rocket->getContentSize().width/2 < 10.0) ||
(_rocket->getPosition().y + _rocket->getContentSize().width/2 > winSize.height - 10.0))
{
this->setScale(this->getScale()-0.005);
}
However, because winSize isn't updated, this essentially scales forever, until the sprite returns to the original view. I am not sure how to update winSize so that it can be used iteratively to find the screen's edge. There may also be a much easier way of approaching this.
i don't understand why the winSize should change.
if you mean the _rock's contentsize not change
you should use
auto size = _rocket->getBoundingBox().size;
They removed some useful camera functions in cocos2d-x 3.+
The workaround is to scale/move the layer containing the game instead of trying to move the camera.

How to get winSize in Cocos2d 3.0

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;

Cocos2d CCLayerColor and CCLayerGradient alpha not working under iOS6

I just upgraded to Xcode 4.5 / iOS6, and my Cocos2d game now has an issue with transparency on CCLayerColor and CCLayerGradient. Layers created with these subclasses appear to be all-white and opaque, when in fact they should be white with transparency.
ccColor4B topStartColor = ccc4(255, 255, 255, 150);
ccColor4B topEndColor = ccc4(255, 255, 255, 100);
CGPoint topVector = ccp(0, 1);
_topGradient = [CCLayerGradient layerWithColor:topStartColor
fadingTo:topEndColor
alongVector:topVector];
I am on Cocos2d 2.0 Beta2. I did have to rework my AppDelegate to deal with the screen rotation issues caused by iOS6, so it is possible I may have inadvertently forgotten to set something up correctly - though I have combed through it pretty carefully. I should add that sprites with alpha are working fine - it seems to be CCLayerColor and CCLayerGradient only. I tested CCLayerColor in a stock Cocos2d 2.0 project, and it seems to work correctly there, so it's something in my app - but I cannot figure out what's different about my project.
This was in fact a bug in Cocos2d 2.0 Beta 2, and has been fixed in the development branch.

Disabling Autorotation for Game Center

I'm trying to implement the Game Center Achievements in my first game. The orientation of my game is landscape left only.
What I have done so far is to change GameConfig.h as below:
#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationNone
#elif __arm__
#define GAME_AUTOROTATION kGameAutorotationNone
My game is then fixed in the Landscape Left orientation at all times. However, when I enter the Game Center Achievement screen from my game, the autorotation is still happening. I am a bit confused with the concept here. Could you guys please tell me why this is happening? And How can I fix this?
Thanks very much in advance. I would really appreciate for your help.
Look into your gameview controller or equivalent version of it in your code. Search for the following function:
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation ;
Comment out the code inside it and only return NO.
I hope this will fix your problem... let me know if it's not working.
Put this in AppDelegate.m before #implementation
#interface UINavigationController (Private)
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
#end
#implementation UINavigationController (Private)
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
return YES;
}
#end

Cocos2d - a way to get the current scene?

I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.
Thanks!
Check out CCDirector. You can get the running scene like this:
[[CCDirector sharedDirector] runningScene];
From the documentation of cocos2D:
-(CCScene*) runningScene [read, assign]
The current running Scene. Director can only run one Scene at the time
Sandro Meier