简体   繁体   中英

How can I get touch co-ordinates in cocos2d?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM