1. Android 平台触摸事件
Ontouch事件接受触摸事件
2.设置图层接触事件
public GameLayerTouch() {
// 设置是否接受触摸事件
this.setIsTouchEnabled(true);
}
3.触摸回调函数
// 用户触摸屏幕
@Override
public boolean ccTouchesBegan(MotionEvent event) {
Log.d(TAG, "ccTouchesBegan");
return super.ccTouchesBegan(event);
}
// 用户手指离开
@Override
public boolean ccTouchesEnded(MotionEvent event) {
Log.d(TAG, "ccTouchesEnded");
return super.ccTouchesEnded(event);
}
// 手指移动
@Override
public boolean ccTouchesMoved(MotionEvent event) {
Log.d(TAG, "ccTouchesEnded");
return super.ccTouchesMoved(event);
}
4.触摸点坐标转换
MotionEvent 的事件是屏幕的左上角为原点,但是cocos2D 是已左下角为原点
// 用户触摸屏幕
@Override
public boolean ccTouchesBegan(MotionEvent event) {
// MotionEvent 的事件是屏幕的左上角为原点,但是cocos2D 是已左下角为原点
float x = event.getX();
float y = event.getY();
CGPoint p1 = CGPoint.ccp(x, y);
CGPoint p2 = CCDirector.sharedDirector().convertToGL(p1);
Log.d(TAG, "ccTouchesBegan: x = " + x + ", y =" + y);
Log.d(TAG, "ccTouchesBegan: p1.x = " + p1.x + ", p1.y =" + p1.y);
Log.d(TAG, "ccTouchesBegan: p2.x = " + p2.x + ", p2.y =" + p2.y);
return super.ccTouchesBegan(event);
}
运行结果
04-21 16:18:18.840: D/GameLayerTouch(9633): ccTouchesBegan: x = 215.55093, y =566.2082
04-21 16:18:18.840: D/GameLayerTouch(9633): ccTouchesBegan: p1.x = 215.55093, p1.y =566.2082
04-21 16:18:18.840: D/GameLayerTouch(9633): ccTouchesBegan: p2.x = 215.55093, p2.y =204.07825
5.完整demo
GameLayerTouch
package com.su.cocos2dhelloworld;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.CGPoint;
import android.util.Log;
import android.view.MotionEvent;
public class GameLayerTouch extends CCLayer {
private static final String TAG = GameLayerTouch.class.getSimpleName();
public GameLayerTouch() {
// 设置是否接受触摸事件
this.setIsTouchEnabled(true);
}
// 用户触摸屏幕
@Override
public boolean ccTouchesBegan(MotionEvent event) {
// MotionEvent 的事件是屏幕的左上角为原点,但是cocos2D 是已左下角为原点
float x = event.getX();
float y = event.getY();
CGPoint p1 = CGPoint.ccp(x, y);
CGPoint p2 = CCDirector.sharedDirector().convertToGL(p1);
Log.d(TAG, "ccTouchesBegan: x = " + x + ", y =" + y);
Log.d(TAG, "ccTouchesBegan: p1.x = " + p1.x + ", p1.y =" + p1.y);
Log.d(TAG, "ccTouchesBegan: p2.x = " + p2.x + ", p2.y =" + p2.y);
return super.ccTouchesBegan(event);
}
// 用户手指离开
@Override
public boolean ccTouchesEnded(MotionEvent event) {
Log.d(TAG, "ccTouchesEnded");
return super.ccTouchesEnded(event);
}
// 手指移动
@Override
public boolean ccTouchesMoved(MotionEvent event) {
Log.d(TAG, "ccTouchesEnded");
return super.ccTouchesMoved(event);
}
}
MainActivity
package com.su.cocos2dhelloworld;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
// 生成一个SurfaceView对象
private CCDirector mCCDirector;
private CCScene mCCScene;
private CCGLSurfaceView mCCGLSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initValue();
initListener();
}
private void initView() {
mCCGLSurfaceView = new CCGLSurfaceView(this);
setContentView(mCCGLSurfaceView);
}
private void initValue() {
// 得到单例模式的CCDirector对象,并通过该对象设置应用程序的各种属性
mCCDirector = CCDirector.sharedDirector();
// view 和 引擎建立连接
mCCDirector.attachInView(mCCGLSurfaceView);
// 设置显示帧率,查看是否流畅
mCCDirector.setDisplayFPS(true);
// 设置每幁所需的时间,1秒钟30次
mCCDirector.setAnimationInterval(1 / 30.0);
// 生成场景对象
mCCScene = CCScene.node();
// 生成布景层对象
//GameLayer mGameLayer = new GameLayer();
GameLayerTouch mGameLayerTouch = new GameLayerTouch();
mCCScene.addChild(mGameLayerTouch);
// 运行游戏场景
mCCDirector.runWithScene(mCCScene);
}
private void initListener() {
// TODO Auto-generated method stub
}
}