研究了一天,多点触控的点无法保存,只能模拟多点触控了
cc.Class({
extends: cc.Component,
properties: {
wheelStick:{
default:null,
type:cc.Sprite
},
hero:
{
default:null,
type:cc.Sprite
},
wheelDir:
{
default:"null",
}
},
// use this for initialization
onLoad: function () {
this.registerInput();
this.test()
},
registerInput:function()
{
var self=this
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
//开始
onTouchBegan: function(touch, event) {
self.onTouchBegan(self,touch);
return true; // don't capture event
},
//移动
onTouchMoved: function(touch, event) {
self.onTouchMoved(self,touch);
},
//结束
onTouchEnded: function(touch, event) {
self.onTouchEnded(self,touch);
}
}, self.node);
},
onTouchBegan:function(self,touch)
{
//show
let touchPos=touch.getLocation()
if(touchPos.x<480)
{
let wheelPos=self.node.position
self.wheelStick.node.position=cc.pSub(touchPos,wheelPos)
//发射消息
self.wheelDir=self.figureDirFromTouchPoint(touchPos)
}
},
onTouchMoved:function(self,touch)
{
//show
let touchPos=touch.getLocation()
if(touchPos.x<480)
{
let wheelPos=self.node.position
self.wheelStick.node.position=cc.pSub(touchPos,wheelPos)
//发射消息
self.wheelDir=self.figureDirFromTouchPoint(touchPos)
}
},
onTouchEnded:function(self,touch)
{
self.wheelStick.node.position=cc.p(0,0)
self.wheelDir="null"
},
figureDirFromTouchPoint:function(touchPoint)
{
let cenPoint=this.node.position;
let arr=cc.pSub(touchPoint,cenPoint)
let angle=Math.atan2(arr.y,arr.x)* 180 / 3.14
if (angle <= 45 && angle > -45)
return "right"
if (angle <= -135 || angle > 135)
return "left"
if (angle >= 45 && angle < 135)
return "up"
if (angle <= -45 && angle >-135)
return "down"
},
update:function()
{
let dir=this.wheelDir
if(dir=="up")
this.hero.node.emit('wheelup', {
msg: '',
});
else if(dir=="down")
this.hero.node.emit('wheeldown', {
msg: '',
});
else if(dir=="left")
this.hero.node.emit('wheelleft', {
msg: '',
});
else if(dir=="right")
this.hero.node.emit('wheelright', {
msg: '',
});
},
test:function()
{
//测试角度
let tdd=require("TDD")
let cenPoint=this.node.position;
let tp1=cc.p(cenPoint.x+1,cenPoint.y+0)
tdd.assert("right"==this.figureDirFromTouchPoint(tp1),"figureDirFromTouchPoint1")
let tp2=cc.p(cenPoint.x+1,cenPoint.y+0.5)
tdd.assert("right"==this.figureDirFromTouchPoint(tp2),"figureDirFromTouchPoint2")
let tp3=cc.p(cenPoint.x+1,cenPoint.y-0.5)
tdd.assert("right"==this.figureDirFromTouchPoint(tp3),"figureDirFromTouchPoint3")
let tp4=cc.p(cenPoint.x+0,cenPoint.y+1)
tdd.assert("up"==this.figureDirFromTouchPoint(tp4),"figureDirFromTouchPoint4")
let tp5=cc.p(cenPoint.x+0.5,cenPoint.y+1)
tdd.assert("up"==this.figureDirFromTouchPoint(tp5),"figureDirFromTouchPoint5")
let tp6=cc.p(cenPoint.x-0.5,cenPoint.y+1)
tdd.assert("up"==this.figureDirFromTouchPoint(tp6),"figureDirFromTouchPoint6")
let tp7=cc.p(cenPoint.x+0,cenPoint.y-1)
tdd.assert("down"==this.figureDirFromTouchPoint(tp7),"figureDirFromTouchPoint7")
let tp8=cc.p(cenPoint.x+0.5,cenPoint.y-1)
tdd.assert("down"==this.figureDirFromTouchPoint(tp8),"figureDirFromTouchPoint8")
let tp9=cc.p(cenPoint.x-0.5,cenPoint.y-1)
tdd.assert("down"==this.figureDirFromTouchPoint(tp9),"figureDirFromTouchPoint9")
let tp10=cc.p(cenPoint.x-1,cenPoint.y+0)
tdd.assert("left"==this.figureDirFromTouchPoint(tp10),"figureDirFromTouchPoint10")
let tp11=cc.p(cenPoint.x-1,cenPoint.y+0.5)
tdd.assert("left"==this.figureDirFromTouchPoint(tp11),"figureDirFromTouchPoint11")
let tp12=cc.p(cenPoint.x-1,cenPoint.y-0.5)
tdd.assert("left"==this.figureDirFromTouchPoint(tp12),"figureDirFromTouchPoint12")
//测试发送消息
this.hero.node.emit('testmsg', {
msg: 'Hello, this is Cocos Creator',
});
},
});