Creator关于坐标的一些代码

update: function (dt) {
      //得到怪物的坐标
      var sp = this.guaiwu.convertToWorldSpaceAR(this.guaiwu.getPosition())
      //得到炮塔的坐标
      var pos  = this.node.convertToWorldSpaceAR(this.node.getPosition())
      cc.log(pos,sp)
      //计算怪物与炮塔的坐标差
      var truePos=cc.p(sp.x-pos.x,sp.y-pos.y)
      //将他们的坐标差转换为弧度
      var angle=cc.pAngleSigned(truePos,cc.p(0,1));
      //将他们的弧度转换为角度
      var angledegrees=cc.radiansToDegrees(angle);
      //让炮塔每一帧都往怪物的坐标进行角度变换
      this.node.runAction(cc.rotateTo(dt,angledegrees))




var po1 = this.paota.getPosition()
        
        if(0){
            //局部坐标转换
            var po2 = this.canvas.convertToNodeSpaceAR(touch.getLocation())
            cc.log(po1.x,po1.y,'  | ',po2.x, po2.y)
        }else{
            //全局坐标转换
            po1 = this.bg.convertToWorldSpaceAR(po1)
            po1 = cc.pSub(touch.getLocation(), po1)
        }
        var at = cc.radiansToDegrees(cc.pAngleSigned(po1, cc.p(0,1)))
        this.hand.rotation = at











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 TypeScript 编写的 Cocos Creator 代码示例: ```typescript // 引入 Cocos Creator 引擎模块 import { Component, Node, Vec2 } from 'cc'; // 自定义组件类 export default class MyComponent extends Component { // 节点引用 private _node: Node | null = null; // 生命周期:组件被添加到节点上时调用 public onEnable() { // 获取节点引用 this._node = this.node.getChildByName('MyNode'); // 注册触摸事件 this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this); } // 生命周期:组件被移除时调用 public onDisable() { // 取消注册触摸事件 this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this); // 释放节点引用 this._node = null; } // 触摸事件处理函数 private onTouchStart(event: any, touch: any) { // 获取触摸点在全局坐标系下的位置 const touchPos: Vec2 = touch.getLocation(); // 将触摸点位置转换为相对于 MyNode 节点的本地坐标系下的位置 const localPos: Vec2 = this._node!.convertToNodeSpaceAR(touchPos); // 输出本地坐标系下的位置 console.log(`Local position: (${localPos.x}, ${localPos.y})`); } } ``` 该代码示例是一个自定义组件类,包含了组件的生命周期函数和触摸事件处理函数。在 `onEnable` 生命周期函数中获取了节点引用,并注册了触摸事件。在 `onDisable` 生命周期函数中取消了触摸事件的注册,并释放了节点引用。在触摸事件处理函数中,通过 `convertToNodeSpaceAR` 方法将触摸点位置转换为相对于 MyNode 节点的本地坐标系下的位置,并输出该位置信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值