Changing texture: unexpected scale results - cocos2d-iphone

I can't get my head around what's going on here and I know I'm being stupid, but here's whats going on:
I have two images, one is 1024x768, one is 2048x1536. They are "background" textures. I'm trying to switch from one texture to the other:
CCSprite *bg = [CCSprite spriteWithFile:#"one.jpg"];
bg.scale = 0.5;
[self addChild:bg];
...
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:two.jpg];
[bg setTexture: tex];
OK this is totally normal, the 2nd image fits perfectly into the same exact space and dimensions of the first, despite being a different resolution. Now i'm adding a 2nd image so i can have the first one fade into the second one
CCSprite *newBG = [CCSprite spriteWithFile:#"two.jpg"];
newBG.scale = bg.scale;
[self addChild: newBG];
This however doesn't work, the image is twice as big as it should be.
So what exactly is happening here? As best I can guess when a sprite is created from an image, the size of the sprite is set, and then any texture applied automatically scales to fit the space, and the 2nd doesn't work because the newly created sprite is using a different sized image?

When you set a texture, you should also call
-(void) setTextureRect:(CGRect) rect;
to set its size. This is automatically called in initWithTexture, but not when you simply call setTexture. So, you assumption is right: the texture size is set when you init the sprite and not changes thereafter; but you can change it when you want to use a new texture.

Related

Scale or resize a sprite with image

The idea is: I have a sprite with rectangle image
CCSprite *sprite = [CCSprite spriteWithFile:#"Rectangle.png"];
When I touch the sprite, 8 red points will appear
Holding a point and drag it to scale(resize) the image like this
Can anyone show me how to do or give me a sample code.
I am not sure about this but you can redraw a texture on that 8 points.
It seems similar to draw texture of soft body. I have implemented that for my game and in that i use 12 points and on that 12 point i tried to draw the texture.
So what you can do is you have to redraw the texture on that points when any of the point dragged. This is a tutorial for soft body,but You can refer this tutorial for reference.
http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/
It's not the same thing what you are looking for but yes using this you can implement.

CCRenderTexture size and position in Cocos2d-iphone

I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:
http://www.cocos2d-iphone.org/forum/topic/18522/page/2
The code "as is" works, and I have integrated it with my project
But I am having trouble doing some of the other things discussed:
If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.
Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:
http://www.cocos2d-iphone.org/forum/topic/18796
But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.
Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:
CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
_rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
[[RIGameScene sharedGameScene]addChild:_rt];
_rt.visible = YES;
I use cocos2d-iphone 1.0.1
You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.
The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.
It seems that you cannot change the position of _rt.sprite.
The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.
When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.
Remember to move the sprites back after you're done. :)

How to Add Image/Sprite on Texture?

I am drawing background Sprite
1) CCRenderTexture...draw texture using it
2)The taking sprite from this
So i need to add some images or sprite(from images) on to the texture..
so is there any API by which we can add to texture and then getting sprite (texture + images)..
or i have to draw on texture using open gl commands???
Thanks in advance
CCRenderTexture is a CCNode. As with every CCNode you can just add sprites as children and position them wherever you want. For animation to them you can use CCAnim just as with any other sprite.

How to overlap sprites in cocos2d

How will I show 2 sprites on same layer. Presently sprites with horizontal image is coming behind the other sprites. Horizontal image sprites drawing in CCRibbon class.
Well, you could assign them a z order. This is done by using code like this:
[self addChild: MySprite z:1]
The higher the z order, the highest up the sprite will be.

How to clear texture on view

I use Framework Cocos2D to make my little game.
I used 3 object:
CCTMXTiledMap at background at layer -1
CCRenderTexture to render some line with texture at layer 0
CCSpriteBatchNode to render game object at layer 1
I use CCRenderTexture at z-oder 0. When i try to render some Texture on this, its working perfect, but when use Clear function to clear the Texture in viewport to redraw another texture, it's clear Map too.
[target clear:0.0f g:0.0f b:0.0f a:1.0f];
How can i so this problem?
Thanks you ! –
//For removing all textures
[[CCTextureCache sharedTextureCache] removeAllTextures];
//For removing un used textures
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
I had this problem, but my solution was to just use [target clear:0.0f g:0.0f b:0.0f a:0.0];.... see the difference there. The alpha is zero. We're clearing the screen with the colour black though we are putting the alpha to zero. That will DEFINATELY work if I think you are trying to sort out what I think you are.