09 游戏开发-触屏事件

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

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值