最近接到一个任务要开发消消乐小游戏,固然首先就想到乐cocosCreator来做为开发工具。 开发自己倒没有多少难点。消消乐的开发官网发行的书上有专门讲到。下面主要总结一下开发中遇到的问题以及解决方法node
因为设计尺寸是750*1336,若是适应高度,则在iphonX下,内容会超出屏幕宽度。按宽适应,iphon4下内容会超出屏幕高度。因此就须要根据屏幕比例来动态设置适配策略。nginx
onLoad() {
var canvas = this.canvas
var designResolution = canvas.designResolution
var viewSize = cc.view.getFrameSize()
if (viewSize.width/viewSize.height > designResolution.width/designResolution.height)
{
canvas.fitHeight = true
}
else{
canvas.fitWidth = true
}
}
复制代码
因为微信头像显示涉及到跨域问题,因此我在咱们游戏所在服务器上用nginx设置了反向代理来解决跨域问题,并把获取到的微信头像地址域名替换成服务器所在域名npm
let url = userObj.headimgurl.replace('http://thirdwx.qlogo.cn',
'咱们的服务器域名');
cc.loader.load({ url, type: 'png' }, (err, res) => {
Message.hide();
this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(res);
});
复制代码
因为要根据动态地址生成二维码这里用到QR.js+graphics来实现。首先引入QR.js(npm上能够找到)并设置为插件引入。 在所在节点上添加Graphics。而后用以下代码生成二维码canvas
//生成二维码
createrQR() {
const urlString = 'http://xxxxxx';
const graphics = this.qrNode.getComponent(cc.Graphics);
// return
graphics.clear();
//背景色
graphics.fillColor = cc.Color.WHITE;
//let rect = this.node.getBoundingBox();
let width = this.qrNode.width;
graphics.rect(0 - width * 0.5, 0 - width * 0.5, width, width);
graphics.fill();
graphics.close();
//生成二维码数据
let qrcode = new QRCode(-1, 2);
qrcode.addData(urlString);
qrcode.make();
graphics.fillColor = cc.Color.BLACK;
let size = width - 10 * 2;
let num = qrcode.getModuleCount();
let tileW = size / num;
let tileH = size / num;
let w = Math.ceil(tileW);
let h = Math.ceil(tileH);
for (let row = 0; row < num; row++) {
for (let col = 0; col < num; col++) {
if (qrcode.isDark(row, col)) {
graphics.rect(10 + col * tileW - width * 0.5, size - tileH - Math.round(row * tileH) + 10 - width * 0.5, w, h);
graphics.fill();
}
}
}
}
复制代码
//截屏
capture() {
cc.director.on(cc.Director.EVENT_AFTER_DRAW, this.callback.bind(this));
}
callback() {
let canvas = document.getElementById("GameCanvas") as HTMLCanvasElement;
let baseUrl = canvas.toDataURL("imagea/png");
cc.director.off(cc.Director.EVENT_AFTER_DRAW);
this.showImgDiv(baseUrl)
}
复制代码
在初始场景上添加一个节点,节点的层级要比场景的canvas高才能看的到音乐图标。而后在节点上添加一个Sprite,把音乐图标加入上面。在该节点上添加以下代码将该节点设置为常驻节点,并控制其播放暂停。跨域
@ccclass
export default class NewClass extends cc.Component {
@property({
type: cc.AudioClip
})
bgAudio: cc.AudioClip;
audioid;
playState: boolean = true;
action;
onEnable() {
cc.game.addPersistRootNode(this.node);
this.autoAudioPlay();
}
autoAudioPlay = () => {
if(typeof this.audioid ==="undefined"){
this.audioid = cc.audioEngine.play(this.bgAudio, true, 1)
}
this.action = cc.repeatForever(cc.rotateBy(3, 360));
this.node.runAction(this.action);
}
playSwitch = () => {
console.log(this.playState)
if (this.playState) {
this.playState = false;
// 中止一个动做
this.node.stopAction(this.action);
cc.audioEngine.pause(this.audioid)
} else {
this.playState = true;
// 执行动做
this.node.runAction(this.action);
cc.audioEngine.resume(this.audioid);
}
}
onDestroy() {
}
}
复制代码