cocos-awesome/assets/Scene/Moving_ghost/Moving_ghost.ts
2020-04-06 02:37:20 +08:00

44 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { ccclass, property } = cc._decorator;
@ccclass
export default class Moving_ghost extends cc.Component {
@property([cc.Sprite])
ghostCanvasList: cc.Sprite[] = [];
@property(cc.Node)
role: cc.Node = null;
@property(cc.Camera)
roleCamera: cc.Camera = null;
onLoad() {
const texture = new cc.RenderTexture();
texture.initWithSize(this.node.width, this.node.height);
const spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
this.roleCamera.targetTexture = texture;
this.ghostCanvasList.forEach((sprite) => {
sprite.spriteFrame = spriteFrame;
});
this.schedule(this.ghostFollow, 0.1, cc.macro.REPEAT_FOREVER);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
}
touchMoveEvent(evt: cc.Event.EventTouch) {
this.role.x += evt.getDeltaX();
this.role.y += evt.getDeltaY();
}
beforeDestroy() {
this.unschedule(this.ghostFollow);
}
ghostFollow() {
this.ghostCanvasList.forEach((sprite, i) => {
const dis = (sprite.node.position as any).sub(this.role.position).mag();
if (dis < 0.5) return; // 给个误差范围涉及到浮点数计算dis的结果不可能精确为0小于0.5就可以认为是静止了
sprite.node.stopAllActions();
sprite.node.runAction(cc.moveTo(i * 0.04 + 0.02, this.role.x, this.role.y));
});
}
}