Thank GOD its working now.
By "saving" bg I just meant make it a member variable, "global" access.
I was already using bg as global.
@mobilebros you are right using scale its working, I was on a mistake. Thanks for all guys who helped me to do it. I would like to share the code to do this all. so that any one can get help from it.
i used self as a CCLayer
in my init I used
-(id) init
{
if((self=[super init]))
{
bg = [CCSprite spriteWithFile:@"GamePlayBG.png"];
[bg setPosition:ccp(0, 0)];
[self addChild:bg z:0 tag:kBackground];
Player1 = [CCSprite spriteWithFile:@"Player.png"];
[Player1 setPosition:ccp(160, 260)];
[bg addChild:Player1 z:1 tag:10];
CGPoint layerStartPos = ccp(30, 140); // I am setting layer position to get the complete background in view while zoomed out
self.position = layerStartPos;
ZoomFactor = 0.45;
self.scale = ZoomFactor;
}
}
My zoom in/out and while zoomed in to move the background .. code is
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if([touches count] == 2)
{
NSArray *touchesArray = [touches allObjects];
UITouch *touch1 = [touchesArray objectAtIndex:0];
CGPoint TouchLoc1 = [touch1 locationInView: [touch1 view]];
TouchLoc1 = [[CCDirector sharedDirector] convertToGL:TouchLoc1];
UITouch *touch2 = [touchesArray objectAtIndex:1];
CGPoint TouchLoc2 = [touch2 locationInView: [touch2 view]];
TouchLoc2 = [[CCDirector sharedDirector] convertToGL:TouchLoc2];
CurrDistance = ccpDistance(TouchLoc1, TouchLoc2);
if(CurrDistance > PrevDistance)
[self ZoomIn];
else
[self ZoomOut];
PrevDistance = CurrDistance;
}
else if([touches count] == 1)
{
CGPoint touchLoc;
UITouch *touch = [touches anyObject];
touchLoc = [touch locationInView:[touch view]];
touchLoc = [[CCDirector sharedDirector] convertToGL:touchLoc];
CGPoint tempPoint = self.position;
if(touchLoc.x > LastX)
tempPoint.x += 1;
else
tempPoint.x -= 1;
if(touchLoc.y > LastY)
tempPoint.y += 1;
else
tempPoint.y -= 1;
self.position = tempPoint;
LastX = touchLoc.x;
LastY = touchLoc.y;
}
}
-(void)ZoomIn
{
ZoomFactor += 0.01;
if(ZoomFactor > 1.0)
ZoomFactor = 1.0;
self.scale = ZoomFactor;
}
-(void)ZoomOut
{
ZoomFactor -= 0.01;
if(ZoomFactor < 0.5)
ZoomFactor = 0.5;
self.scale = ZoomFactor;
}
for detecting sprite on touch ended function is
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint locationGL;
UITouch *touch = [touches anyObject];
locationGL = [touch locationInView: [touch view]];
locationGL = [[CCDirector sharedDirector] convertToGL:locationGL];
CGPoint tapPosition = [bg convertToNodeSpace:locationGL];
CCSprite * obj = (CCSprite *)[bg getChildByTag:10];
CGRect SpriteRectangle = [self rectForSprite:obj];
if (CGRectContainsPoint(SpriteRectangle, tapPosition))
{
printf("sprite touched\n");
}
else
{
printf("not touched\n");
}
}
-(CGRect)rectForSprite:(CCSprite *)sprite
{
float h = [sprite contentSize].height;
float w = [sprite contentSize].width;
float x = sprite.position.x - w/2;
float y = sprite.position.y - h/2;
CGRect rect = CGRectMake(x,y,w,h);
return rect;
}
If there is any problem to run the code I can help,
Only you need to restrict the black background not to view.
if some one can advice me for that?