scale the background layer touch position is changed? - iphone

I am doing some r &d in cocos2d.I have one backgroundLayer and add one sprite in that layer.
In touches Moved i changed the sprite position to current touch position.If background layer is not scaling that means backgroundLayer scale is 1 ,the code is run perfectly (simply set the touch position to sprite position).if i scaling the backgroundLayer i have the problem sprite doesn't move touch position ..please any one help me.
Here is my code:
layer1=[CCLayer node];
[self addChild:layer1];
layer1.scale=2;
iconImg=[CCSprite spriteWithFile:#"Icon-72.png"];
iconImg.position=ccp(512,384);
[layer1 addChild:iconImg];
In Touches Moved
iconImg.position=tchLocation;
My problem is after scaling the backgroundLayer how to find the touch position according to the background layer position..any one guide me...

How do you calculate your tchLocation?
It should work with:
CGPoint *tchLocation = [layer1 convertTouchToNodeSpace:touch];
Where touch is your UITouch.

Related

Why do we have to set the rotation/scale of gesture recognizer back to 0/1?

I had a problem regarding rotation of an image in my code using rotation gesture. After spending some time on SO I got a link to Ray Wenderlich's tutorial on UIGestureRecognizers.
Initially my view was rotating and scaling very fast on corresponding gestures and this link has a nice tutorial on using them properly. However I could not understand why did setting rotation and scale to there normal values 0 and 1 respectively solved the problem.
Here's the link to the tutorial
http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
UIPinchGestureRecognizer and UIRotationGestureRecognizer is the section I am referring to.
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
The code first create a new transform from the recognizer scale and assign it to your view.
After that it reset the scale to 1.
This is actually consider the scaling to the view each time start from 1.
Similar to the rotation, we consider the rotation degree start from 0 every time it called.

How to add transition effect in Current running scene in cocos2d iphone

How to add transition effect in Current running scene in cocos2d iphone. Means I am making a game and after each goal I want to give a fade effect or any type of effect on the current running scene.
If I write this, It replace current scene to its new scene. But I don't want to replace scene.
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0f scene:[GamePage scene]]];
Is there some way to show effect on the current page like this. I know that it is wrong but I want something like this :
[self transitionEffect:[CCTransitionFade actionWithDuration:0.5]];
For Scene,Layer (Subclass of CCNode) action related to Opacity will not work. !
You can Either Use transition, or Must have to apply CCFadeTo to all of your sprite.
But If you are choosing to CCFadeTo to all sprites, this will require allocation of lots of actions suddenly ! FPS slow down !!
Another best approach:
Tell to your designer, to make 1 x 1 pixel Square black dot image.
Add this code at last in the init method.
CCSprite *temp=[CCSprite spriteWithFile:#"squaredotBlack.png"];
temp.position=ccp(s.w/2,s.h/2);
[self addChild:temp z:50000]; //set as most top layer
temp.scaleX=s.w;
temp.scaleY=s.h;
temp.opacity=0;
Then Apply, For "Fade out" process of whole screen, Increase the opacity.
temp.opacity=0;
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:255]]; //0 to 255
Then Apply, For "Fade In" process of whole screen, decrease the opacity.
temp.opacity=255; // this will cover whole screen with black color
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:0]]; //255 to 0
you can run an action on the entire CCLayer
[self runAction:[CCFadeOut actionWithDuration:0.5f]];
or you can use CCFadeTo to fade to a desired opacity.

Cocos2d-iPhone Drag anywhere to move sprite

Ok what I would like to do is duplicate the controls of Space Invaders Evolution.
Begin a touch anywhere then drag around and the sprite moves 1:1 with your finger.
Sprite can not be moved of screen.
I have been using UIGestureRecognizer to handle dragging the sprite around the screen but im still pretty new to this and I have not been able to get this to work yet.
I don't know if you're unwilling to try this but it seems to me that this sort of thing could easily be done using ccTouchesBegan and ccTouchesMoved. I'd implement it by putting the following in my ccTouchesBegan and ccTouchesMoved methods (where sprite is the name of the sprite you're trying to move):
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
sprite.position = ccp(location.x, location.y);
Are you using Cocos2D or the Apple Frame-work?
In cocos2d you could use a layer (your scene for example) to detect the touch, which then moves the sprite. When the move is applied, check whether the sprite is still within screen boundaries, and move it back into screen boundaries if it's not.
There is example touch code in the cocos2d distribution. Not for your specific case, but general code, with text and explanation.

Showing a frame when collision is detected (cocos2d iPhone)

I have a hero character all set up with a CCSpriteBatchNode, which has all the animation and frames. But I wonder, how do I display a frame when the hero is hit (I set up collision detection already). How do I make this happen? Do I put it inside the CCSpriteBatchNode? It's not part of moving, only for special occasions.
You can set your hero invisible ( [sprite setVisible: NO]) and show another sprite at the hero's position.
More then it, you can keep this sprite in your hero class object and provide a method which will change the visible sprite when collision happens. Something like this
-(void) onCollision
{
[heroMainSprite setVisible:NO];
[heroCollisionSprite setVisible:YES];
}
Such solution will also preserve all your logic, that has been done before.
When hero gets hit.. Change the texture..
[spr setTextureRect:CGRectMake(startX, startY, width, height)];
It needs to be in the same batch node as the rest of the animation..

How to center align a sprite?

In cocos2d does anyone know how to center a sprite? Right now, I have a sprite that moves to where you touch on the screen. The problem is that the sprite is aligned to the lower left corner. This means that instead of moving down, if you touch just a little over the bottom the sprite will move up. Thanks in advance!
Here is my code...
(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];
return YES;
}
The default transformation anchor for sprites in Cocos2D is the center of the sprite, so it should be moving such that the center of the sprite ends up in the touched location as you have it now. Have you changed the sprite's transform anchor?
The only other thing I can think of is that if your mainSprite is a child of another CocosNode then you may need to convert the touch coordinates to node space using this method:
- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;
...on the parent node. However, I doubt that is the problem. Sorry if this is unhelpful.
EDIT: OP, if you read this, what version of Cocos2D are you using? I believe 0.8 (currently in the svn trunk) changes the way that anchoring works; for future reference it may be useful to others to know what you're working with.
I got it working! For anyone that wants to know here is the code...
[mainSprite setTransformAnchor:ccp(24.0, 64.5)];
24 is half of the sprites width
64.5 is half of the sprites height