update
This commit is contained in:
parent
81a9a5a2f8
commit
6d37ab0468
@ -1,55 +1,53 @@
|
|||||||
import { _decorator, Component, Node, input, Input, EventTouch, Vec2, Vec3, UITransform } from "cc";
|
import { _decorator, Component, Node, input, Input, EventTouch, Vec2, Vec3, UITransform } from "cc"
|
||||||
const { ccclass, property } = _decorator;
|
const { ccclass, property } = _decorator
|
||||||
|
|
||||||
@ccclass("JoyStickManager")
|
@ccclass("JoyStickManager")
|
||||||
export class JoyStickManager extends Component {
|
export class JoyStickManager extends Component {
|
||||||
input: Vec2 = Vec2.ZERO;
|
input: Vec2 = Vec2.ZERO
|
||||||
|
|
||||||
private body: Node;
|
private body: Node
|
||||||
private stick: Node;
|
private stick: Node
|
||||||
private touchStartPos: Vec2;
|
private defaultPos: Vec2
|
||||||
private defaultPos: Vec2;
|
private radius: number = 0
|
||||||
private radius: number = 0;
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
this.body = this.node.getChildByName("Body");
|
this.body = this.node.getChildByName("Body")
|
||||||
this.stick = this.body.getChildByName("Stick");
|
this.stick = this.body.getChildByName("Stick")
|
||||||
const { x, y } = this.body.position;
|
const { x, y } = this.body.position
|
||||||
this.defaultPos = new Vec2(x, y);
|
this.defaultPos = new Vec2(x, y)
|
||||||
this.radius = this.body.getComponent(UITransform).contentSize.x / 2;
|
this.radius = this.body.getComponent(UITransform).contentSize.x / 2
|
||||||
|
|
||||||
input.on(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
|
||||||
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
|
||||||
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
onDestroy() {
|
onDestroy() {
|
||||||
input.off(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
|
||||||
input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
|
||||||
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
onTouchStart(e: EventTouch) {
|
||||||
|
const touchPos = e.getUILocation()
|
||||||
|
this.body.setPosition(touchPos.x, touchPos.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
onTouchMove(e: EventTouch) {
|
onTouchMove(e: EventTouch) {
|
||||||
const touchPos = e.touch.getUILocation();
|
const touchPos = e.getUILocation()
|
||||||
if (!this.touchStartPos) {
|
const stickPos = new Vec2(touchPos.x - this.body.position.x, touchPos.y - this.body.position.y)
|
||||||
this.touchStartPos = touchPos.clone();
|
if (stickPos.length() > this.radius) {
|
||||||
this.body.setPosition(this.touchStartPos.x, this.touchStartPos.y);
|
stickPos.multiplyScalar(this.radius / stickPos.length())
|
||||||
}
|
}
|
||||||
|
this.stick.setPosition(stickPos.x, stickPos.y)
|
||||||
|
|
||||||
const stickPos = new Vec2(touchPos.x - this.touchStartPos.x, touchPos.y - this.touchStartPos.y);
|
this.input = stickPos.clone().normalize()
|
||||||
const len = stickPos.length();
|
console.log(this.input)
|
||||||
if (len > this.radius) {
|
|
||||||
stickPos.multiplyScalar(this.radius / len);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.stick.setPosition(stickPos.x, stickPos.y);
|
|
||||||
this.input = stickPos.clone().normalize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onTouchEnd() {
|
onTouchEnd() {
|
||||||
this.body.setPosition(this.defaultPos.x, this.defaultPos.y);
|
this.body.setPosition(this.defaultPos.x, this.defaultPos.y)
|
||||||
this.stick.setPosition(0, 0);
|
this.stick.setPosition(0, 0)
|
||||||
this.touchStartPos = undefined;
|
this.input = Vec2.ZERO
|
||||||
this.input = Vec2.ZERO;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user