CCAnimation shownig as black rectangle on iPod 4G - iphone

I create an animation like this:
//xxx animation cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"mainmenu_xxx.plist"];
//xxx blink
CCAnimation *animXxx1Anim = [CCAnimation animationWithSpriteSequence:#"char_xxx_loops%##2x.png" frameSequence:#"02,03,04" delay:1/24.0];
[animXxx1Anim setRestoreOriginalFrame:NO];
[[CCAnimationCache sharedAnimationCache] addAnimation:animXxx1Anim name:#"xxxBlink"];
and then run it like this:
//blink animation
[animatedXxxSprite runAction:[CCAnimate actionWithAnimation:[[CCAnimationCache sharedAnimationCache] animationByName:#"xxxBlink"]]];
It shows as it should on simulator, iPhone 3GS, iPhone 4S, but it shows as a black rectangle on iPod 4G. Any idea what could be causing it?

The problem was with the texture size. It is recomended that the texture size is lower than 1024x1024 pixels.
Also, sometimes, textures are shown with a very small (1-2 px) position error if it is not in POT format.

Related

iOS - SpriteKit and image dimessions

I just started using SpriteKit and I am trying to put a background on my scene. I want to support iPhone 5,5S,6,6+. So I created background with dimensions(using image casset: #1x at 320x480 , #2x at 750x1334 and #3x 1242x2208.
Then I am creating a Sprite:
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:#"Backgroun.png"];
background.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:background];
but the background is not covering the whole screen. What I am doing wrong here?
I found a workaround:
background.size = self.frame.size;
but this is not right! I want to know what dimessions the background has to be for each device.
Thanks in advance.

Using iPhone 5 full screen for non retina game

I would like to update my non retina game to use the wide screen in the iPhone 5, is it possible to use a 568x320 sized OpenGL window?
Yes. But your graphics will not show as sharp as it could be.
I suggest you to make #2x version of all your graphics.
I have now compiled and got my game working at 568x320, all I had to do is include the Default-568h#2x file and do this in createFrameBuffer
[self setFrame:rect];
[self setBounds:rect];
[self.layer setFrame:rect];
[self.layer setBounds:rect];

How to scale a game designed for iPad to iPhone?

I made a game using cocos2d for iPad.. Now I have to scale it for iPhone. I tried using scaleX and ScaleY parameters for my GameScene. It scaled the sprites, but I have a lot of hard coded boundaries for my objects which don't get scaled. How can I do the complete scaling of my game??
you have to do something like following
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[yourview setFrame:CGRectMake(400, 208, viewrect.size.width, viewrect.size.height)];
else
[yourview setFrame:CGRectMake(190, 96, viewrect.size.width, viewrect.size.height)];
you already done for iPad, so now you need to change else portion
set your view's frame there according to device orientation.

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.. :)

iPhone or cocos2d bug in handling retina display touches?

I was trying to handle retina display touches in my cocos2d 1.01rc iPhone app and found that to do so I have to multiply the touch point by the scale factor (see code below) even if I set to retina display in my AppDelegate (see code at the end of page). I am a bit confused as I would have expected that the locationInView funciton would have retrieved the retina display touch and not the "standard" 480x640 resolution touch. My guess is that this is due to the fact that locationInView comes from the ios library and not from cocos2d and the retina display setup in the cocos2d App delegate does not get propagated till the ios level. Strange. I post the code below and the output for clarity but would appreciate if you had similar problems and if I should consider this "bug?" as a warning bell on something else that might be lost between cocos2d and ios sdk. I might just have been stupid an not found the proper documentation page.
That's the code I am running on ccTouchesEnded event:
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
for(int i=0; i<[[touches allObjects] count]; i++){
UITouch *touch = [[touches allObjects] objectAtIndex:i];
CGPoint point = [touch locationInView: [touch view]];
CCLOG(#"Before x:%f y:%f", point.x, point.y);
point = [[CCDirector sharedDirector] convertToGL: point];
float factor = CC_CONTENT_SCALE_FACTOR();
CCLOG(#"factor %f:", factor);
CCLOG(#"After x:%f y:%f", point.x , point.y );
CCLOG(#"After x:%f y:%f", point.x * factor, point.y * factor);
...
}
Output:
Before x:314.000000 y:3.000000
factor 2.000000:
After x:314.000000 y:477.000000
After x:628.000000 y:954.000000
The app is running under retina display on an iPod touch 4th generation, here are some of the initialization data:
cocos2d: cocos2d v1.0.1
cocos2d: GL_VENDOR: Imagination Technologies
cocos2d: GL_RENDERER: PowerVR SGX 535
cocos2d: GL_VERSION: OpenGL ES-CM 1.1 IMGSGX535-63.14.2
cocos2d: GL_MAX_TEXTURE_SIZE: 2048
cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH: 16
cocos2d: GL_MAX_SAMPLES: 4
cocos2d: GL supports PVRTC: YES
cocos2d: GL supports BGRA8888 textures: YES
cocos2d: GL supports NPOT textures: YES
cocos2d: OS version: 5.0.1 (0x05000100)
cocos2d: surface size: 640x960
I checked on google and stackoverflow and it did seem to me that this was a know bug one year ago, I am just not 100% sure if this has been fixed in the rc1.01 version or if I am doing something stupid in my AppDelegate:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
//added
CC_DIRECTOR_INIT();
// Init the window
// window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
// Init the View Controller
// viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
// viewController.wantsFullScreenLayout = YES;
//
// Create the EAGLView manually
// 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [director openGLView];
[glView setMultipleTouchEnabled:YES];
// attach the openglView to the director
[director setOpenGLView:glView];
// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
CCLOG(#"Retina Display Not supported");
Any ideas? Will this affect also other aspects of my code? It does seem to concern only the touch detection and for this the fix of multiplying by the scale factor seems alright, and I did seem not to have problem when I access the relative position of the sprite objects. I will do some more testing for this and keep this in mind.
Thanks!
This is by design.
You have to understand that iOS devices report touch locations in points. Both Retina and non-Retina devices have a point resolution of 480x320 points. On Retina devices one point consists of 4 pixels, whereas on all other devices a point equals a pixel on the screen.
According to the event handling guide touches on Retina devices can be reported as .5 locations (read the first Notes box), giving you full Retina touch resolution. If you don't, then maybe that resolution gets lost during the conversions (check the original UITouch location). Either way, Retina resolution for touches is way too accurate to make any use of, 480x320 is plenty for touches.
In iOS (and Cocos) touches and a lot of things (like views positions/sizes) are not handled in pixels, but points.
In non-retina screens, a point is exactly one pixel, but in retina displays, a point is two pixels wide and two pixels tall.
This means that all your touches and other coordinates will always be in the range of a standard display (320 pixels wide and 480 pixels tall in portrait). This is great because you don't have to worry about the kind of screen, and your code is cleaner.
Both iOS and Cocos manage high resolution pretty much under the hood, so images loaded will be at high resolution, but you won't have to worry about screens with more pixels in your code.
(If for some specific reason you want to use pixels instead of points, then what you are doing is right, multiply the point by the content size. The idea behind what Apple did with points is that you don't have to bother with that, though)
try
CGPoint point = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:point];
CGPoint locationInSelf = [self convertToNodeSpace:convertedLocation];
i think the appropriate scale factor is applied by cocos in convertToNodeSpace. Relying on the API will make your code survive any potential future changes (either in cocos or in apple's stuff).
As for concerns on retina, my total work time was near zero (only had to handle cases where i got creative and strayed from the above statement on 'survival', in the non HD version).
CCLOG(#"After x:%f y:%f", point.x * factor, point.y * factor);
here no need to multiply using factor.
Because in retina display 640 * 960 is pixel size and for touch handling they return point so remove factor multiplication . may be its help you.