Commit 39cfbc65 authored by wangxin's avatar wangxin

优化适配

parent 4a62cde1
...@@ -3,320 +3,325 @@ ...@@ -3,320 +3,325 @@
*/ */
cc.Class({ cc.Class({
extends: cc.Component, extends: cc.Component,
properties: { properties: {
options: { options: {
default: null, default: null,
type: cc.Node, type: cc.Node,
}, },
questionFrame: { questionFrame: {
default: null, default: null,
type: cc.Node, type: cc.Node,
}, },
// 标题 // 标题
title: { title: {
default: null, default: null,
type: cc.Label, type: cc.Label,
}, },
question: { question: {
default: null, default: null,
type: cc.Label, type: cc.Label,
}, },
openMusic: { openMusic: {
default: null, default: null,
type: cc.AudioClip, type: cc.AudioClip,
}, },
op_1: { op_1: {
default: null, default: null,
type: cc.Prefab, type: cc.Prefab,
}, },
op_2: { op_2: {
default: null, default: null,
type: cc.Prefab, type: cc.Prefab,
}, },
cloud: { cloud: {
default: null, default: null,
type: cc.Node, type: cc.Node,
}, },
che: { che: {
default: null, default: null,
type: cc.Node, type: cc.Node,
}, },
_isPlayTitle: false, _isPlayTitle: false,
_isPlayQuestion: false, _isPlayQuestion: false,
}, },
onEnable() { onEnable() {
cc.game.on("STOPALL", this.stopAllAudio, this); cc.game.on("STOPALL", this.stopAllAudio, this);
}, },
onDisable() { onDisable() {
cc.game.off("STOPALL", this.stopAllAudio, this); cc.game.off("STOPALL", this.stopAllAudio, this);
}, },
// 生命周期 onLoad // 生命周期 onLoad
onLoad() { onLoad() {
this.initSceneData(); this.initSceneData();
this.initSize(); this.initSize();
}, },
_imageResList: null, _imageResList: null,
_audioResList: null, _audioResList: null,
_animaResList: null, _animaResList: null,
initSceneData() { initSceneData() {
this._imageResList = []; this._imageResList = [];
this._audioResList = []; this._audioResList = [];
this._animaResList = []; this._animaResList = [];
}, },
_designSize: null, // 设计分辨率 _designSize: null, // 设计分辨率
_frameSize: null, // 屏幕分辨率 _frameSize: null, // 屏幕分辨率
_mapScaleMin: null, // 场景中常用缩放(取大值) _mapScaleMin: null, // 场景中常用缩放(取大值)
_mapScaleMax: null, // 场景中常用缩放(取小值) _mapScaleMax: null, // 场景中常用缩放(取小值)
_cocosScale: null, // cocos 自缩放 (较少用到) _cocosScale: null, // cocos 自缩放 (较少用到)
initSize() { initSize() {
// 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小 // 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小
let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height; let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height;
let design_size = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height; let design_size = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height;
let f = screen_size >= design_size; let f = screen_size >= design_size;
cc.Canvas.instance.fitHeight = f; cc.Canvas.instance.fitHeight = f;
cc.Canvas.instance.fitWidth = !f; cc.Canvas.instance.fitWidth = !f;
const frameSize = cc.view.getFrameSize(); const frameSize = cc.view.getFrameSize();
this._frameSize = frameSize; this._frameSize = frameSize;
this._designSize = cc.view.getDesignResolutionSize(); this._designSize = cc.view.getDesignResolutionSize();
let sx = cc.winSize.width / frameSize.width; let sx = cc.winSize.width / frameSize.width;
let sy = cc.winSize.height / frameSize.height; let sy = cc.winSize.height / frameSize.height;
this._cocosScale = Math.min(sx, sy); this._cocosScale = Math.min(sx, sy);
sx = frameSize.width / this._designSize.width; sx = frameSize.width / this._designSize.width;
sy = frameSize.height / this._designSize.height; sy = frameSize.height / this._designSize.height;
this._mapScaleMin = Math.min(sx, sy) * this._cocosScale; this._mapScaleMin = Math.min(sx, sy) * this._cocosScale;
this._mapScaleMax = Math.max(sx, sy) * this._cocosScale; this._mapScaleMax = Math.max(sx, sy) * this._cocosScale;
}, },
// 生命周期 start // 生命周期 start
start() { start() {
let getData = this.getData.bind(this); let getData = this.getData.bind(this);
if (window && window.courseware) { if (window && window.courseware) {
getData = window.courseware.getData; getData = window.courseware.getData;
} }
getData((data) => { getData((data) => {
console.log("data:", data); console.log("data:", data);
this.data = data || this.getDefaultData(); this.data = data || this.getDefaultData();
this.data = JSON.parse(JSON.stringify(this.data)); this.data = JSON.parse(JSON.stringify(this.data));
this.preloadItem(); this.preloadItem();
}); });
}, },
getData(cb) { getData(cb) {
cb(this.getDefaultData()); cb(this.getDefaultData());
}, },
getDefaultData() { getDefaultData() {
const dataJson = const dataJson =
'{"title":"test","answer":3,"question":"hello world hello world hello world hello world","options":[{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2, how are you doing aaaa"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"","text":"选项2, how are you doing aaaa"}],"audio_title":"http://staging-teach.cdn.ireadabc.com/f7fb9f71ab7d460ac2f78c85efba7610.mp3","audio_question":"http://staging-teach.cdn.ireadabc.com/7934cdcec68eabe639020bad5420d7f8.mp3"}'; '{"title":"test","answer":3,"question":"hello world hello world hello world hello world","options":[{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"http://staging-teach.cdn.ireadabc.com/e8627e7f3ec1a04fdb3660eae81f7636.mp3","text":"选项2, how are you doing aaaa"},{"pic_url":"http://staging-teach.cdn.ireadabc.com/3064a1f8bec54390c031b2c2d8b84e7f.jpeg","audio_url":"","text":"选项2, how are you doing aaaa"}],"audio_title":"http://staging-teach.cdn.ireadabc.com/f7fb9f71ab7d460ac2f78c85efba7610.mp3","audio_question":"http://staging-teach.cdn.ireadabc.com/7934cdcec68eabe639020bad5420d7f8.mp3"}';
const data = JSON.parse(dataJson); const data = JSON.parse(dataJson);
return data; return data;
}, },
preloadItem() { preloadItem() {
this.addPreloadImage(); this.addPreloadImage();
this.addPreloadAudio(); this.addPreloadAudio();
this.addPreloadAnima(); this.addPreloadAnima();
this.preload(); this.preload();
}, },
addPreloadImage() { addPreloadImage() {
for (var i = 0; i < this.data.options; i++) { for (var i = 0; i < this.data.options; i++) {
this._imageResList.push({ url: this.data.options[i]["pic_url"] }); this._imageResList.push({ url: this.data.options[i]["pic_url"] });
} }
}, },
addPreloadAudio() { addPreloadAudio() {
for (var i = 0; i < this.data.total; i++) { for (var i = 0; i < this.data.total; i++) {
this._audioResList.push({ url: this.data.options[i]["audio_url"] }); this._audioResList.push({ url: this.data.options[i]["audio_url"] });
} }
this._audioResList.push({ url: this.data.audio_title }); this._audioResList.push({ url: this.data.audio_title });
this._audioResList.push({ url: this.data.audio_question }); this._audioResList.push({ url: this.data.audio_question });
}, },
addPreloadAnima() {}, addPreloadAnima() {},
preload() { preload() {
const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList); const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList);
cc.assetManager.loadAny(preloadArr, null, null, (err, data) => { cc.assetManager.loadAny(preloadArr, null, null, (err, data) => {
this.loadEnd(); this.loadEnd();
if (window && window["air"]) { if (window && window["air"]) {
window["air"].hideAirClassLoading(); window["air"].hideAirClassLoading();
} }
cc.debug.setDisplayStats(false); cc.debug.setDisplayStats(false);
}); });
}, },
loadEnd() { loadEnd() {
this.initData(); this.initData();
this.initView(); this.initView();
}, },
_cantouch: null, _cantouch: null,
initData() { initData() {
// 所有全局变量 默认都是null // 所有全局变量 默认都是null
this._cantouch = true; this._cantouch = true;
}, },
initView() { initView() {
this.initBg(); this.initBg();
// 初始化title // 初始化title
this.title.string = this.data.title; this.title.string = this.data.title;
this.question.string = this.data.question; this.question.string = this.data.question;
let count = this.data.options.length; let count = this.data.options.length;
let scale = 1; let scale = 1;
let scaleAdjust = 1; let scaleAdjust = 1;
let countScale = this.data.options.length - 4; let countScale = this.data.options.length - 4;
if (countScale <= 1) { if (countScale <= 1) {
scaleAdjust = 0.2; scaleAdjust = 0.2;
} else if (countScale >= 2 && countScale < 3) { } else if (countScale >= 2 && countScale < 3) {
scaleAdjust = 0.16; scaleAdjust = 0.16;
} else if (countScale >= 3 && countScale < 4) { } else if (countScale >= 3 && countScale < 4) {
scaleAdjust = 0.14; scaleAdjust = 0.14;
} else if (countScale >= 4 && countScale < 5) { } else if (countScale >= 4 && countScale < 5) {
scaleAdjust = 0.13; scaleAdjust = 0.13;
} else { } else {
scaleAdjust = 0.12; scaleAdjust = 0.12;
} }
if (countScale > 0) { if (countScale > 0) {
scale = 1 - countScale * scaleAdjust; scale = 1 - countScale * scaleAdjust;
} }
for (let i = 0; i < this.data.options.length; i++) { let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height;
let op = null; if (screen_size < 16 / 9) {
if (count >= 4) { scale = scale * 0.9;
op = cc.instantiate(this.op_1); }
} else {
op = cc.instantiate(this.op_2); for (let i = 0; i < this.data.options.length; i++) {
} let op = null;
op.scale = scale; if (count >= 4) {
if (i == this.data.answer) { op = cc.instantiate(this.op_1);
op.getComponent("option")._isAnswer = true; } else {
} op = cc.instantiate(this.op_2);
op.getComponent("option").setPic(this.data.options[i].pic_url); }
op.getComponent("option").setTitle(this.data.options[i].text); op.scale = scale;
op.getComponent("option").setAudio(this.data.options[i].audio_url); if (i == this.data.answer) {
op.getComponent("option")._count = this.data.options.length; op.getComponent("option")._isAnswer = true;
this.options.addChild(op); }
} op.getComponent("option").setPic(this.data.options[i].pic_url);
cc.audioEngine.playEffect(this.openMusic, false); op.getComponent("option").setTitle(this.data.options[i].text);
op.getComponent("option").setAudio(this.data.options[i].audio_url);
let self = this; op.getComponent("option")._count = this.data.options.length;
this.scheduleOnce(function () { this.options.addChild(op);
self.options.getComponent(cc.Layout).enabled = false; }
}, 0.2); cc.audioEngine.playEffect(this.openMusic, false);
cc.view.setResizeCallback(function () { let self = this;
let frameSize = cc.view.getFrameSize(); this.scheduleOnce(function () {
let designSize = cc.Canvas.instance.designResolution; self.options.getComponent(cc.Layout).enabled = false;
let scaleX = frameSize.width / designSize.width; }, 0.2);
let scaleY = frameSize.height / designSize.height;
let scale = Math.min(scaleX, scaleY); cc.view.setResizeCallback(function () {
if (scale < 1) { let frameSize = cc.view.getFrameSize();
self.options.scale = scale; let designSize = cc.Canvas.instance.designResolution;
} let scaleX = frameSize.width / designSize.width;
}); let scaleY = frameSize.height / designSize.height;
}, let scale = Math.min(scaleX, scaleY);
if (scale < 1) {
playTitle() { self.options.scale = scale;
if (this.data.audio_title && !this._isPlayTitle) { }
cc.game.emit("STOPALL"); });
this._isPlayTitle = true; },
this.playAudioByUrl(this.data.audio_title, () => {
this._isPlayTitle = false; playTitle() {
}); if (this.data.audio_title && !this._isPlayTitle) {
} cc.game.emit("STOPALL");
}, this._isPlayTitle = true;
this.playAudioByUrl(this.data.audio_title, () => {
playQuestion() { this._isPlayTitle = false;
if (this.data.audio_question && !this._isPlayQuestion) { });
cc.game.emit("STOPALL"); }
this._isPlayQuestion = true; },
this.playAudioByUrl(this.data.audio_question, () => {
this._isPlayQuestion = false; playQuestion() {
}); if (this.data.audio_question && !this._isPlayQuestion) {
} cc.game.emit("STOPALL");
}, this._isPlayQuestion = true;
this.playAudioByUrl(this.data.audio_question, () => {
initBg() { this._isPlayQuestion = false;
const bgNode = cc.find("Canvas/bg"); });
bgNode.scale = this._mapScaleMax; }
},
this.che.scale = this._mapScaleMax;
this.cloud.scale = this._mapScaleMax; initBg() {
this.questionFrame.scale = this._mapScaleMin; const bgNode = cc.find("Canvas/bg");
this.question.node.scale = this._mapScaleMin; bgNode.scale = this._mapScaleMax;
},
this.che.scale = this._mapScaleMax;
// ------------------------------------------------ this.cloud.scale = this._mapScaleMax;
getSprNode(resName) { this.questionFrame.scale = this._mapScaleMin;
const sf = cc.find("Canvas/res/img/" + resName).getComponent(cc.Sprite).spriteFrame; this.question.node.scale = this._mapScaleMin;
const node = new cc.Node(); },
node.addComponent(cc.Sprite).spriteFrame = sf;
return node; // ------------------------------------------------
}, getSprNode(resName) {
const sf = cc.find("Canvas/res/img/" + resName).getComponent(cc.Sprite).spriteFrame;
getSpriteFrimeByUrl(url, cb) { const node = new cc.Node();
cc.loader.load({ url }, (err, img) => { node.addComponent(cc.Sprite).spriteFrame = sf;
const spriteFrame = new cc.SpriteFrame(img); return node;
if (cb) { },
cb(spriteFrame);
} getSpriteFrimeByUrl(url, cb) {
}); cc.loader.load({ url }, (err, img) => {
}, const spriteFrame = new cc.SpriteFrame(img);
if (cb) {
getSprNodeByUrl(url, cb) { cb(spriteFrame);
const node = new cc.Node(); }
const spr = node.addComponent(cc.Sprite); });
this.getSpriteFrimeByUrl(url, (sf) => { },
spr.spriteFrame = sf;
if (cb) { getSprNodeByUrl(url, cb) {
cb(node); const node = new cc.Node();
} const spr = node.addComponent(cc.Sprite);
}); this.getSpriteFrimeByUrl(url, (sf) => {
}, spr.spriteFrame = sf;
if (cb) {
playAudioByUrl(audio_url, cb = null) { cb(node);
if (audio_url) { }
cc.assetManager.loadRemote(audio_url, (err, audioClip) => { });
const audioId = cc.audioEngine.play(audioClip, false); },
if (cb) {
cc.audioEngine.setFinishCallback(audioId, () => { playAudioByUrl(audio_url, cb = null) {
cb(); if (audio_url) {
}); cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
} const audioId = cc.audioEngine.play(audioClip, false);
}); if (cb) {
} cc.audioEngine.setFinishCallback(audioId, () => {
}, cb();
});
stopAllAudio() { }
cc.audioEngine.stopAllEffects(); });
this._isPlayQuestion = false; }
this._isPlayTitle = false; },
},
stopAllAudio() {
// ------------------------------------------ cc.audioEngine.stopAllEffects();
this._isPlayQuestion = false;
this._isPlayTitle = false;
},
// ------------------------------------------
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment