cocos creator摇杆

摇杆属性

module.exports = {

    TouchType : cc.Enum({
        DEFAULT: 0,
        FOLLOW: 1,
    }),

    DirectionType : cc.Enum({
        FOUR: 4,
        EIGHT: 8,
        ALL: 0,
    }),

};

摇杆类

var Common = require('JoystickCommon');

cc.Class({
    extends: cc.Component,

    properties: {
        dot: {
            default: null,
            type: cc.Node,
            displayName: '摇杆节点',
        }, 
        playerNode: {
            default: null,
            type: cc.Node,
            displayName: '被操作的目标Node',
        },
        playerAnim: {
            default: null,
            type: cc.Animation,
            displayName: '被操作的目标动画',
        },
        touchType: {
            default: Common.TouchType.DEFAULT,
            type: Common.TouchType,
            displayName: '触摸类型',
        },   
        directionType: {
            default: Common.DirectionType.ALL,
            type: Common.DirectionType,
            displayName: '方向类型',

        }, 

        _angle: {
            default: null,
            displayName: '当前触摸的角度',

        },
       
        _radian: {
            default: null,
            displayName: '弧度',
        },      


        _speed: 0,          //实际速度
        speed1: 100,         //一段速度
        speed2: 200,         //二段速度
        _opacity: 0,        //透明度
    },


    onLoad: function()
    {
        // game组件下的player节点
        this._playerRigid = this.playerNode.getComponent(cc.RigidBody);
        if(this.touchType == Common.TouchType.DEFAULT){
            //对圆圈的触摸监听
            this._initTouchEvent();
        }
    },


    //对圆圈的触摸监听
    _initTouchEvent: function()
    {
        var self = this;

        self.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, self);

        self.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, self);

        // 触摸在圆圈内离开或在圆圈外离开后,摇杆归位,player速度为0
        self.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, self);
        self.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, self);
    },

    //更新移动目标
    update: function(dt)
    {
        switch (this.directionType)
        {
            case Common.DirectionType.ALL:   
                this._allDirectionsMove();
                break;
            default :
                break;
        }
    },
     //全方向移动
    _allDirectionsMove: function()
    {
        var x = Math.cos(this._angle * (Math.PI/180)) * this._speed ;//+ this._playerNode.x;
        var y = Math.sin(this._angle * (Math.PI/180)) * this._speed ;//+ this._playerNode.y;
        if (x > 0 && (!this.anim || this.anim.name !== "right")) {
            cc.log("right")
            this.anim = this.playerAnim.play("right");
        }else if(x < 0 && (!this.anim || this.anim.name !== "left")){
            cc.log("left")
            this.anim = this.playerAnim.play("left");
        }else if(this._speed === 0){
            cc.log("stop")
            this.playerAnim.stop();
            this.anim = null;
        }
        // this._playerNode.setPosition(x,y);
        // cc.log(x,y,this._speed);
        this._playerRigid.linearVelocity = cc.p(x,y);
        // this._playerRigid.applyForceToCenter(cc.p(x,y),true);
        // this._playerNode.x += Math.cos(this._angle * (Math.PI/180)) * this._speed;
        // this._playerNode.y += Math.sin(this._angle * (Math.PI/180)) * this._speed;
    },

     //计算两点间的距离并返回
    _getDistance: function(pos1, pos2)
    {
        return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) +
        Math.pow(pos1.y - pos2.y, 2));
    },

    /*角度/弧度转换
    角度 = 弧度 * 180 / Math.PI
    弧度 = 角度 * Math.PI / 180*/
    //计算弧度并返回
    _getRadian: function(point)
    {
        this._radian = Math.PI / 180 * this._getAngle(point);
        return this._radian;
    },

    //计算角度并返回
    _getAngle: function(point)
    {
        
        var pos = this.node.getPosition();
        this._angle = Math.atan2(point.y - pos.y, point.x - pos.x) * (180/Math.PI);
        return this._angle;
    },

     //设置实际速度
    _setSpeed: function(point)
    {
        //触摸点和遥控杆中心的距离
        var distance = this._getDistance(point, this.node.getPosition());

        //如果半径
        if(distance < this._radius)
        {
            this._speed = this.speed1;
            cc.log(this._speed,this.speed1);
        }
        else
        {
            this._speed = this.speed2;
            cc.log(this._speed,this.speed2);
        }
    },

    _touchStartEvent: function(event) {
        // 获取触摸位置的世界坐标转换成圆圈的相对坐标(以圆圈的锚点为基准)
        var touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
        //触摸点与圆圈中心的距离
        var distance = this._getDistance(touchPos,cc.p(0,0));
        //圆圈半径
        var radius = this.node.width / 2;
        // 记录摇杆位置,给touch move使用
        this._stickPos = touchPos;
        var posX = this.node.getPosition().x + touchPos.x;
        var posY = this.node.getPosition().y + touchPos.y;
         //手指在圆圈内触摸,控杆跟随触摸点
        if(radius > distance)
        {
            this.dot.setPosition(cc.p(posX, posY));
            return true;
        }
        return false;
    },

    _touchMoveEvent: function(event){
        var touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
        var distance = this._getDistance(touchPos,cc.p(0,0));
        var radius = this.node.width / 2;
        // 由于摇杆的postion是以父节点为锚点,所以定位要加上ring和dot当前的位置(stickX,stickY)
        var posX = this.node.getPosition().x + touchPos.x;
        var posY = this.node.getPosition().y + touchPos.y;
        if(radius > distance)
        {
            this.dot.setPosition(cc.p(posX, posY));
        }
        else
        {
            //控杆永远保持在圈内,并在圈内跟随触摸更新角度
            var x = this.node.getPosition().x + Math.cos(this._getRadian(cc.p(posX,posY))) * radius;
            var y = this.node.getPosition().y + Math.sin(this._getRadian(cc.p(posX,posY))) * radius;
            this.dot.setPosition(cc.p(x, y));
        }
        //更新角度
        this._getAngle(cc.p(posX,posY));
        //设置实际速度
        this._setSpeed(cc.p(posX,posY));

    },

    _touchEndEvent: function(){
        this.dot.setPosition(this.node.getPosition());
        this._speed = 0;
    },
});

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容