I have strange problem in cocos2d v3, probably some misconfiguration which I was unable to find in google.
I'm adding background for iphone5 which is 640X1136
CCSprite *background = [CCSprite spriteWithImageNamed:#"background_iphone5.png"];
background.positionType = CCPositionTypeNormalized;
background.position = ccp(0.5f, 0.5f);
[self addChild:background];
but it is scaled up about 2x times so doesn't fits the screen. Same image worked perfectly in cocos2d v2
Please help
Cocos2d, like UIKits #2x uses suffixes to differentiate images intended for different screen sizes and resolutions.
These are the ones used by Cocos2d :
ipad
ipadhd
hd
iphone5hd
So you would have to add the appropriate image for your display with the suffix into your bundle like background_iphone5.png.
In your use case Cocos2d is resizing your non-retina image to a retina size (2x height, 2x width).
For consistency reasons this is a desired behaviour.
Consider the case of somebody not non-retina resources exclusively: all the sprites would be 2 times smaller on retina displays, which would probably get your game logics out of whack.
I highly suggest using SpriteBuiler because all this resizing and naming behaviour is integrated into the tool and works seamlessly. You only create retina ipad resolution images and it resizes them for you automatically.
Related
I am a new learner in cococ2d-x game library, and I am learning how to create a simple game in iphone6. The SKD I am using is Xcode, but I have a problem in the screen resolution in the simulator, the situation is as follows:
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
CCLOG("visibleSize: visibleSize.width=%f, visibleSize.height=%f",visibleSize.width,visibleSize.height);
CCLOG("origin: origin.x=%f, origin.y=%f",origin.x, origin.y);
I use this code to print the visible size and the result is as follows:
visibleSize: visibleSize.width=180.281693,visibleSize.height=320.000000
origin: origin.x=149.859161, origin.y=0.000000
I go to google and found that the size of an iphone 6 is much bigger than the size I get from cocos2d-x code(320 x 480 points) and about the simulator, the tutorial is using iphone retina to show the demo, but I cannot found the iphone Retina simulator in my Xcode, I can only find Ipad Retina, and my simulator version is IOS9.3, this is really annoying because I cannot use the code like
node->setPosition(Point::ZERO);
to set the node to ZERO position, this object will be invisible due to the size problem.
Can someone help me please?
The resolution which you get by calling Director::getInstance()->getVisibleSize(); is not the real device's resolution. It's your Design Resolution.
Besides, if you want to understand more about cocos2d-x easy multi-resolution mechanism, check my other answer.
And notice the bottom-left point of the screen is determined by Origin Point not Point::ZERO.
The problem is, Cocos seems to think retina devices are the same resolution as standard devices. If you draw a sprite at 768,1024 on the ipad, it will put it at the upper right corner. If you draw a sprite at 768,1024 on the retina ipad, content scaling makes it so it also puts it in the upper right corner. Yet if you draw a sprite on the retina iphone 5 at 640,1136 it isn't in the upper right corner, its off the screen by 2x the distance. In order to put it in the corner you have to draw it at 320,568 because of the content scaling.
I do have a Default-568h#2x.png image, and I am on version 2.1.
My question is: Is there a way to get Cocos2d to make it so that drawing a sprite at 640,1136 on an iPhone5 will result in the sprite being in the upper right corner?
Is it possible to set up a cocos2d custom GL projection and set winSizeInPoints to equal winSizeInPixels and content scale 1.0 so that you can use the iPhone 4/5's full native resolution instead of using the half-size coordinates of older iPhones.
You can easily do this by changing the iPad suffix to use "-hd" via CCFileUtils. That way iPad devices will load the regular -hd assets for Retina iPhones.
Update regarding comments:
The positions on devices is measured in points, not pixels. So a non-retina iPad and a retina iPad both have a point resolution of 1024x768. This is great exactly because it makes adapting to screens with different pixel densities a no-brainer. This also works on the iPhone devices.
My suspicion is that you simply haven't added the Default-568h#2x.png launch image to your project yet, which may cause the widescreen devices to be treated differently.
And specifically on older cocos2d versions dated before iPhone 5 there's a bug that will treat the iPhone 5 as a non-Retina device, proper default image or not.
Yes You can share, easy way to do this is put all common sprite in sprite sheet and in runtime check if iPad, if yes then load iPhone HD sheet. We did this in many project and worked.
if(IS_IPAD)
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"GameSpriteSheet-hd.plist"];
}
else
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"GameSpriteSheet.plist"];
}
For background image, good to have separate iPad image, if scaling not look bad then you can scale image at runtime.
As far as I can understand what you are trying to do, the following approach should work:
call [director enableRetinaDisplay:NO] when setting up your CCDirector;
hack or override the following methods in CCDirector's so that winSizeInPixels is defines as the full screen resolution:
a. setContentScaleFactor:;
b. reshapeProjection:;
c. setView:;
Step 1 will make sure that no scaling factor is ever applied when rendering sprites or doing calculations; Step 2 will ensure that the full screen resolution is used whenever required (e.g., when defining the projection, but likely elsewhere as well).
About Step 2, you will notice that all listed methods show a statement like this:
winSizeInPixels_ = CGSizeMake( winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height * __ccContentScaleFactor );
__ccContentScaleFactor is made equal to 1 by step 1, and you should leave it like that; you could, e.g. customise winSizeInPixels calculation to your aim, like this:
if (<IPHONE_4INCHES>)
winSizeInPixels_ = CGSizeMake( winSizeInPoints_.width * 2, winSizeInPoints_.height * 2 );
else
winSizeInPixels_ = CGSizeMake( winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height * __ccContentScaleFactor );
Defining a custom projection would unfortunately not work because winSizeInPixels is always calculated based on __ccContentScaleFactor; but, __ccContentScaleFactor is also used everywhere in Cocos2D to position/size sprites and the likes.
A final note on implementation, you could hack this changes into the existing CCDirectorIOS class or you could derive from it your own MYCCDirectorIOS and override the methods there.
Hope it helps.
I want to use a full color PNG image as a particle in cocos2d with an emitter designed in ParticleDesigner.
I dragged in the image I want to use and set-up everything how I want it in ParticleDesigner and it looks good.
Problem is when I import into cocos2d, the particles appear to have grey squares over them (a small bit of the yellow image is visible on the side, but they gray covers the rest including transparent areas).
Code:
CCParticleSystemQuad* particleSystem = [CCParticleSystemQuad particleWithFile:#"coin magnet.plist"];
particleSystem.position = ccp(320, 320-16);
[self addChild:particleSystem z:1000];
I'm guessing it might be an issue with blending options...
I've tried GL_SRC_ALPHA to GL_SRC_MINUS_ALPHA (set by the normal button in ParticleDesigner), additive combinations, and trying different things with GL_ZERO and GL_ONE
Why are the particles appearing grey? Does cocos2d support using full color images as particles?
Figured it out!
Turns out the image didn't embed properly in the plist (bug in ParticleDesigner) so that's why it was appearing grey.
Exporting with the png separate solved the issue.
And yes, you can definitely use color images as particles in cocos2d!
I have my own class that holds a sprite.
The sprite is an animation made with Zwoptex. I got both retina and standard images alright.
I put my class in middle of the scene. And, for some reason, the sprite displays with a really small size.
I thought it might be because of scaling (although I never scale the sprite). So I decided to put two NSLogs:
NSLog(#"%f",enemy2.scale);
NSLog(#"%f",enemy2.sprite.scale);
One tells me the scale of my custom class itself and the other the scale of the sprite itself.
However, when I put those two lines of code, the sprite appears with the expected size (bigger).
And the NSLog result is 1.0.
Why? Any ideas?
Unless the scale getter method has been implemented and changes the scale_ instance variable this should not happen.
Initially I would say that the sprites being small size seems to indicate that you are loading the SD images on a Retina device, instead of the HD images. Be sure to name your SD and HD assets like this if you're using a texture atlas:
// SD images
textureatlas.png
textureatlas.plist
--> player.png
--> enemy.png
// HD images
textureatlas-hd.png
textureatlas-hd.plist
--> player.png // no -HD suffix!
--> enemy.png // no -HD suffix!
It is a common mistake to also suffix the sprite frames inside the texture atlas. So if your sprite frames in the HD texture atlas are named player-hd.png and enemy-hd.png then Cocos2D will not find them and reverts to loading the SD images.
It should be noted that TexturePacker will detect and can automatically correct this for you. Last time I used Zwoptex, it would allow you to create such incorrect texture atlases.
I am using cocos2d for a game which uses sprite sheets for my character animations. I created these images using TexturePacker. Now, I want to use PVRTC 4 format for reducing memory consumption due to some reasons. But as the PVRTC Texture Compression
Usage Guide suggests, I need to add extra border of 4 pixels in each character to produce proper results. Even if I add border, I will have to mask this image with alpha image to remove border at run time. I am using Texture Packer to create a sprite sheet with PVRTC4 format and created alpha masking image matching it. I am ready with these 2 images in hand which are of same width and height.
Now my question is, how can I mask my PVRTC texture with alpha image in Cocos2D?
It will be more helpful if the solution provided works with Batch Nodes!
Thanks in advance for any solutions!
Why don't you just make the border/padding area completely transparent?
I was having the same problem, and after reading ray wenderlichs page about masking, I made a little ccsprite subclass which allows you to mask by 2 images.
CCMaskedSprite