摇杆是所有手游中不可或缺的一部分,是最基本的操作方式,下面是用cococ creator实现的一个简单的四方向摇杆,后面将基于此慢慢优化。
一、场景
新建一个场景,拖入三中图片,如图:
分别表示:spPlayer 代表游戏中的角色,通过摇杆控制其移动;
spRoker 摇杆的事件影响区域;
spRokerCenter 摇杆的中心点。
二、事件监听
为了能控制摇杆,需要监听摇杆的事件,新建一个脚本CompRoker,挂到Canvas上,然后编辑器其内容如下:
cc.Class({
extends: cc.Component,
properties: {
spPlayer: cc.Sprite,
spRoker: cc.Sprite,
spRokerCenter: cc.Sprite
},
onLoad: function () {
this.spRoker.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.spRoker.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.spRoker.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.spRoker.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTo