cocosCreator 新版本的动作函数API的应用

时间:2023-03-08 19:34:23
cocosCreator 新版本的动作函数API的应用

利用触摸位置判断,点击的是屏幕的左侧还是右侧,控制主角左右移动;

见代码:

InputControl:function () {
var self=this;
//cc.systemEvent
self.node.on(cc.Node.EventType.TOUCH_START,function(event){
var target=event.getCurrentTarget();
var touches = event.getTouches();
var touchLoc = touches[].getLocation();
var locationNode=target.convertToNodeSpace(touchLoc);
if(locationNode.x< self.node.width/){
self.MoveToLeft(); //向左边移动的函数
}
else{
self.MoveToRight();//向右边移动的函数
}
return true;
},self.node);
self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
}, self.node);
self.node.on(cc.Node.EventType.TOUCH_END, function (event) {
}, self.node);
},
MoveToRight:function () {
var goright=cc.moveTo(0.2,cc.p(this.node.width/-this.borderWith,this.player.getPositionY())).easing(cc.easeCubicActionIn());
this.player.runAction(goright);
},
MoveToLeft:function () {
var goleft=cc.moveTo(0.2,cc.p(-(this.node.width/-this.borderWith),this.player.getPositionY())).easing(cc.easeCubicActionIn());
this.player.runAction(goleft);
},