2025-10-23 10:48:08 +04:00
|
|
|
// Learn TypeScript:
|
|
|
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
|
|
|
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
|
|
|
|
|
// Learn Attribute:
|
|
|
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
|
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
|
|
|
|
|
// Learn life-cycle callbacks:
|
|
|
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
|
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
|
|
|
|
|
|
|
|
|
const {ccclass, property} = cc._decorator;
|
|
|
|
|
|
|
|
|
|
@ccclass
|
|
|
|
|
export default class NewClass extends cc.Component {
|
|
|
|
|
|
|
|
|
|
private isDragging: boolean = false;
|
|
|
|
|
private touchOffset: cc.Vec2 = cc.v2(0, 0);
|
|
|
|
|
|
2025-10-23 14:11:10 +04:00
|
|
|
@property(cc.Prefab)
|
|
|
|
|
greenBullet: cc.Prefab = null;
|
|
|
|
|
|
|
|
|
|
shootBullets() {
|
|
|
|
|
var bullet = cc.instantiate(this.greenBullet);
|
|
|
|
|
bullet.setPosition(this.node.position.x, this.node.position.y + 40);
|
|
|
|
|
this.node.parent.addChild(bullet);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-23 10:48:08 +04:00
|
|
|
onLoad() {
|
|
|
|
|
// Enable touch on this node directly
|
|
|
|
|
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
|
|
|
|
|
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
|
|
|
|
this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
|
|
|
|
|
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
2025-10-23 14:11:10 +04:00
|
|
|
|
|
|
|
|
this.schedule(this.shootBullets, 0.1, cc.macro.REPEAT_FOREVER, 0);
|
2025-10-23 10:48:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onTouchStart(event: cc.Event.EventTouch) {
|
|
|
|
|
this.isDragging = true;
|
|
|
|
|
|
|
|
|
|
// Get touch location in parent coordinates
|
|
|
|
|
const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
|
|
|
|
|
|
// Calculate offset between jet center and touch point
|
|
|
|
|
this.touchOffset = this.node.position.sub(cc.v2(touchPos.x, touchPos.y));
|
|
|
|
|
}
|
|
|
|
|
onTouchMove(event: cc.Event.EventTouch) {
|
|
|
|
|
if (!this.isDragging) return;
|
|
|
|
|
|
|
|
|
|
// Convert touch position to parent space
|
|
|
|
|
const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
|
|
|
|
|
|
// Keep the offset so dragging feels natural (finger stays on the same point)
|
|
|
|
|
this.node.setPosition(cc.v2(touchPos.x, touchPos.y).add(this.touchOffset));
|
|
|
|
|
}
|
|
|
|
|
onTouchEnd(event: cc.Event.EventTouch) {
|
|
|
|
|
this.isDragging = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDestroy() {
|
|
|
|
|
this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
|
|
|
|
|
this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
|
|
|
|
this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
|
|
|
|
|
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
|
|
|
|
}
|
|
|
|
|
}
|