How can I get touch co-ordinates in cocos2d? - iphone

What I want is that wherever user touches on iPhone screen, where I will display a pic at that place.
Can anyone help me how to get co-ordinates of the area everytime when user touches the screen?

You don't say you are using cocos2d, but you tagged your question as such. I will assume this is the case.
You need to put code in your ccTouchesEnded function. This is basically what it looks like:
-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CCSprite *myImage = [CCSprite spriteWihFile: #"myImage.png"];
[myImage setPosition: location];
[self addChild: myImage];
return YES;
}
It sounds like you might need more help than that. If so, you should go to the cocos2d iphone page and look through the setup and tutorials.

Related

How to make two specific images, when a user places one on top of the other, create a third specified image (all of them pre-created images)

I would like the user to be able to press and drag one image (ex: image1.png) and place it over another (ex: image2.png). When the user releases, a third image (ex: image3.png) is added to the screen. How could I go about doing that in xcode?
It's not so easy to do what you want, you have to start to see this touches tutorial from Apple Developer program
here is a code sample for the image moving:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (CGRectContainsPoint([image1 frame], location)) {
images.center = location;
if (CGRectContainsPoint([image1 frame], [image2 frame]) {
//place your code when image1 = images2
[self.view addSubview:image3];
}
}
}
i think something like this could work

Animate object along a drawn path?

I recently downloaded the sample application, GLPaint from apple. This app showed me how to implement drawing, but now I would like to animate a image over the drawn lines. Similarly to how the application "Flight Control" works, I would like to be able to draw a path for an image and then have the image animate over this path.
any ideas?
you could create your path
CGMutablePathRef thePath = CGPathCreateMutable();
and then in the touchesBegan and touchesMoved add to your path
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathMoveToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathAddLineToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
and on touchedEnded, as Joshua said, create a CAKeyframeAnimation and set its path to thePath and set the other properties of your animation (duration, etc) and apply it to your object
Look at CAKeyframeAnimation, this allows you to set a path (which is a CGPathRef) for the animation to run along.
Based on code from AtomRiot, I created a simple project on my blog. See this post, also posted a short video showing the result.
However when the animation ends, the image will somehow repositioned back to (0,0), so might need to add extra code to reset the image location after animation finished.
Plus, found that you can't change the animation once it started, have to wait for it to end before you can change it.
Hope this helps!

move sprite by using touchmoved in specified regions of screen

I am making an application, in that I want to move the sprite in a specified region of the screen, With cocos2d I am not able to make move of sprite, I only know the method -(void)ccTouchMoved:(UITouch *)touches withEvent:(UIEvent *)event, but I don't know how to move a sprite,
can any one help me????
Thanks in advance
Try something like the following.
-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate:
[touch locationInView:touch.view]];
[yourSprite setPosition:ccp(location.x , location.y )];
return kEventHandled;
}
Edit: If you simply want to move the sprite without a touch event simply call
[yourSprite setPosition:ccp(someX, someY)];

How i can set an image to another fixed position by touch option?

I am trying to build an iPhone app by using Cocos2d. And i would like to set an image to another fixed position from a fixed position by using touch as my wish(speedy, or slowly). I have got some code but it does not work properly.
so friends it will more helpful to me if i get any solution.
The question is a little fuzzy, but if you want to set the position of a CocosNode you do:
[myNode setPosition:cpv(x,y)];
If you want the node to be offset from a touch location, you can do this by implementing ccTouchesBegan:withEvent
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[Director sharedDirector] convertCoordinate:location];
[myNode setPosition: cpv(convertedLocation.x - 100, convertedLocation.y - 100)];
return kEventHandled;
}
That will offset the CocosNode by -100,-100 to where the touch occurred.
The ccTouchesBegan:withEvent: should be implemented in your Layer, and isTouchesEnabled should be set to YES to enable touches.

How can I detect touch in cocos2d?

I am developing a 2d game for iPhone by using cocos2d.
I use many small sprite (image) in my game. I want to touch two similar types of sprite(image) and then both sprite(image) will be hidden.
How can I detect touch in a specific sprite(image) ?
A better way to do this is to actually use the bounding box on the sprite itself (which is a CGRect). In this sample code, I put all my sprites in a NSMutableArray and I simple check if the sprite touch is in the bounding box. Make sure you turn on touch detection in the init. If you notice I also accept/reject touches on the layer by returning YES(if I use the touch) or NO(if I don't)
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *station in _objectList)
{
if (CGRectContainsPoint(station.boundingBox, location))
{
DLog(#"Found sprite");
return YES;
}
}
return NO;
}
Following Jonas's instructions, and adding onto it a bit more ...
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
if(CGRectContainsPoint(particularSpriteRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
You may need to adjust the x/y a little to account for the 'centered positioning' in Cocos
In your layer that contains your sprite, you need to say:
self.isTouchEnabled = YES;
then you can use the same events that you would use in a UIView, but they're named a little differently:
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
//in your touchesEnded event, you would want to see if you touched
//down and then up inside the same place, and do your logic there.
}
#david, your code has some typos for cocos 0.7.3 and 2.2.1, specifically CGRectMake instead of CGMakeRect and [touch location] is now [touch locationInView:touch.view].
here's what I did:
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
#Genericrich: CGRectContainsPoint works in CocosLand because of the call 2 lines above:
[[Director sharedDirector] convertCoordinate:]
The Cocos2D objects will be using the OpenGL coordinate system, where 0,0 is the lower left, and UIKit coordinates (like where the touch happened) have 0,0 is upper left. convertCoordinate: is making the flip from UIKit to OpenGL for you.
Here's how it worked for me...
Where spriteSize is obviously the sprite's size... :P
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x-spriteSize/2, sprite.position.y-spriteSize/2, spriteSize, spriteSize);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
this is a good tutorial explaining the basic touch system
http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/
first, write
self.isTouchEnabled = YES;
then, you need to implement the functions ccTouchesEnded, ccTouchesBegan, etc
from what I understood, you want to be able to 'match' two sprites that can be on different coordinates on the screen.
a method for doing this.. : (im sure theres many other methods)
consider having 2 global variables.
so everytime a touch touches a sprite, you use the CGRectContainsPoint function that is mentioned several times to find which sprite has been touched. then, you can save the 'tag' of that sprite in one of the global variables.
You do the same for the second touch, and then you compare the 2 global variables.
you should be able to figure out the rest but comment if you have problems.