如图我想动态将A图形的锚点转移到B图形的中心点 那么A的锚点应该咋么计算呢?
(A,B图形初始锚点都是(0.5,0.5) 新人求教
const { ccclass, property } = cc._decorator;
@ccclass
export default class ChangePos extends cc.Component {
@property({ type: cc.Vec2, displayName: '锚点', tooltip: '改变锚点而不改变相对位置' })
set anchor(pos: cc.Vec2) {
this._changAnchorPoint(pos);
}
get anchor() {
return this.node.getAnchorPoint();
}
@property({ type: cc.Vec2, displayName: '坐标', tooltip: '改变坐标而不改变相对位置' })
set position(pos: cc.Vec2) {
this._changePosition(pos);
}
get position() {
return this.node.getPosition(cc.v2());
}
/**
* 改变锚点而不改变相对位置
* @param newAchor 新的锚点
*/
_changAnchorPoint(newAchor: cc.Vec2) {
//新坐标
let x = (newAchor.x - this.node.anchorX) * this.node.width * this.node.scaleX;
let y = (newAchor.y - this.node.anchorY) * this.node.height * this.node.scaleY;
let newPos = cc.v2(x + this.node.x, y + this.node.y);
this.node.setPosition(newPos);
this.node.setAnchorPoint(newAchor);
}
/**
* 改变坐标而不改变相对位置
* @param newPos 新的坐标
*/
_changePosition(newPos: cc.Vec2) {
//新锚点
let x = (newPos.x - this.node.x) / this.node.width / this.node.scaleX
let y = (newPos.y - this.node.y) / this.node.height / this.node.scaleY;
let newAchor = cc.v2(x + this.node.anchorX, y + this.node.anchorY)
this.node.setPosition(newPos);
this.node.setAnchorPoint(newAchor);
}
}
```
给A挂上这脚本,坐标内输入B的坐标
1赞
非常感谢您
