how to visible the portion sprite - iphone

I want to visible portion of sprite while on touching screen.
when i touch on the spriteModelHair touch location should be visibel and rest of the part of the sprite should be not visible.]
spriteModelHair = [CCSprite spriteWithFile:#"hair001.png" rect:CGRectMake(0, 0, 450, 612)];
spriteModelHair.position = ccp(winSize.width/2, winSize.height/2+25);
spriteModelHair.visibel = No;
[self addChild:spriteModelHair z:2];

Get the RGBA data of each pixel and each pixel's position on the screen. If the location of your touch is the same as one of the pixel's locations (or in a predefined area around the touch) change that pixel's alpha to 0 or something.

Related

Move Background CCSprite repeatedly like a moving road

I'm new to cocos2D , I have a image with 640 X 1136 pixel and I set it as background sprite statically. Now I want to move this sprite, continuously like moving background in upward direction. Set the code below in init method. Here my code :
background = [CCSprite spriteWithFile:#"bgImagevertical.png"];
background.scaleX = 1;
background.scaleY = 1;
NSLog(#"size X :: %f && Y :: %f",size.width,size.height);
background.position = ccp(size.width/2,size.height/2);
[self addChild:background];
Lot of Parallax scrolling tutorials are available on internet. Try searching with the right term. Here is the one for you: http://www.learn-cocos2d.com/2012/12/ways-scrolling-cocos2d-explained/

Cocos2d, Weird position offset when using CCParallaxNode

I'm using cocos2d for iOS. Not sure if I'm doing something wrong here or if CCParallaxNode does something strange that I just don't understand.
I'm trying to create a few layers of parallax scrolling in the background of my game, at the moment I just have one layer added to the parallax node till I can figure out what's going wrong. When the game loads the layer it's always offset down and left by about 30% of the image size. I've uploaded an image demonstrating the difference in position here http://oi42.tinypic.com/29dz1av.jpg.
Here is my code:
background = [CCParallaxNode node];
background.anchorPoint = ccp(0,0);
background.position = ccp(0,0);
[self addChild: background];
background_image = [CCSprite spriteWithFile: #"layer01.png"
rect: CGRectMake(0, 0, 100, 100)];
background_image.anchorPoint = ccp(0,0);
background_image.position = ccp(0,0);
[background addChild: background_image z:0 parallaxRatio: ccp(0,0) positionOffset:ccp(0,0)];
The cocos2d icon is attached to the same layer as the parallax node and it's position and anchor point are set to 0,0 so the bottom left of the icon should be in the same location as the bottom left of the blue background image.
I have tested this using a basic sprite in place of the parallax node and everything lines up as it should so it's not the image itself.
Any help with this would be appreciated.
First, I don't suggest you to set parallax node & its children's anchor point & position directly because it will cause some unexpected results. You should add the parallax node to a layer as its child, and set the layer's anchor point & position to place your parallax.
Second, CCParallaxNode also provides a positionOffset that you can place your picture in parallax at first. This can help if you want to align several layers of pictures' view center.
So I think you should do something like this(Since I'm using cocos2d-x, I'm not sure its right with obj-C):
layer = [[CCLayer alloc]init];
background = [CCParallaxNode node];
[layer addChild: background];
[self addChild: layer];
Then you just have to add the picture sprite in background, and set layer's position & anchor point.
Hope it can help.
Make sure the background image size is 480x320.
Why did you write CGRectMake(0, 0, 100, 100) ? Why 100 and 100 ? You're limiting your 'CGRect', that's probably wrong..

Need help in understanding the positions of sprite in cocos2d?

I am new in cocos2d i have created a simple example
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *player = [CCSprite spriteWithFile:#"Player.jpg"
rect:CGRectMake(0, 0, 27, 40)];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
but I am not clear about the position of sprite how to manager it
Your code will create a sprite and position it within the parent node so that its lower-left corner is at ccp(player.contentSize.width/2, winSize.height/2).
If you wonder why your sprite is not centered respect to the coordinate you provide, the answer is that it is the lower-left corner which is positioned, not the sprite center.
You can tweak this behavior by defining the anchorPoint property of the sprite, like this:
player.anchorPoint = ccp(0.5,0.5);
player.position = ...
EDIT:
You can think of the anchor point as the "center of gravity" of the sprite: the texture is centered around it, any scaling or other kind of transformation will be relative to it.
If the anchor point is set at (0,0), then it coincides with the lower-left corner (default); if it is (0.5, 0.5) then it is exactly in the middle of the sprite (50% width, 50% height). Its coordinates are not point, but the relative displacement within the sprite; the coordinates can go from 0.0 to 1.0.

Cocos2D CCNode position in absolute screen coordinates

I've been looking around for a while, and I have not been able to find an answer to this for some reason. It seems simple enough, but maybe I just can't find the right function in the library.
I have a scene with a layer that contains a bunch of CCNodes with each one CCSprite in them.
During the application, I move around the position of the main layer, so that I "pan" around a camera in a way. (i.e. I translate the entire layer so that the viewport changes).
Now I want to determine the absolute position of a CCNode in screen coordinates. The position property return the position relative to the parent node, but I really would like this transformed to its actual position on screen.
Also, as an added bonus, it would be awesome if I could express this position as coordinate system where 0,0 maps to the upper left of the screen, and 1,1 maps to the lower right of the screen. (So I stay compatible with all devices)
Edit: Note that the solution should work for any hierarchy of CCNodes preferably.
Every CCNode, and descendants thereof, has a method named convertToWorldSpace:(CGPoint)p
This returns the coordinates relative to your scene.
When you have this coordinate, flip your Y-axis, as you want 0,0 to be in the top left.
CCNode * myNode = [[CCNode alloc] init];
myNode.position = CGPointMake(150.0f, 100.0f);
CCSprite * mySprite = [[CCSprite alloc] init]; // CCSprite is a descendant of CCNode
[myNode addChild: mySprite];
mySprite.position = CGPointMake(-50.0f, 50.0f);
// we now have a node with a sprite in in. On this sprite (well, maybe
// it you should attack the node to a scene) you can call convertToWorldSpace:
CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];
// to convert it to Y0 = top we should take the Y of the screen, and subtract the worldCoord Y
worldCoord = CGPointMake(worldCoord.x, ((CGSize)[[CCDirector sharedDirector] displaySizeInPixels]).height - worldCoord.y);
// This is dry coded so may contain an error here or there.
// Let me know if this doesn't work.
[mySprite release];
[myNode release];

Can i display a portion of an image in iphone application using cocos2d?

I want to display portion of image not to display the total image.At first display the full image but after a delay time display the a portion of this image. I can not understand how can i do this. Like we can display portion of text by fixing the dimension of Label.But in case of image how can i do that using sprite or other.
[someImage drawInRect:CGRectMake(0, 0, 30, 30)];
Set the texture rect on the CCSprite.
//create a sprite, e.g. myFile is 128x128 pixels
CCSprite* mySprite = [CCSprite spriteWithFile:#"myFile.png"];
//position it somewhere
[mySprite setPosition:ccp(xPos,yPos)];
//show only the first 64x64 square
[mySprite setTextureRect:CGRectMake(0, 0, 64, 64)];
//add it to the root
[self addChild:mySprite];