cocos-awesome/assets/Scene/Moving_ghost/Moving_ghost.ts

50 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2020-04-05 18:35:34 +00:00
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() {
2020-04-11 10:15:04 +00:00
const roleZindex = 10;
this.role.zIndex = roleZindex;
2020-04-05 18:35:34 +00:00
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;
2020-04-11 10:15:04 +00:00
this.ghostCanvasList.forEach((ghost, idx) => {
ghost.node.scaleY = -1;
ghost.node.zIndex = roleZindex - idx;
ghost.node.opacity = 100 - idx * 15;
ghost.spriteFrame = spriteFrame;
2020-04-05 18:35:34 +00:00
});
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() {
2020-04-11 10:15:04 +00:00
this.ghostCanvasList.forEach((ghost, i) => {
const dis = (ghost.node.position as any).sub(this.role.position).mag();
if (dis < 0.5) return; // 给个误差范围涉及到浮点数dis的计算不可能精准小于0.5就可以认为是静止了
ghost.node.stopAllActions();
ghost.node.runAction(cc.moveTo(i * 0.04 + 0.02, this.role.x, this.role.y));
2020-04-05 18:35:34 +00:00
});
}
}