如何让对象对 Cocos2D 中的触摸做出反应?
问题描述
Alright, so I'm starting to learn more about Coco2D, but I'm kinda frusterated. A lot of the tutorials I have found are for outdated versions of the code, so when I look through and see how they do certain things, I can't translate it into my own program, because a lot has changed. With that being said, I am working in the latest version of Coco2d, version 0.99.
What I want to do is create a sprite on the screen (Done) and then when I touch that sprite, I can have "something" happen. For now, let's just make an alert go off. Now, I got this code working with the help of a friend. Here is the header file:
And here is the implementation file:
However, this only works for 1 object, the sprite which I create the CGRect for. I can't do it for 2 sprites, which I was testing. So my question is this: How can I have all sprites on the screen react to the same event when touched?
For my program, the same event needs to be run for all objects of the same type, so that should make it a tad easier. I tried making a subclass of CCNode and over write the method, but that just didn't work at all... so I'm doing something wrong. Help would be appreciated!
Going through the "Touches" project in cocos2D and seeing if I see how they did it. It looks like they made a subclass and overwrote the methods:
So now I get to figure out why mine doesn't work... hmm...
I got it. I had to add some more code to the custom class:
Header File:
Implementation:
这篇关于如何让对象对 Cocos2D 中的触摸做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!