Scene.js 8.76 KB
Newer Older
xytk's avatar
xytk committed
1 2 3
import Tittle from "./Tittle"
import Middle from "./Middle"
import Bottom from "./Bottom"
Li Mingzhe's avatar
Li Mingzhe committed
4 5 6 7 8 9 10


cc.Class({

	extends: cc.Component,

	properties: {
xytk's avatar
xytk committed
11
		loadInAudioSource:cc.AudioSource,
xytk's avatar
xytk committed
12 13
		dragonBonesNode:cc.Node,
		pic_a_sprite:cc.Sprite,
xytk's avatar
xytk committed
14 15 16 17
		pic_c_sprite:cc.Sprite,
		tittleJs:Tittle,
		middleJs:Middle,
		bottomJs:Bottom
xytk's avatar
xytk committed
18

Li Mingzhe's avatar
Li Mingzhe committed
19 20 21 22 23 24
	},

	// 生命周期 onLoad
	onLoad() {
		this.initSceneData();
		this.initSize();
xytk's avatar
xytk committed
25
		this.loadInAudioSource.play();
Li Mingzhe's avatar
Li Mingzhe committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	},

	_imageResList: null,
	_audioResList: null,
	_animaResList: null,
	initSceneData() {
		this._imageResList = [];
		this._audioResList = [];
		this._animaResList = [];
	},

	_designSize: null,  // 设计分辨率
	_frameSize: null,  // 屏幕分辨率
	_mapScaleMin: null,  // 场景中常用缩放(取大值)
	_mapScaleMax: null,  // 场景中常用缩放(取小值)
	_cocosScale: null,  // cocos 自缩放 (较少用到)
	initSize() {

		// 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小
		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 f = screen_size >= design_size
		cc.Canvas.instance.fitHeight = f
		cc.Canvas.instance.fitWidth = !f

		const frameSize = cc.view.getFrameSize();
		this._frameSize = frameSize;

		this._designSize = cc.view.getDesignResolutionSize();

		let sx = cc.winSize.width / frameSize.width;
		let sy = cc.winSize.height / frameSize.height;
		this._cocosScale = Math.min(sx, sy);

		sx = frameSize.width / this._designSize.width;
		sy = frameSize.height / this._designSize.height;
		this._mapScaleMin = Math.min(sx, sy) * this._cocosScale;
		this._mapScaleMax = Math.max(sx, sy) * this._cocosScale;
	},


	// 生命周期 start
范雪寒's avatar
范雪寒 committed
68
	start() {
Li Mingzhe's avatar
Li Mingzhe committed
69 70 71 72
		let getData = this.getData.bind(this);
		if (window && window.courseware) {
			getData = window.courseware.getData;
		}
xytk's avatar
xytk committed
73
		this.dragonBonesNode.on(cc.Node.EventType.TOUCH_START,this.playDragonAnimation,this)
Li Mingzhe's avatar
Li Mingzhe committed
74
		getData((data) => {
xytk's avatar
xytk committed
75
			cc.log('data:', data);
Li Mingzhe's avatar
Li Mingzhe committed
76 77 78 79 80 81
			this.data = data || this.getDefaultData();
			this.data = JSON.parse(JSON.stringify(this.data))
			this.preloadItem()
		})
	},

xytk's avatar
xytk committed
82 83 84
    playDragonAnimation(){
        let ad = this.dragonBonesNode.getComponent(dragonBones.ArmatureDisplay);
        if(ad){
xytk's avatar
xytk committed
85
            ad.playAnimation("normal", -1);
xytk's avatar
xytk committed
86 87 88 89
        }

    },

Li Mingzhe's avatar
Li Mingzhe committed
90 91 92 93 94
	getData(cb) {
		cb(this.getDefaultData());
	},

	getDefaultData() {
xytk's avatar
xytk committed
95
		const dataJson = '{"bt_text":"11111","bt_audio_url":"http://staging-teach.cdn.ireadabc.com/b0c23eb327ec13212d09516a1a8a49ec.mp3","dragonBone":{"skeJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/14ca834d522122b031a2e18be5129251.json","name":"sb_boy_ske.json"},"texJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/ecc48e23d55b1af8eb40d997dc573791.json","name":"sb_boy_tex.json"},"texPngData":{"url":"http://staging-teach.cdn.ireadabc.com/2d668f8049bc98c0e026921ef316a0fd.png","name":"sb_boy_tex.png"}},"dragonBone_audio_url":"http://staging-teach.cdn.ireadabc.com/b0c23eb327ec13212d09516a1a8a49ec.mp3","pic_A_url":"http://staging-teach.cdn.ireadabc.com/79f4b6912d1ffad13da6c05dda328237.jpg","pic_C_url":"http://staging-teach.cdn.ireadabc.com/79f4b6912d1ffad13da6c05dda328237.jpg","bottom_text":"底部的文本","bottom_audio_url":"http://staging-teach.cdn.ireadabc.com/b0c23eb327ec13212d09516a1a8a49ec.mp3","pic_A_audio_url":"http://staging-teach.cdn.ireadabc.com/b0c23eb327ec13212d09516a1a8a49ec.mp3","pic_C_audio_url":"http://staging-teach.cdn.ireadabc.com/b0c23eb327ec13212d09516a1a8a49ec.mp3","dragonBone_animationName":"unit1_boy_jump","dragonBone_armatureName":"Armature"}'
Li Mingzhe's avatar
Li Mingzhe committed
96 97 98
		const data = JSON.parse(dataJson);
		return data;
	},
范雪寒's avatar
范雪寒 committed
99

Li Mingzhe's avatar
Li Mingzhe committed
100 101 102 103 104 105 106 107 108
	preloadItem() {
		this.addPreloadImage();
		this.addPreloadAudio();
		this.addPreloadAnima();
		this.preload();
	},


	addPreloadImage() {
xytk's avatar
xytk committed
109 110 111
		this._imageResList.push({ url: this.data.pic_A_url });
		this._imageResList.push({ url: this.data.pic_C_url });
		this._imageResList.push({ url: this.data.dragonBone.texPngData.url });
Li Mingzhe's avatar
Li Mingzhe committed
112 113 114
	},

	addPreloadAudio() {
xytk's avatar
xytk committed
115 116 117 118 119
		this._audioResList.push({ url: this.data.bt_audio_url });
		this._audioResList.push({ url: this.data.dragonBone_audio_url });
		this._audioResList.push({ url: this.data.pic_A_audio_url });
		this._audioResList.push({ url: this.data.pic_C_audio_url });
		this._audioResList.push({ url: this.data.bottom_audio_url });
Li Mingzhe's avatar
Li Mingzhe committed
120 121 122
	},

	addPreloadAnima() {
xytk's avatar
xytk committed
123 124 125
		this._animaResList.push({url: this.data.dragonBone.skeJsonData.url});
		this._animaResList.push({url: this.data.dragonBone.texJsonData.url});
		this._animaResList.push({url: this.data.dragonBone.texPngData.url});
Li Mingzhe's avatar
Li Mingzhe committed
126 127 128 129 130 131 132 133 134 135 136
	},

	preload() {

		const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList);
		cc.assetManager.loadAny(preloadArr, null, null, (err, data) => {

			this.loadEnd();
			if (window && window["air"]) {
				window["air"].hideAirClassLoading();
			}
范雪寒's avatar
范雪寒 committed
137

Li Mingzhe's avatar
Li Mingzhe committed
138 139 140 141 142 143 144 145 146 147
			cc.debug.setDisplayStats(false);
		});
	},


	loadEnd() {
		this.initData();
		this.initAudio();
		this.initView();
		// this.initListener();
xytk's avatar
xytk committed
148 149 150



Li Mingzhe's avatar
Li Mingzhe committed
151 152 153 154 155 156 157
	},

	_cantouch: null,
	initData() {
		// 所有全局变量 默认都是null 
		this._cantouch = true;
	},
范雪寒's avatar
范雪寒 committed
158

Li Mingzhe's avatar
Li Mingzhe committed
159 160
	audioBtn: null,
	initAudio() {
xytk's avatar
xytk committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
		this.getAudioByUrl(this.data.bt_audio_url,(ac)=>{
			this.tittleJs.setAudioClip(ac);
		});
		this.getAudioByUrl(this.data.bottom_audio_url,(ac)=>{
			this.bottomJs.setAudioClip(ac);
		});
		this.getAudioByUrl(this.data.dragonBone_audio_url,(ac)=>{
			this.middleJs.setAudioClip(ac,0);
		});
		this.getAudioByUrl(this.data.pic_A_audio_url,(ac)=>{
			this.middleJs.setAudioClip(ac,1);
		});
		this.getAudioByUrl(this.data.pic_C_audio_url,(ac)=>{
			this.middleJs.setAudioClip(ac,2);
		});
Li Mingzhe's avatar
Li Mingzhe committed
176 177 178 179 180 181
	},


	initView() {

		this.initBg();
xytk's avatar
xytk committed
182
		this.initPic();
xytk's avatar
xytk committed
183
		//this.initBtn();
Li Mingzhe's avatar
Li Mingzhe committed
184
		this.initIcon();
xytk's avatar
xytk committed
185
		this.loadDragonBones();
Li Mingzhe's avatar
Li Mingzhe committed
186
	},
xytk's avatar
xytk committed
187
	loadDragonBones(){
xytk's avatar
xytk committed
188
		var animNode = this.dragonBonesNode;
xytk's avatar
xytk committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		var dragonDisplay = animNode.addComponent(dragonBones.ArmatureDisplay);

		var image = this.data.dragonBone.texPngData.url;
		var ske = this.data.dragonBone.skeJsonData.url;
		var atlas = this.data.dragonBone.texJsonData.url;
		var item = this.data;
		cc.loader.load(image, (error, texture) => {
			cc.loader.load({ url: atlas, type: 'txt' }, (error, atlasJson) => {
				cc.loader.load({ url: ske, type: 'txt' }, (error, dragonBonesJson) => {
					var atlas = new dragonBones.DragonBonesAtlasAsset();
					atlas.atlasJson = atlasJson;
					atlas.texture = texture;

					var asset = new dragonBones.DragonBonesAsset();
					asset.dragonBonesJson = dragonBonesJson;

					dragonDisplay.dragonAtlasAsset = atlas;
					dragonDisplay.dragonAsset = asset;
xytk's avatar
xytk committed
207 208 209 210
					let dbj = JSON.parse(dragonBonesJson);
					let armatureNames = dbj.armature.map(data => data.name);
					if(armatureNames.length>0){
						dragonDisplay.armatureName = armatureNames[0];
xytk's avatar
xytk committed
211

xytk's avatar
xytk committed
212
					}
xytk's avatar
xytk committed
213 214 215 216 217
					// dragonDisplay.playAnimation(item.dragonBone_animationName, 0);
				});
			});
		});

Li Mingzhe's avatar
Li Mingzhe committed
218

xytk's avatar
xytk committed
219
	},
Li Mingzhe's avatar
Li Mingzhe committed
220 221 222 223 224 225 226 227
	initBg() {
		const bgNode = cc.find('Canvas/bg');
		bgNode.scale = this._mapScaleMax;
	},

	pic1: null,
	pic2: null,
	initPic() {
xytk's avatar
xytk committed
228
		this.getSpriteFrimeByUrl(this.data.pic_A_url,(spriteFrame)=>{
xytk's avatar
xytk committed
229 230
			let width = this.pic_a_sprite.node.width;
			let height = this.pic_a_sprite.node.height;
xytk's avatar
xytk committed
231
			this.pic_a_sprite.spriteFrame = spriteFrame;
xytk's avatar
xytk committed
232 233
			this.pic_a_sprite.node.width = width;
			this.pic_a_sprite.node.height = height;
Li Mingzhe's avatar
Li Mingzhe committed
234
		});
xytk's avatar
xytk committed
235
		this.getSpriteFrimeByUrl(this.data.pic_C_url,(spriteFrame)=>{
xytk's avatar
xytk committed
236 237
			let width = this.pic_c_sprite.node.width;
			let height = this.pic_c_sprite.node.height;
xytk's avatar
xytk committed
238
			this.pic_c_sprite.spriteFrame = spriteFrame;
xytk's avatar
xytk committed
239 240
			this.pic_c_sprite.node.width = width;
			this.pic_c_sprite.node.height = height;
Li Mingzhe's avatar
Li Mingzhe committed
241 242 243 244
		});

	},

xytk's avatar
xytk committed
245
	initIcon() {},
Li Mingzhe's avatar
Li Mingzhe committed
246 247

	curPage: null,
xytk's avatar
xytk committed
248
	initBtn() {	},
Li Mingzhe's avatar
Li Mingzhe committed
249

xytk's avatar
xytk committed
250
	leftMove() {},
Li Mingzhe's avatar
Li Mingzhe committed
251 252 253

	rightMove() {
	},
范雪寒's avatar
范雪寒 committed
254 255
	// update (dt) {},

Li Mingzhe's avatar
Li Mingzhe committed
256 257 258 259 260 261 262 263 264 265
	// ------------------------------------------------
	getSprNode(resName) {
		const sf = cc.find('Canvas/res/img/' + resName).getComponent(cc.Sprite).spriteFrame;
		const node = new cc.Node();
		node.addComponent(cc.Sprite).spriteFrame = sf;
		return node;
	},


	getSpriteFrimeByUrl(url, cb) {
范雪寒's avatar
范雪寒 committed
266
		cc.loader.load({ url }, (err, img) => {
Li Mingzhe's avatar
Li Mingzhe committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			const spriteFrame = new cc.SpriteFrame(img)
			if (cb) {
				cb(spriteFrame);
			}
		})
	},

	getSprNodeByUrl(url, cb) {
		const node = new cc.Node();
		const spr = node.addComponent(cc.Sprite);
		this.getSpriteFrimeByUrl(url, (sf) => {
			spr.spriteFrame = sf;
			if (cb) {
				cb(node);
			}
		})
	},

xytk's avatar
xytk committed
285 286 287 288 289 290 291 292 293 294
	getAudioByUrl(audio_url, cb = null) {
		if (audio_url) {
			cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
				if (cb) {
					cb(audioClip);
				}
			});
		}
	},

范雪寒's avatar
范雪寒 committed
295
	playAudioByUrl(audio_url, cb = null) {
Li Mingzhe's avatar
Li Mingzhe committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		if (audio_url) {
			cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
				const audioId = cc.audioEngine.play(audioClip, false, 0.8);
				if (cb) {
					cc.audioEngine.setFinishCallback(audioId, () => {
						cb();
					});
				}
			});
		}
	},

	// ------------------------------------------

});