开发者

Cocos2D Touch HELP

开发者 https://www.devze.com 2023-03-15 02:10 出处:网络
I`m new to cocos2d library, I worked before wit开发者_如何学Goh libgdx and pure openGL. How can I handle a touch event in Cocos2d for Android?The 4 methods for handling touches on android are defined

I`m new to cocos2d library, I worked before wit开发者_如何学Goh libgdx and pure openGL. How can I handle a touch event in Cocos2d for Android?


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); 


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) {
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消