分类 Cocos 下的文章

把 cocos creator 2.4 項目 export 到目前項目發現 cocos creator 開啟就卡在讀取中

2024-05-28T18:22:26.151Z - success: wechatgame loaded2024-05-28T18:22:26.168Z - success: xiaomi-runtime loaded2024-05-28T18:22:26.572Z - success: im-plugin loaded2024-05-28T18:22:27.776Z - info: Cocos Service load base data!2024-05-28T18:22:28.381Z - normal: Initializing Cocos2d2024-05-28T18:22:28.794Z - normal: Initializing engine extends

項目開啟卡在 extends, 之前有遇過就是腳本問題, 分批把資源跟腳本分開匯入發現是腳本問題

//typescript 3.7 support 

this.level = data?.level || 1

//改成這個寫法就好了

this.level = data?data.level || 1:undefined

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
Optional Chaining 在3.7版本的說明
61872-a1xmg8jxgli.png

從 Cocos Creator 論壇看到功能在 3.0 才支持, 不知道為什麼原始項目卻能跑
50610-4r9wjgd5bb2.png

建構資源打包到 app 後發現個問題, 遊戲啟動就黑屏, 看了日誌如下
71709-8vuqnmarzhh.png

沒看出甚麼問題重新打個包, 發現一個奇怪的錯誤, 但之前都是這樣打包也沒注意這個情況
06298-lq8keank53.png

查看了系統配置, JAVA_HOME、PATH 都是正常的, 改其他版本 Java 也沒用, 最後發現 Cocos
這個配置是配置到 java 安裝目錄而不是 bin 目錄, 修改如下就好了
27549-w2j5isy8ix.png

打包 apk 測試遇到奇怪的問題,

[ERROR]: E/ ERROR: Uncaught TypeError: Error 6903, please go to https://github.com/cocos-creator/engine/blob/develop/EngineErrorMap.md#6903 to see details., location: src/cocos-js/cc.js:0:0

官網說這是資源加載有問題, 測試了確定不是加載不到, 是在後續流程使用clone出錯

public static createEffect(path: any, cb: any, parent: any) {
    this.loadRes("/gamePackages/effects/" + path, Prefab, function (err: any, 
        prefab: any) {
        if (err) {
            cb('err', null);
            return;
        }

        //實際是這裡出錯
        var node = instantiate(prefab);
        if (!parent) {
            parent = find("Canvas");
        }

        parent.addChild(node);
        cb(null, node);
        });
    }

測試了發現instantiate(prefab) 有問題, prefab載入的並非是prefab資源

public static loadRes(url: string, type: any, cb: Function = () => { }) {
        resources.load(url, (err: any, res: any) => {
            if (err) {
                error(err.message || err);
                cb(err, res);
                return;
            }
        cb && cb(null, res);
    })
}

由於 BonusTime.Prefab 跟 BonusTime.anim 原本在同一個層級目錄所造成的, resources.load 加載到的是 animation 資源

後來把 BonusTime.anim 改名成 BonusTimeaAim.anim 結果發現 animation.on("finished" 在動畫結束後調用沒有正常調用, 查看了下 animation 配置, 估計是因為修改了命名造成

代碼裡是這樣撥放的

let ani = node.getComponent(Animation);
ani.play('bonusTime');
ani.once(Animation.EventType.FINISHED, () => {
    node && node.destroy();
    callback(null);
 }, this);

後來把命名改回去, 拆分到不同目錄才解決問題

後來測試發現轉盤點Start會卡死, 看了看日誌也沒發現問題在哪

看了下代碼, 因為轉盤很單純的, 後來去排查老版本發現也有問題, 那就不是替換造成的了, 查了裡面的animation發現有missing, 把 animation.play 關閉後就好了

cocos 在onload setposition 无效

在 pveUI 加載 mainstageUI prefab 生成出來後掛載在 scrollview content 底下, 且mainstageUI 上面已經掛好了腳本 mainstageUIPanel, 奇怪的是 mainstageUIPanel 設置更新 pot position 卻無效。

一開始以為是 scrollview 還是其他因素造成, 後來測試許久發現是 mainstageUIPanel 內的都無效, 最終多方查看發現可能有一點奇妙的就是 mainstageUIPanel 初始化是寫在 onLoad 內的, 把位置刷新改到 start 再執行發現可以了。

可能是因為 prefab 生成掛載執行流程是 onload -> start, 在這過程中 widget 會再次重製, 所以才會無效, 也可以看看論壇說的

ref. https://forum.cocos.org/t/topic/137332

修改項目時發現這個"普通領取"文字在runtime沒顯示出來, 這個 RichText 的定位方式比較獨特, 不清楚介面作者為甚麼是這樣的思路的...
75076-xhbt0g2qm0p.png

37507-mrndvh741.png

main是整個簽到介面, 而btnNormalReceive則是最下面的"普通領取"按鈕
53041-ryv7y514gp.png

main的weidge預設是沒開啟的, 並且 align 為 bottom
54156-i1bbunzjv9.png

btnNormalReceive的 align 也是 bottom

Runtime下總是看不到, 後來切換了 resolution 總算發現了,順著這個因素去查
67335-44ett4fgsd2.png

普通領取文字其實是剛好跑到了白色

調整了下 mian 的 widget 設定改成 center, 切換過來發現 middle 竟然不是0, 那也就難怪代碼邏輯上去開啟 widget.enable = true 之後為什麼位置偏移蓋到了 "普通領取" 按鈕文字了
87242-q783n71yqg.png

再次修改 btnNormalReceive 放到 main 底下, 並且將 main、btnNormalReceive 兩個都設定為 center , 總算解決問題了
74833-21q4vvru1oz.png