How to recognize when an object is touched in Cocos2d?
I am making a game in Cocos2d. It has a 'shoot' button. How can I make it so that it: 1. Recognizes that the button sprite was touched 2. Perform a method that wi开发者_JS百科ll actually load the bullet graphic onto the layer and 3. Have it moved at a steady pace across the screen (like a real bullet)
Any help is appreciated. Thanks!
Great questions, all three, and all are covered in the various Cocos2d getting started books (Yes, books!) and guides! To answer your questions specifically would require that you answer a number of questions about how you've set up Cocos2d in the first place; what kind of map you are using, whether and which physics engine, etc....
If you haven't answered those questions yet, best to start by researching Cocos2d.
Start here.
I haven't read the book(s) available, but Ray Wenderlich's Cocos2d tutorials were incredible helpful and demonstrative.
If you're subclassing CCLayer
, set the isTouchEnabled
property to YES. You then receive callbacks like -ccTouchesBegan:withEvent:
, -ccTouchesMoved:withEvent:
, etc. Your button handling code would go in those methods. See the documentation on CCLayer and related classes for more information.
Set the isTouchesEnabled to TRUE in init. And just get rectangular co-ordinates around your button sprite and then in touchesBegan just check whether the touched location is inside the rectangular button area, if it is inside it just call your respective function.
精彩评论