如何在 cocos2d 中检测触摸?

How can I detect touch in cocos2d?(如何在 cocos2d 中检测触摸?)

本文介绍了如何在 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) ?

解决方案

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.
}

这篇关于如何在 cocos2d 中检测触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 cocos2d 中检测触摸?