Spring774
2025-10-27 a621979f62588ed1a899d17c937dc3aee8670ec1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const {ccclass, property} = cc._decorator;
 
@ccclass
export default class FireController extends cc.Component {
 
    @property(cc.Node)
    markNode: cc.Node = null;
 
    onLoad(){
        // 使用枚举类型来注册
        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.onTouchCancel, this);
    }
 
    onTouchStart(event: cc.Event.EventTouch){
        this.onTouchMove(event);
    }
 
    onTouchMove(event: cc.Event.EventTouch){
        let currTouch = event.touch.getLocation();//world position
        let localPos = this.node.convertToNodeSpaceAR(cc.v2(currTouch.x, currTouch.y));
        let localVec = cc.v2(localPos.x, localPos.y);
        //cc.log("localPos = " + localVec);
 
        if (localVec.x > 170){
            localVec.x = 170;
        }else if(localVec.x < -170){
            localVec.x = -170;
        }
 
        this.markNode.setPosition(cc.v2(localVec.x, this.markNode.position.y));
 
        
        //4 - 176 degree
        let degree = 4 + 172 * ( (170 - localVec.x) / 340 );
        let angle = cc.misc.degreesToRadians(degree);
        let direction:cc.Vec2 = cc.v2(Math.cos(angle), Math.sin(angle));
        // this.playground.aim(direction);
    }
 
    onTouchEnd(event: cc.Event.EventTouch){
        // this.playground.tryFire();
        this.resetMarkNodePosition();
    }
 
    onTouchCancel(event: cc.Event.EventTouch){
        // this.playground.cancelAim();
        this.resetMarkNodePosition();
    }
 
    resetMarkNodePosition(){
        this.markNode.setPosition(cc.v2(0, this.markNode.position.y));
    }
}