- 本帖最后由 jetion 于 2012-10-2 19:24 编辑 *
我遇到的情况是这样的,有两个Layer,一个是GestureLayer,也就是识别手势的层;还有一个是PlayLayer,就是游戏层。在我的GestureLayer接收到相关的手势事件时,我想让PlayLayer上实现某些动画等。
现在我的问题是:PlayLayer怎么知道GestureLayer触发手势事件了。
目前我的想法:1.就是设置标志位,当GestureLayer接收到手势事件时,给标志位true;然后在PlayLayer中不断检测该标志位,若检测到true,就执行相应的动画,然后马上把该标志位false。
2.就是设置一个监听,写一个接口,假设为 GestureLayerDelegate ,里面有相关手势触发后执行的函数,然后让PlayLayer继承它,并复写相关函数,在GestureLayer中定义一个 GestureLayerDelegate * delegate;的对象,在GestureLayer中触发手势事件时,用delegate来调用PlayLayer中的重写的接口函数。我遇到的问题就是:手势触发时,delegate调用PlayLayer中重写的接口函数是没问题的,我用CCLog打印了,是有执行的,但是我在接口函数中写的一句sprite->runAction()并没有执行,sprite是我在PlayLayer中添加的精灵,这是什么回事呢?
class GestureLayerDelegate
{
public:
virtual void onGestureRun()=0;
virtual void onGestureBack()=0;
virtual void onGestureUpOne()=0;
virtual void onGestureUpTwo()=0;
virtual void onGestureDown()=0;
};
#endif
class PlayLayer :public CCLayer, public GestureLayerDelegate
{
private:
NormalPerson* nPerson;
public:
PlayLayer(void);
~PlayLayer(void);
virtual bool init();
void update();
virtual void onGestureRun();
virtual void onGestureBack();
virtual void onGestureUpOne();
virtual void onGestureUpTwo();
virtual void onGestureDown();
};
class GestureLayer : public CCLayer
{
private:
GestureLayerDelegate* delegate;
}
在touch事件中执行
delegate->onGestureUpOne();
如果可以的话,帮忙讲解一下监听和委托的原理,万分感谢!!