test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { _decorator, Component, EventTouch, Label, Node, UITransform, Vec2, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('test')
  4. export class test extends Component {
  5. @property(Node) bg:Node = null;
  6. @property(Node) img:Node = null;
  7. @property(Node) img_point:Node = null;
  8. @property(Node) btn_rotate:Node = null;
  9. protected is_rotate:boolean = false;
  10. protected anchor_point:Vec2 = new Vec2(0.5,0.5)
  11. start() {
  12. this.img.on(Node.EventType.TOUCH_END, (et:EventTouch)=> {
  13. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  14. let n_p = this.img.getComponent(UITransform).convertToNodeSpaceAR(p)
  15. // console.log('n_p',n_p)
  16. let img_position_x = n_p.x
  17. let img_position_y = n_p.y
  18. this.img_point.position = new Vec3(img_position_x, img_position_y, 1)
  19. this.anchor_point = new Vec2(0.5,0.5)
  20. this.img.getComponent(UITransform).setAnchorPoint(this.anchor_point)
  21. let img_contentSize = this.img.getComponent(UITransform).contentSize
  22. let anchor_point_x = (img_contentSize.width / 2 + n_p.x) / img_contentSize.width
  23. let anchor_point_y = (img_contentSize.height / 2 + n_p.y) / img_contentSize.height
  24. this.anchor_point = new Vec2(anchor_point_x, anchor_point_y)
  25. console.log('当前锚点=',this.anchor_point)
  26. },this)
  27. this.btn_rotate.on(Node.EventType.TOUCH_END, ()=> {
  28. if(this.is_rotate) {
  29. // 归位
  30. this.btn_rotate.children[0].getComponent(Label).string = '旋转'
  31. this.img.angle = 0
  32. this.anchor_point = new Vec2(0.5,0.5)
  33. this.img_point.position = new Vec3(0, 0, 1)
  34. } else {
  35. // 旋转
  36. this.btn_rotate.children[0].getComponent(Label).string = '归位'
  37. this.img.angle = 10
  38. }
  39. this.img.getComponent(UITransform).setAnchorPoint(this.anchor_point)
  40. this.is_rotate = !this.is_rotate
  41. },this)
  42. }
  43. }