Cocos2D Touch HELP

[亡魂溺海] 提交于 2019-12-11 10:07:11

问题


I`m new to cocos2d library, I worked before with libgdx and pure openGL. How can I handle a touch event in Cocos2d for Android?


回答1:


The 4 methods for handling touches on android are defined as follows:

public boolean ccTouchesBegan(MotionEvent event);

public boolean ccTouchesMoved(MotionEvent event);

public boolean ccTouchesEnded(MotionEvent event);

public boolean ccTouchesCancelled(MotionEvent event);

These are the listeners you should use.

And also add below line in constructor of your CCLayer class to enable touch event.

this.setIsTouchEnabled(true); 



回答2:


To start touch event you have to first to set variable

isTouchEnabled_=true;

or

setIsTouchEnabled(true);

After that touch will work

You can use methods as:-

  @Override
      public boolean ccTouchesBegan(MotionEvent event) {
}
      @Override
    public boolean ccTouchesMoved(MotionEvent event) {
}

      @Override
        public boolean ccTouchesEnded(MotionEvent event) {
}
      @Override
      public boolean ccTouchesCancelled(MotionEvent event) {
}

I have used this like as in CCColorLayer:-

protected GameLayer(ccColor4B color) {
        super(color);
        // TODO Auto-generated constructor stub
        isTouchEnabled_=true;
}

      @Override
          public boolean ccTouchesBegan(MotionEvent event) {
    }


来源:https://stackoverflow.com/questions/6473780/cocos2d-touch-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!