Cocos Creater学习之手指虚拟摇杆控制角色移动

一、国际惯例引入各种组件

import { _decorator, Component, CCInteger, Node, input, Input, EventTouch, Vec2} from 'cc';
const { ccclass, property } = _decorator;

二、预设各种Node节点,变量

	// 玩家移动
	@property(Node)
	playerMove : Node
	// 移动速度
	@property({ type: CCInteger })
	public Speed : number = 200;
	// 方向
	dir : Vec2 = new Vec2(0, 0)

三、加载各种输入事件

	onLoad() {
		//节点初始位置,每次触摸结束更新
		const nodePos = this.playerMove.getPosition();
		//触摸监听(this.node.parent是屏幕)
		//想达到按住节点,节点才能移动的效果,将监听函数注册到 this.node 上,去掉  .parent 即可
		this.playerMove.parent.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
		this.playerMove.parent.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
		this.playerMove.parent.on(Input.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
		console.log('TOUCH_MOVE加载' + nodePos + 'dir是什么,' + this.dir.x + this.dir.y)
	}

四、方向控制

	//方向控制;
	onTouchMove(event) {
		const delta = event.getDelta(); //获取移动的差值
		console.log('TOUCH_MOVE' + delta.x + delta.y)  //例行打印
		// 通过移动的差值判断方向
		if(delta.x > 0){
			console.log('右移')
			this.dir.x = 1
		}else if(delta.x < -0){
			console.log('左移')
			this.dir.x = -1
		}else if(delta.x == 0){
			console.log('左右停止')
			this.dir.x = 0
		}
		if(delta.y > 0){
			console.log('上移')
			this.dir.y = 1
		}else if(delta.y < -0){
			console.log('下移')
			this.dir.y = -1
		}else if(delta.y == 0){
			console.log('上下停止')
			this.dir.y = 0
		}
	}

五、移开手指还原变量

	onTouchEnd() {
		this.dir = new Vec2(0, 0);
		console.log('重置方向')
	}
	onTouchCancel() {
		this.dir = new Vec2(0, 0);
		console.log('重置方向')
	}

六、移动方法,根据方向和预设的速度移动

Moving(){
		const pos = this.playerMove.position;
		this.playerMove.setPosition(pos.x + 0.01 * this.dir.x * this.Speed, pos.y + 0.01 * this.dir.y * this.Speed)
	}

七、走内置的update持续执行移动方法实现角色移动

	update(deltaTime : number) {
		this.Moving()
	}

完整代码:

import { _decorator, Component, CCInteger, Node, input, Input, EventTouch, Vec2} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
	// 玩家移动
	@property(Node)
	playerMove : Node
	// 移动速度
	@property({ type: CCInteger })
	public Speed : number = 200;
	// 方向
	dir : Vec2 = new Vec2(0, 0)

	onLoad() {
		//节点初始位置,每次触摸结束更新
		const nodePos = this.playerMove.getPosition();
		//触摸监听(this.node.parent是屏幕)
		//想达到按住节点,节点才能移动的效果,将监听函数注册到 this.node 上,去掉  .parent 即可
		this.playerMove.parent.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
		this.playerMove.parent.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
		this.playerMove.parent.on(Input.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
		console.log('TOUCH_MOVE加载' + nodePos + 'dir是什么,' + this.dir.x + this.dir.y)
	}
	//方向控制;
	onTouchMove(event) {
		const delta = event.getDelta(); //获取移动的差值
		console.log('TOUCH_MOVE' + delta.x + delta.y)  //例行打印
		// 通过移动的差值判断方向
		if(delta.x > 0){
			console.log('右移')
			this.dir.x = 1
		}else if(delta.x < -0){
			console.log('左移')
			this.dir.x = -1
		}else if(delta.x == 0){
			console.log('左右停止')
			this.dir.x = 0
		}
		if(delta.y > 0){
			console.log('上移')
			this.dir.y = 1
		}else if(delta.y < -0){
			console.log('下移')
			this.dir.y = -1
		}else if(delta.y == 0){
			console.log('上下停止')
			this.dir.y = 0
		}
	}
	onTouchEnd() {
		this.dir = new Vec2(0, 0);
		console.log('重置方向')
	}
	onTouchCancel() {
		this.dir = new Vec2(0, 0);
		console.log('重置方向')
	}
	Moving(){
		const pos = this.playerMove.position;
		this.playerMove.setPosition(pos.x + 0.01 * this.dir.x * this.Speed, pos.y + 0.01 * this.dir.y * this.Speed)
	}

	update(deltaTime : number) {
		this.Moving()
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值