为屏幕添加触摸事件,将左上角坐标转换为左下角坐标
1 package com.arlen.cocos2d.touch01; 2 3 import org.cocos2d.layers.CCLayer; 4 import org.cocos2d.nodes.CCDirector; 5 import org.cocos2d.types.CGPoint; 6 7 import android.view.MotionEvent; 8 9 public class GameLayer extends CCLayer { 10 11 // 接受用户的触摸事件,首先必须对当前图层进行设置 12 13 public GameLayer() { 14 this.setIsTouchEnabled(true); 15 } 16 17 // 当用户开始触摸事执行该方法 18 @Override 19 public boolean ccTouchesBegan(MotionEvent event) { 20 21 // 以屏幕的左上角为原点 22 float x = event.getX(); 23 float y = event.getY(); 24 25 CGPoint point1 = CGPoint.ccp(x, y); 26 // 将左上角坐标转换为左下角坐标 27 CGPoint point2 = CCDirector.sharedDirector().convertToGL(point1); 28 System.out.println("ccTouchesBegan:"); 29 System.out.println("左上角坐标:" + point1.x + "," + point1.y); 30 System.out.println("左下角坐标" + point2.x + "," + point2.y); 31 32 return super.ccTouchesBegan(event); 33 } 34 35 // 当用户手指离开屏幕是执行此方法 36 @Override 37 public boolean ccTouchesEnded(MotionEvent event) { 38 System.out.println("ccTouchesEnded"); 39 return super.ccTouchesEnded(event); 40 } 41 42 // 当用户手指在屏幕上移动的时候执行此方法 43 @Override 44 public boolean ccTouchesMoved(MotionEvent event) { 45 System.out.println("ccTouchesMoved"); 46 return super.ccTouchesMoved(event); 47 } 48 }