为屏幕添加触摸事件,将左上角坐标转换为左下角坐标
package com.arlen.cocos2d.touch01; import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.CGPoint; import android.view.MotionEvent; public class GameLayer extends CCLayer { // 接受用户的触摸事件,首先必须对当前图层进行设置 public GameLayer() {
this.setIsTouchEnabled(true);
} // 当用户开始触摸事执行该方法
@Override
public boolean ccTouchesBegan(MotionEvent event) { // 以屏幕的左上角为原点
float x = event.getX();
float y = event.getY(); CGPoint point1 = CGPoint.ccp(x, y);
// 将左上角坐标转换为左下角坐标
CGPoint point2 = CCDirector.sharedDirector().convertToGL(point1);
System.out.println("ccTouchesBegan:");
System.out.println("左上角坐标:" + point1.x + "," + point1.y);
System.out.println("左下角坐标" + point2.x + "," + point2.y); return super.ccTouchesBegan(event);
} // 当用户手指离开屏幕是执行此方法
@Override
public boolean ccTouchesEnded(MotionEvent event) {
System.out.println("ccTouchesEnded");
return super.ccTouchesEnded(event);
} // 当用户手指在屏幕上移动的时候执行此方法
@Override
public boolean ccTouchesMoved(MotionEvent event) {
System.out.println("ccTouchesMoved");
return super.ccTouchesMoved(event);
}
}