skippy_winks
skippy_winks

Reputation: 778

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 will 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!

Upvotes: 0

Views: 210

Answers (3)

Minakshi
Minakshi

Reputation: 1433

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.

Upvotes: 0

Tom Harrington
Tom Harrington

Reputation: 70946

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.

Upvotes: 1

bbum
bbum

Reputation: 162712

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.

Upvotes: 3

Related Questions