做一个cocos2d-html5的虚拟摇杆
来源:互联网 发布:淘宝图片护盾怎么设置 编辑:程序博客网 时间:2024/05/01 12:09
做了个简易的joystick 分享下~
先上代码
/** * Created by L on 14-2-4. */var CH5Joystick = cc.Layer.extend({ _winSize:null, _pCenter:null, _dock:null, _joystick:null, _delegate:null, _power:null, _direction:null, _angle:null, _Actived:null,//摇杆被激活 用户正在使用摇杆 _texture_dock_actived:null, _texture_dock_normal:null, ctor:function(){ this._super(); }, init:function () { if (this._super()){ this._Actived=false; this._winSize=cc.Director.getInstance().getWinSize(); this._pCenter = cc.p(this._winSize.width / 2,this._winSize.height / 2); this._texture_dock_actived=cc.TextureCache.getInstance().addImage(s_dock_actived); this._texture_dock_normal=cc.TextureCache.getInstance().addImage(s_dock_normal); //添加背景 this.addDock(); //添加球 this.addBall(); this.schedule(this.updateJoystick); } return true; }, updateJoystick:function(dt){ if(this._delegate&&this._Actived==true&&this._delegate.armature) { var newPos=cc.pAdd(this._delegate.armature.getPosition(),this._direction); //var winSize=this._delegate._winSize; var objSize=this._delegate.armature.getContentSize(); //设定屏幕边界 if((newPos.x+(objSize.width/2))>=this._winSize.width||(newPos.x-(objSize.width/2))<=0) { return; } if((newPos.y+(objSize.height/2))>=this._winSize.height||(newPos.y-(objSize.height/2))<=0) { return; } this._delegate.armature.setPosition(cc.pAdd(this._delegate.armature.getPosition(),this._direction)); } }, addDock:function(){ this._dock=cc.Sprite.create(s_dock_normal); this._dock.setPosition(cc.p(this._dock.getContentSize().width/2+5,this._dock.getContentSize().height/2)); this.addChild(this._dock); }, addBall:function(){ this._joystick=cc.Sprite.create(s_joystick); this._joystick.setPosition(cc.p(this._dock.getPosition().x,this._dock.getPosition().y)); this.addChild(this._joystick,1); }, onEnter:function(){ this._super(); cc.registerTargetedDelegate(0, true, this); }, onExit:function(){ cc.unregisterTouchDelegate(this); }, touchInAllowRadius:function(touch){ //判断touch是否在允许范围内 if(cc.rectContainsPoint(this._dock.getBoundingBox(),touch)) { //如果在范围内 返回true cc.log("touch在范围内"); return true; } else { //不在 则返回 false cc.log("touch不在范围内"); return false; } }, resertBallPosition:function(){ this._joystick.setPosition(cc.p(this._dock.getPosition().x,this._dock.getPosition().y)); }, calculate_angle_power_direction:function(pos){ this._angle=180+Math.atan2(this._dock.getPosition().y-this._joystick.getPosition().y,this._dock.getPosition().x-this._joystick.getPosition().x)/(3.14159/180); //力度计算 返回摇杆目标点至摇杆远点的距离 0-64 this._power=Math.sqrt(Math.pow((this._joystick.getPosition().x-this._dock.getPosition().x),2)+Math.pow((this._joystick.getPosition().y-this._dock.getPosition().y),2)); this._direction=cc.p(Math.cos(this._angle*(3.14159/180)),Math.sin(this._angle*(3.14159/180))); // cc.log(pos.x,pos.y); // cc.log("angle:"+angle,"power:"+power+"direction:"+direction.x +direction.y); }, onTouchBegan:function (touch, event){ // 点击点的范围判断 var curPoint = touch.getLocation(); this._dock.setTexture(this._texture_dock_actived); if(this._delegate) { this._Actived=true; this._delegate.actionJoystickStart(this._angle,this._power,this._direction); } if(this.touchInAllowRadius(curPoint)==true) { this._joystick.setPosition(curPoint); this.calculate_angle_power_direction(curPoint); } return true; }, onTouchMoved:function (touch, event){ var curPoint = touch.getLocation(); if(this._delegate) { this._Actived=true; this._delegate.actionJoystickStart(this._angle,this._power,this._direction); } if(this.touchInAllowRadius(curPoint)==true) { this._joystick.setPosition(curPoint); this.calculate_angle_power_direction(curPoint); } }, onTouchEnded:function (touch, event){ this.resertBallPosition(); this._dock.setTexture(this._texture_dock_normal); if(this._delegate) { this._Actived=false; this._delegate.actionJoystickEnd(this._angle,this._power,this._direction); } }});CH5Joystick.create= function(){ var joypad = new CH5Joystick(); if (joypad && joypad.init()){ return joypad; } return null;};
使用方法:
this._joystick=CH5Joystick.create();
this._joystick._delegate=this;
this.addChild(this._joystick);
actionJoystickStart:function(angle,power,direction){//your code}
actionJoystickEnd:function(angle,power,direction){//your code}
在CH5Joystick.js的updateJoystick:function(dt){ }中对你的角色进行控制
还不是很完善 慢慢更新
0 0
- 做一个cocos2d-html5的虚拟摇杆
- Cocos2d-js3.3 虚拟摇杆的实现
- Cocos2d-js3.3虚拟摇杆的实现
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- cocos2d-x的初步学习十三之虚拟摇杆SneakyInput
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- 遥控小车之:HTML5+ 虚拟摇杆
- 【html5手游开发】虚拟摇杆及虚拟按键的开发
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- 【cocos2d-x】之添加虚拟摇杆
- [cocos2d-x教程之 Hero教你打灰机] 如何制作一个虚拟摇杆
- Unity用UGUI做虚拟摇杆
- Unity用UGUI做虚拟摇杆
- UGUI 虚拟摇杆的实现
- unity虚拟摇杆的实现
- 基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动
- 教你一步步实现一个虚拟摇杆
- 虚拟摇杆
- 使用MySQL Migration Toolkit快速将Oracle数据导入MySQL
- 安卓中显示单位px和dip以及sp讲解
- 基础练习 特殊回文数
- ajax(一) 文本格式操作 get和post 实例
- Qt模块化笔记之Qt Widgets——动作类与小部件菜单项
- 做一个cocos2d-html5的虚拟摇杆
- Struts2与Servlet解耦
- [UVA 705] Slash Maze (建图 + DFS)
- 毕业设计--CTD
- Let the balloon rise
- android 新浪微博客户端的表情功能的实现
- 基础练习 回文数
- link安装法
- 第十篇:万丈高楼平地起-驱动编程基础知识点(二)