做一个cocos2d-html5的虚拟摇杆
2014-02-03 16:07
309 查看
做了个简易的joystick 分享下~
先上代码
使用方法:
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){ }中对你的角色进行控制
还不是很完善 慢慢更新
先上代码
/** * 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){ }中对你的角色进行控制
还不是很完善 慢慢更新
相关文章推荐
- [cocos2d-x教程之 Hero教你打灰机] 如何制作一个虚拟摇杆
- 闲得无聊 之 Cocos2d html5引擎 显示一个菜单
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- 从零开始,使用Cocos2d HTML5完成一个游戏——第三步:连环反应
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- [入门教程]使用Cocos2d-html5游戏引擎编写一个简单的游戏 第一回合: 搭建开发环境
- cocos2D 虚拟摇杆Joystick功能实现
- Cocos2d-js3.3虚拟摇杆的实现
- 教你一步步实现一个虚拟摇杆
- Unity3D自制虚拟摇杆和一个方法控制多个按钮
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- Cocos2d-x3.2实现虚拟摇杆多点触摸-----沈大海
- Cocos2d-x《赵云要格斗》--虚拟摇杆控制精灵上下左右运动 推荐
- Cocos2d-x虚拟摇杆控制精灵上下左右运动
- 【cocos2d-x】之添加虚拟摇杆
- Cocos2d-x虚拟摇杆控制精灵上下左右运动----之游戏开发《赵云要格斗》(1)
- 【Cocos2d-x】新手自学(三)虚拟屏幕摇杆,控制精灵移动
- cocos2d-x 3.x学习之虚拟摇杆
- 【Cocosd2d实例教程五】Cocos2d添加虚拟摇杆控制器
- Cocos2d-HTML5游戏引擎,编写一个简单的打飞机游戏