cocos creator
-
Uncaught ReferenceError: Global is not defined
解决:从定义 Global 的场景运行。 -
web-view(内嵌小游戏)跳转小程序时提示 "wx.miniprogram undefined"
解决:不要用 js 动态引入(document.createElement('script’)),应该直接把
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
放到 html 文件中。 -
九宫格是用来拉伸的
-
用户脚本组件在非激活状态下也会执行
-
spine 动画左右翻转
node.scaleX = -1
- 把 node1移动到 node2的位置
public static moveN1toN2(node1: cc.Node, node2: cc.Node) {
node1.position = node1.parent.convertToNodeSpaceAR(node2.parent.convertToWorldSpaceAR(node2.position))
}
- 取消带参数的计时器:
(1)返回带名字的 function
this.schedule(this.reading(p1, p2), 0.2);
reading(arg1, arg2) {
return function f(){
…
this.unschedule(f);
…
}
}
(2)重新声明一个函数
this.f = this.reading.bind(this, p1, p2);
this.schedule(this.f, 0.2);
reading(arg1, arg2) {
…
this.unschedule(this.f);
…
}
- 自动播放声音资源
解决:如果构建时勾选 MD5 Cache,需要转换资源路径:
const that = this;
const wxsdk= document.createElement('script');
wxsdk.src ='//res.wx.qq.com/open/js/jweixin-1.2.0.js';
document.body.appendChild(wxsdk);
wxsdk.onload=function(){
wx.config({
debug: false,
appId: '',
timestamp: 1,
nonceStr: '',
signature: '',
jsApiList: []
})
wx.ready(function () {
const audio = document.createElement('audio');
window.audio = audio;
let audioSrc = that.AudioClip;
if (cc.loader.md5Pipe) {
audioSrc = cc.loader.md5Pipe.transformURL(audioSrc);
}
if (audio.readyState === 0) {
audio.src = audioSrc;
audio.load();
}
const audioPlayStart = window.setInterval(() => {
if(audio.readyState === 4){
audio.play();
window.clearInterval(audioPlayStart);
}
},200);
})
}
//对于 resources 文件夹中的音频资源:
playAudio: function (currentId) {
const lesson = Global.lesson;
const category = Global.category;
const sort = Global.sort;
const audioUrl = sort + '/' + category + '/' + lesson + '/audio/audio' + currentId;
cc.loader.loadResDir(audioUrl, function (err, assets, urls) {
console.log('assets[0]',assets[0])
console.log('urls[0]',urls[0])
let audioSrc = assets[0].nativeUrl;
if (cc.loader.md5Pipe) {
audioSrc = cc.loader.md5Pipe.transformURL(audioSrc);
}
audio.src = audioSrc;
console.log('audio.src-gameWord',audio.src)
audio.load();
const audioPlayStart = window.setInterval(() => {
console.log('readyState',audio.readyState)
if(audio.readyState === 4){
audio.play();
window.clearInterval(audioPlayStart);
}
},200);
// util.playAudio(assets[0]);
});
}
- spine 远程加载
// effectPath: ../../../static/idle
const name = effectPath.substring(effectPath.lastIndexOf('/') + 1);
cc.assetManager.loadRemote(`${effectPath}/${name}.png`, (err, texture) => {
cc.assetManager.loadRemote(`${effectPath}/${name}.atlas`, (err, atlasText) => {
cc.assetManager.loadRemote(`${effectPath}/${name}.json`, (err, skeletonJson) => {
let asset = new sp.SkeletonData();
asset.skeletonJson = skeletonJson.json;
asset.atlasText = atlasText.text;
asset.textures = [texture];
asset.textureNames = [name + '.png'];
this.newSkeletonData = asset;
});
});
});
- 透明背景
(1)Main Camera节点:Camera组件:Background Color属性:透明度A设为0
(2)构建生成的 style-mobile.css:body:background-color: transparent;
(3)用户脚本组件首行添加:cc.macro.ENABLE_TRANSPARENT_CANVAS = true; - ios 静音开关问题
解决:为了使 ios 在打开静音开关时也能正常播放,需要使用 DOM Audio 模式加载音频。对于内置资源可以在属性检查器中直接修改,远程加载时可以传入 audioLoadMode 参数。android 不支持 DOM Audio。