| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import { _decorator, Component, Sprite, Label, Animation, Vec3, v3, ImageAsset, assetManager, SpriteFrame, Texture2D, EventHandler } from 'cc';
- import { Http } from '../../core/net/Http';
- import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
- import { BitmapFont } from '../../core/ui/BitmapFont';
- import { UITween } from '../../core/ui/tween/UITween';
- import { Utils } from '../../core/utils/Utils';
- import { g } from '../../Data/g';
- const { ccclass, property } = _decorator;
- @ccclass('ChildItem')
- export class ChildItem extends Component {
- @property({ type: Http, tooltip: "http组件" }) http: Http;
- @property({ type: Sprite, tooltip: "头像" }) head: Sprite;
- @property({ type: BitmapFont, tooltip: "贡献" }) moneyLabel: Label;
- @property({ type: Animation, tooltip: "动画组件" }) anim: Animation;
- @property({ type: EventHandler, tooltip: "tween结束回调窗口" }) handler: EventHandler;
- private startPos: Vec3;
- private userID = -1;
- start() {
- this.anim.on(Animation.EventType.FINISHED, () => {
- this.deleyAnimation();
- }, this);
- this.deleyAnimation();
- this.startPos = this.node.position.clone();
- this.node.active = false;
- }
- public async setData(data: any) {
- this.node.active = true;
- this.userID = data.userID;
- if (data.avatar) {
- let img = await ResourcesUtils.loadRemote<ImageAsset>(data.avatar, { ext: ".png" }, this);
- const spriteFrame = new SpriteFrame();
- const texture = new Texture2D();
- texture.image = img;
- spriteFrame.texture = texture;
- this.head.spriteFrame = spriteFrame;
- }
- this.moneyLabel.string = "+" + (!!data.revenueAmount ? data.revenueAmount + "" : "0");
- }
- public async onTouch() {
- if (this.userID != -1) {
- let result = await this.http.send("/api/fission/ReceiveFission", { receiveUserID: this.userID });
- if (result.code == 0) {
- g.userData.contribution += result.data.addTotalContribution;
- this.doTween();
- }
- }
- }
- public doTween() {
- return new Promise<any>((resolve: (data: any) => void) => {
- let self = this;
- self.getComponent(UITween).play();
- this.scheduleOnce(() => {
- resolve(null);
- }, 0.2);
- })
- }
- private deleyAnimation(): void {
- let self = this;
- this.scheduleOnce(() => {
- self.anim.play();
- }, Utils.random_both(0.5, 2.5));
- }
- public onTweenOver(): void {
- this.node.active = false;
- this.node.setPosition(this.startPos);
- this.node.setScale(v3(1, 1, 1));
- this.handler && this.handler.emit([]);
- }
- }
|