I'm using cocos2d v0.99.5-beta2 and I have a strange problem only on iPhone 1st gen and iPhone 3g.
When I change iPhone's orientation all my CCSprite are deformed and duplicated.
I already have tested many methods to rotate my scene but I always have the same result.
But I haven't this problem on simulator, iPhone 4 and iPad.
See screenshots
I wonder if it's not a problem linked to the hardware. It looks like as the graphic hardware doesn't support orientation changes. But it's only suppositions.
I hope someone can help me.
Not sure if this will make a difference but are you specifying the content size of the texture or are you letting cocos2d define it? Here I am using a subclass of CCSprite (spuButton):
CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:#"RedButtonNormal.png"]; //I don't specify the content size.
spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];
redButton.position = ccp(((size.width / 2) - (redButton.contentSize.width / 2)), ((size.height / 2) + (redButton.contentSize.height / 2)));
[self addChild:redButton];
Hope this helps 8)
Related
I created a rounded rectangle on illustrator and export in to png , then added this image to texturepacker and import to cocos2d here is my rectangle,
Have you any idea why there are white pixels on the corner.
Here is image https://dl.dropboxusercontent.com/u/9018754/Screen%20Shot%202014-07-31%20at%2023.28.52.png
Might be that the image was written as png8 with blending towards a white background in Illustrator.
Can you also post the png you created from Illustrator?
-- update --
The image looks fine. I think we can rule out problems with exporting the image.
In your settings "Premultiply Alpha" is off. When importing the image into cocos2d, did you enabled premultipled alpha?
Both values must be matching. Otherwise you will get some strange behaviour.
If it's on in TexturePacker and off in cocos2d you get black outlines.
If it's off in TexturePacker and on in cocos2d you get white outlines.
So: Try enabling it in TexturePacker.
Yes Premultiply Alpha solved my problem
Changing ccConfig.h to
#ifndef CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
#define CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL 1
#endif
and setting premultiply alpha + exclude 1 solved my problem ,
It is written in the comment line
**
` If enabled, the texture coordinates will be calculated by using this formula:
- texCoord.left = (rect.origin.x*2+1) / (texture.wide*2);
- texCoord.right = texCoord.left + (rect.size.width*2-2)/(texture.wide*2);
The same for bottom and top.
This formula prevents artifacts by using 99% of the texture.
The "correct" way to prevent artifacts is by using the spritesheet-artifact-fixer.py or a similar tool.
Affected nodes:
- CCSprite / CCSpriteBatchNode and subclasses: CCLabelBMFont, CCTMXLayer
- CCLabelAtlas
- CCParticleSystemQuad
- CCTileMap
To enabled set it to 1. Disabled by default.
**
I couldnt figure out what is going on at the moment but I will check code deeper ,
Thank you Andreas Löw
Thanks for reading.
I'm working on a setup in Cocos2D 1.x where I have a huge CCLayerPanZoom in a scene with free panning and zooming.
Every frame, I have to additionally draw a CCRenderTexture on top to create "darkness" (I'm cutting out the light). That works well.
Now I've added single sprites to the surface, and they are managed by Box2D. That works as well. I can translate to the RenderTexture where the light sources ought to be, and they render fine.
And then I wanted to add a HUD layer on top, by adding a CCLayer to the scene. That layer needs to contain several sprites stacked on top of each other, as user interface elements.
Only, all of these elements fail to draw where I need them to be: Exactly in the center of screen. The Sprites added onto the HUD layer are all off, and I have iterated through pretty much every variation "convertToWorldSpace", "convertToNodeSpace", etc.
It is as if the constant scaling by the CCPanZoomLayer in the background throws off anchor points in the layer above each frame, and resetting them doesn't help. They all seem to default into one of the corners of the node bounding box they are attached to, as if their transform is blocked or set to zero when it comes to the drawing.
Has anyone run into this problem? Is this a known issue when using CCLayerPanZoom and drawing a custom CCRenderTexture on top each frame?
Ha! I found the culprit! There's a bug in Cocos2D' way of using Zwoptex data. (I'm using Cocos2D v 1.0.1).
It seems that when loading in Zwoptex v3 data, sprite frames' trim offset data is ignored when the actual sprite frame anchor point is computed. The effect is that no anchor point on a sprite with trim offset in its definition (eg in the plist) has its anchor point correctly set. Really strange... I wonder whether this has occurred to anybody else? It's a glaring issue.
Here's how to reproduce:
Create any data for a sprite frame in zwoptex v3 format (the one that uses the trim data). Make sure you actually have a trimmed sprite, i.e. offset must be larger than zero, and image size must be larger than source.
Load in sprite, and try to position it at center of screen. You'll see it's off. Here's how to compute your anchor point correctly:
CCSprite *floor = [CCSprite spriteWithSpriteFrameName:#"Menu_OmeFloor.png"]; //create a sprite
CCSpriteFrame *frame=[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"Menu_OmeFloor.png"]; //get its frame to access frame data
[floor setTextureRectInPixels:frame.rect rotated:frame.rotated untrimmedSize:frame.originalSizeInPixels]; //re-set its texture rect
//Ensure that the coordinates are right: Texture frame offset is not counted in when determining normal anchor point:
xa = 0.5 + (frame.offsetInPixels.x / frame.originalSizeInPixels.width);
ya = 0.5 + (frame.offsetInPixels.y / frame.originalSizeInPixels.height);
[floor setAnchorPoint:ccp(xa,ya)];
floor.position=(where you need it);
Replace the 0.5 in the xa/ya formula with your required anchor point values.
I'm really going crazy trying to figure this out, so any help would be really appreciated. I'm trying to hide most of a sprite and show it gradually. This works fine if I only work with rectangles. For example, I found someone's implementation of the "ClippingNode" class and it worked well, namely, this part of the code:
-(void) visit
{
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(clippingRegion.origin.x + positionInPixels_.x, clippingRegion.origin.y + positionInPixels_.y, clippingRegion.size.width, clippingRegion.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
The problem is I need an irregular shape, not just a rectangle. I was hoping I could stack calls to glScissor and create a shape with many smaller rectangles, but unfortunately glScissor only works once (the last time it was called).
It seems that cocos2d doesn't support OpenGLs stencil buffer (does it?) and even if it did, I find OpenGL so hard to understand, I'd still need someone to explain it to me. If I could set a bezier path on the sprite as a mask (which I think you can do in Quartz), that would be great, but it doesn't seem like that's supported.
Please, if anyone has any bit of wisdom here, that'd be great!
Figured it out. You can call glScissor multiple times, you just also need to draw that scissored shape each time:
-(void) visit
{
NSEnumerator *enumerator;
NSValue *val;
CGRect aRegion;
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
enumerator = [regions objectEnumerator];
while ((val = (NSValue *)[enumerator nextObject])) {
aRegion = [val CGRectValue];
glScissor(aRegion.origin.x, aRegion.origin.y,
aRegion.size.width, aRegion.size.height);
[super visit];
}
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
It isn't possible with glScissor, but you could easily achieve this effect using the stencil buffer. Here is the documentation:
http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node117.html
There is also a NeHe tutorial on the stencil buffer, but it is in C++, not Objective C (though it should be easy to translate into whatever application you need):
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=26
EDIT: This is based on the assumption that you want to clip it to some arbitrary shape, for example a star, smiley whatever, instead of just a rectangle.
I am using enableRetinaDisplay in my project and it is working very well except when I use this code.
//+++VRope
//create batchnode to render rope segments
CCSpriteBatchNode *ropeSegmentSprite = [CCSpriteBatchNode batchNodeWithFile:#"rope.png" ];
[game addChild:ropeSegmentSprite];
//Create two cgpoints for start and end point of rope
CGPoint pointA = ccp(73, 330); //Top
CGPoint pointB = ccp(self.position.x +5, self.position.y +30); //Bottom
//create vrope using initWithPoints method
verletRope = [[VRope alloc] initWithPoints:pointA pointB:pointB spriteSheet:ropeSegmentSprite];
Instead of drawing one high-res image of the rope, this code is drawing two rope images. I know that it is the retina display that is causing this because I tested it on an iphone 3gs and the simulator and it works great... until I test it on my iphone 4 then it draws two ropes instead of one. Am I doing something wrong?
I know it's too late but I found this question on the first page when searching google so I'll post this answer for others to find in the future.
In VRope.mm search for
[[[spriteSheet textureAtlas] texture] pixelsHigh]
and replace with
[[[spriteSheet textureAtlas] texture] pixelsHigh]/CC_CONTENT_SCALE_FACTOR()
That's it.
Hi i want to develop game like 'Doodle jump'.But i have some problem with the following features-
1.How to move background scene/image.
2.How to detect collision between object.Is it needed a physics engine like box2d or i should just use manual collision.
3.what should be the size of the background image.
4.In fact i have no idea how does background move .So i need a explanation from someone.
Background Movement
A) You could create a TMX Tilemap and then make a very high Tiled-Map.
B) You could create one texture and then cycle the texture coords instead of really moving it.
Detect it manually. Best is detect it via "Point in Boundingbox" or "Rect in Rect".
For more detail visit my blog entry for collision detection with cocos2d : http://www.anima-entertainment.de/?p=262
Size of an Image
Keep in Mind that textures are always at power of 2 in the memory. If you want to create one Background-Image at retina highresolution (960x640 Pixel) in the memory will be a texture of 1024x1024. If possible use smaller Background-Images and stretch them. (like 512x512). But I really would recommend for big scrolling images the TMX Support.
CCTMXTiledMap * tmxNode = [CCTMXTiledMap tiledMapWithGMXFile:#"Level.tmx"];
// lets say you want to move it 50 pixels down in 1 second :
[tmxNode runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(0,-50)];
To create a tilemap : http://www.mapeditor.org/
In the folder of cocos2d, you could get many demos of tilemap.
TileMapTest.h
TileMapTest.m
refer this tutorial this will helpful for you.
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
this is used screen movement with pan recognizer