Commit 0a84505c authored by Tt's avatar Tt

双指缩放部分代码整理

parent eefb3126
...@@ -10,23 +10,12 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -10,23 +10,12 @@ export default class SceneComponent extends MyCocosSceneComponent {
addPreloadImage() { addPreloadImage() {
// TODO 根据自己的配置预加载图片资源 // TODO 根据自己的配置预加载图片资源
let arr = []; let arr = [];
// this.data.questions.forEach(question => {
// arr.push({ url: question.bgItem.url })
// question.hotZoneItemArr.forEach(zone => {
// })
// })
// this._imageResList.push(...arr); // this._imageResList.push(...arr);
} }
addPreloadAudio() { addPreloadAudio() {
// TODO 根据自己的配置预加载音频资源 // TODO 根据自己的配置预加载音频资源
let arr = []; let arr = [];
// this.data.questions.forEach(question => {
// question.hotZoneItemArr.forEach(zone => {
// arr.push({ url: zone.audio_url })
// })
// })
// this._audioResList.push(...arr); // this._audioResList.push(...arr);
} }
...@@ -55,7 +44,8 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -55,7 +44,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
layout_finish: cc.Node = null; layout_finish: cc.Node = null;
@property(cc.Node) @property(cc.Node)
card_node: cc.Node = null; card_node: cc.Node = null;
private openDouble: boolean;
private openSingle: boolean; private openSingle: boolean;
private openPlay: boolean; private openPlay: boolean;
private openShowVoice: boolean; private openShowVoice: boolean;
...@@ -63,17 +53,15 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -63,17 +53,15 @@ export default class SceneComponent extends MyCocosSceneComponent {
initData() { initData() {
Game.getIns().init(this.data); Game.getIns().init(this.data);
Game.getIns().reset({ w: this.layout_game.width, h: this.layout_game.height }); Game.getIns().reset({ w: this.layout_game.width, h: this.layout_game.height });
this.openDouble = false; this.hideDouble();
this.openSingle = false; this.openSingle = false;
this.openPlay = false; this.openPlay = false;
this.readCount = 0; this.readCount = 0;
this.scale = 1; this.scale = 1;
this.lastMove = cc.v2(0, 0) this.lastMove = cc.v2(0, 0)
} }
private audioId: any;
async initView() { async initView() {
let btns = pg.view.find(this, 'btns'); this.updateDoubleBtns();
pg.view.visible(pg.view.find(btns, 'btn_double_1'), false);
this.initQuestions(); this.initQuestions();
} }
exitGame() { exitGame() {
...@@ -90,16 +78,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -90,16 +78,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.view.touchOn(pg.view.find(this, 'btn_return'), this.exitGame, this); pg.view.touchOn(pg.view.find(this, 'btn_return'), this.exitGame, this);
pg.view.touchOn(pg.view.find(btns, 'btn_next_1'), this.onTouchBtnNext, this); pg.view.touchOn(pg.view.find(btns, 'btn_next_1'), this.onTouchBtnNext, this);
pg.view.touchOn(pg.view.find(btns, 'btn_pre_1'), this.onTouchBtnPre, this); pg.view.touchOn(pg.view.find(btns, 'btn_pre_1'), this.onTouchBtnPre, this);
pg.view.touchOn(pg.view.find(btns, 'btn_double_0'), this.onTouchBtnDouble, this); this.initFingerDouble();
pg.view.touchOn(pg.view.find(btns, 'btn_double_1'), this.onTouchBtnDouble, this);
pg.view.touchOn(pg.view.find(btns, 'btn_single_0'), this.onTouchBtnSingle, this); pg.view.touchOn(pg.view.find(btns, 'btn_single_0'), this.onTouchBtnSingle, this);
pg.view.touchOn(pg.view.find(btns, 'btn_single_1'), this.onTouchBtnSingle, this); pg.view.touchOn(pg.view.find(btns, 'btn_single_1'), this.onTouchBtnSingle, this);
pg.view.touchOn(pg.view.find(btns, 'btn_play_0'), this.onTouchBtnPlay, this); pg.view.touchOn(pg.view.find(btns, 'btn_play_0'), this.onTouchBtnPlay, this);
pg.view.touchOn(pg.view.find(btns, 'btn_play_1'), this.onTouchBtnPlay, this); pg.view.touchOn(pg.view.find(btns, 'btn_play_1'), this.onTouchBtnPlay, this);
// 触摸监听
pg.view.find(btns, 'panel_double').on('touchstart', this.onTouchPanelStart, this);
pg.view.find(btns, 'panel_double').on('touchmove', this.onTouchPanelMove, this);
pg.view.find(btns, 'panel_double').on('touchend', this.onTouchPanelEnd, this);
} }
initQuestions() { initQuestions() {
// 直接渲染多个页面 // 直接渲染多个页面
...@@ -118,7 +101,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -118,7 +101,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
gameStart(isPlayAudio = true) { gameStart(isPlayAudio = true) {
this.updateTouch(); this.updateTouch();
this.openDouble = false; this.hideDouble();
this.openSingle = false; this.openSingle = false;
this.onStopAllAudio(); this.onStopAllAudio();
// this.onPlayAllAudio(); // this.onPlayAllAudio();
...@@ -126,6 +109,133 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -126,6 +109,133 @@ export default class SceneComponent extends MyCocosSceneComponent {
private gameOver() { private gameOver() {
onHomeworkFinish({}) onHomeworkFinish({})
} }
//-------------------------------------双指缩放------------------------------
private openDouble: boolean;
showDouble() {
this.openDouble = true;
}
hideDouble() {
this.openDouble = false;
}
// 初始化双指操作
initFingerDouble() {
let btns = pg.view.find(this, 'btns');
pg.view.touchOn(pg.view.find(btns, 'btn_double_0'), this.onTouchBtnDouble, this);
pg.view.touchOn(pg.view.find(btns, 'btn_double_1'), this.onTouchBtnDouble, this);
// 触摸监听
pg.view.find(btns, 'panel_double').on('touchstart', this.onTouchPanelStart, this);
pg.view.find(btns, 'panel_double').on('touchmove', this.onTouchPanelMove, this);
pg.view.find(btns, 'panel_double').on('touchend', this.onTouchPanelEnd, this);
}
// 点击双指触摸开关
onTouchBtnDouble() {
//允许双指操作
this.openDouble = !this.openDouble;
if (this.openDouble) {
this.openSingle = false;
this.onStopAllAudio();
this.updateTips('开启手动缩放模式');
}
}
// 手指1
private startPos1: any;
// 手指2
private startPos2: any;
// 手指间距
private pointsDis: any;
// 开始触碰
onTouchPanelStart(event) {
//双指操作部分
let touches = event.getTouches();
if (touches.length == 1) {
// 一根手指是移动,这里不用写任何代码
let node = pg.view.find(this, 'layout_game/show_area');
this.nodeStartPos = cc.v2(node);
} else if (touches.length == 2) {
this.startPos1 = this.node.convertToNodeSpaceAR(touches[0].getLocation());
this.startPos2 = this.node.convertToNodeSpaceAR(touches[1].getLocation());
this.pointsDis = this.startPos1.sub(this.startPos2).mag();
}
}
scale: number;
lastMove: cc.Vec2;
nodeStartPos: cc.Vec2;
touchTwo: boolean;
// 手指移动 【放大/缩小】
onTouchPanelMove(event) {
let touches = event.getTouches();
if (touches.length == 1) {// 单指位移
if (this.touchTwo) return;
let startPos = touches[0].getStartLocation()
let nowPos = touches[0].getLocation()
let movePos = cc.v2(nowPos.x - startPos.x, nowPos.y - startPos.y);
let targetPos = cc.v2(movePos.x + this.nodeStartPos.x, movePos.y + this.nodeStartPos.y)
this.updateAreaPosition(targetPos);
} else if (touches.length == 2) {//双指缩放
this.touchTwo = true;
// 两根手指是缩放
let touchPoint1 = this.node.convertToNodeSpaceAR(touches[0].getLocation());
let touchPoint2 = this.node.convertToNodeSpaceAR(touches[1].getLocation());
let newPointsDis = touchPoint1.sub(touchPoint2).mag();
if (!this.pointsDis) // 该行代码针对安卓手机
this.pointsDis = 0;
// 表明两根手指在往外划
if (newPointsDis > this.pointsDis) {
this.pointsDis = newPointsDis;
this.scale += 0.05;
}
else if (newPointsDis < this.pointsDis) {
this.pointsDis = newPointsDis;
this.scale -= 0.05;
}
//执行缩放程序
this.updateAreaScale(this.scale);
}
}
// 触摸结束最后
onTouchPanelEnd(event) {
// 缩放过小,自动回弹
if (this.scale <= 1) {
this.scale = 1;
this.updateAreaPosition();
this.updateAreaScale();
}
// 回归触摸状态
this.scheduleOnce(() => {
this.touchTwo = false;
}, 0.1)
}
// 更新显示区域和触摸区域的回弹
updateAreaScale(scale = 1) {
let show_area = pg.view.find(this, 'layout_game/show_area');
let touch_area = pg.view.find(this, 'layout_game/touch_area');
show_area.scale = scale;
touch_area.scale = scale;
}
// 更新显示区域和触摸区域的重置 【此处有特殊的pos计算内容】
updateAreaPosition(position = cc.v2(0, 0)) {
let show_area = pg.view.find(this, 'layout_game/show_area');
let touch_area = pg.view.find(this, 'layout_game/touch_area');
show_area.x = position.x;
show_area.y = position.y;
touch_area.x = position.x - 643.196;
touch_area.y = position.y + 476.596;
}
// 更新按钮及面板状态
updateDoubleBtns() {
let btns = pg.view.find(this, 'btns');
pg.view.visible(pg.view.find(btns, 'btn_double_0'), !this.openDouble);
pg.view.visible(pg.view.find(btns, 'btn_double_1'), this.openDouble);
pg.view.visible(pg.view.find(btns, 'panel_double'), this.openDouble);
}
//--------------------------------------UPDATE-------------------------------- //--------------------------------------UPDATE--------------------------------
update(dt) { update(dt) {
let btns = pg.view.find(this, 'btns'); let btns = pg.view.find(this, 'btns');
...@@ -137,9 +247,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -137,9 +247,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.view.visible(pg.view.find(btns, 'btn_play_1'), this.openPlay); pg.view.visible(pg.view.find(btns, 'btn_play_1'), this.openPlay);
pg.view.visible(pg.view.find(btns, 'btn_play_2'), this.openPlay && !this.pauseing); pg.view.visible(pg.view.find(btns, 'btn_play_2'), this.openPlay && !this.pauseing);
pg.view.visible(pg.view.find(btns, 'btn_double_0'), !this.openDouble); this.updateDoubleBtns();
pg.view.visible(pg.view.find(btns, 'btn_double_1'), this.openDouble);
pg.view.visible(pg.view.find(btns, 'panel_double'), this.openDouble);
pg.view.visible(pg.view.find(btns, 'btn_single_0'), !this.openSingle); pg.view.visible(pg.view.find(btns, 'btn_single_0'), !this.openSingle);
pg.view.visible(pg.view.find(btns, 'btn_single_1'), this.openSingle); pg.view.visible(pg.view.find(btns, 'btn_single_1'), this.openSingle);
...@@ -238,7 +346,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -238,7 +346,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.event.emit('page_to_left', Game.getIns().getCurrentPageNum()); pg.event.emit('page_to_left', Game.getIns().getCurrentPageNum());
Game.getIns().addPage(); Game.getIns().addPage();
this.updateTouch(); this.updateTouch();
this.openDouble = false; this.hideDouble();
this.openSingle = false; this.openSingle = false;
// this.onPlayAllAudio(); // this.onPlayAllAudio();
} }
...@@ -250,7 +358,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -250,7 +358,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.playLocalAudio('page'); this.playLocalAudio('page');
pg.event.emit('page_to_right', Game.getIns().getCurrentPageNum()); pg.event.emit('page_to_right', Game.getIns().getCurrentPageNum());
this.updateTouch(); this.updateTouch();
this.openDouble = false; this.hideDouble();
this.openSingle = false; this.openSingle = false;
// this.onPlayAllAudio(); // this.onPlayAllAudio();
} }
...@@ -296,22 +404,13 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -296,22 +404,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.view.visible(pg.view.find(item, 'item_laba'), false) pg.view.visible(pg.view.find(item, 'item_laba'), false)
} }
} }
onTouchBtnDouble() {
//允许双指操作
this.openDouble = !this.openDouble;
if (this.openDouble) {
this.openSingle = false;
this.onStopAllAudio();
this.updateTips('开启手动缩放模式');
}
}
onTouchBtnSingle() { onTouchBtnSingle() {
this.onStopAllAudio(); this.onStopAllAudio();
//允许点击操作 //允许点击操作
this.openSingle = !this.openSingle; this.openSingle = !this.openSingle;
if (this.openSingle) { if (this.openSingle) {
this.openShowVoice = true; this.openShowVoice = true;
this.openDouble = false; this.hideDouble();
this.updateAreaScale(); this.updateAreaScale();
this.updateAreaPosition(cc.v2(0, 0)); this.updateAreaPosition(cc.v2(0, 0));
this.updateTips('开启课文点读模式'); this.updateTips('开启课文点读模式');
...@@ -331,7 +430,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -331,7 +430,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} else { } else {
this.updateTips('开始连续播放音频'); this.updateTips('开始连续播放音频');
this.openDouble = false; this.hideDouble();
this.updateAreaScale(); this.updateAreaScale();
this.updateAreaPosition(cc.v2(0, 0)); this.updateAreaPosition(cc.v2(0, 0));
this.openSingle = false; this.openSingle = false;
...@@ -377,92 +476,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -377,92 +476,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.onStopAudio(); this.onStopAudio();
this.stopRoll(); this.stopRoll();
} }
startPos1: any;
startPos2: any;
pointsDis: any;
onTouchPanelStart(event) {
//双指操作部分
let touches = event.getTouches();
if (touches.length == 1) {
// 一根手指是移动,这里不用写任何代码
let node = pg.view.find(this, 'layout_game/show_area');
this.nodeStartPos = cc.v2(node);
} else if (touches.length == 2) {
this.startPos1 = this.node.convertToNodeSpaceAR(touches[0].getLocation());
this.startPos2 = this.node.convertToNodeSpaceAR(touches[1].getLocation());
this.pointsDis = this.startPos1.sub(this.startPos2).mag();
}
}
scale: number;
lastMove: cc.Vec2;
nodeStartPos: cc.Vec2;
touchTwo: boolean;
onTouchPanelMove(event) {
//双指操作部分
let touches = event.getTouches();
if (touches.length == 1) {
if (this.touchTwo) return;
let startPos = touches[0].getStartLocation()
let nowPos = touches[0].getLocation()
let movePos = cc.v2(nowPos.x - startPos.x, nowPos.y - startPos.y);
let targetPos = cc.v2(movePos.x + this.nodeStartPos.x, movePos.y + this.nodeStartPos.y)
this.updateAreaPosition(targetPos);
} else if (touches.length == 2) {
this.touchTwo = true;
// 两根手指是缩放
let touchPoint1 = this.node.convertToNodeSpaceAR(touches[0].getLocation());
let touchPoint2 = this.node.convertToNodeSpaceAR(touches[1].getLocation());
let newPointsDis = touchPoint1.sub(touchPoint2).mag();
if (!this.pointsDis) // 该行代码针对安卓手机
this.pointsDis = 0;
if (newPointsDis > this.pointsDis) {
// 表明两根手指在往外划
this.pointsDis = newPointsDis;
this.scale += 0.05;
}
else if (newPointsDis < this.pointsDis) {
this.pointsDis = newPointsDis;
this.scale -= 0.05;
}
//执行缩放程序
this.updateAreaScale(this.scale);
}
}
onTouchPanelEnd(event) {
let touches = event.getTouches();
// 表明两根手指在往内划
if (this.scale <= 1) {
this.scale = 1;
this.updateAreaPosition();
this.updateAreaScale();
}
this.scheduleOnce(() => {
this.touchTwo = false;
}, .1)
if (touches.length == 1) {
} else if (touches.length == 2) {
}
}
updateAreaScale(scale = 1) {
let show_area = pg.view.find(this, 'layout_game/show_area');
let touch_area = pg.view.find(this, 'layout_game/touch_area');
show_area.scale = scale;
touch_area.scale = scale;
}
updateAreaPosition(position = cc.v2(0, 0)) {
let show_area = pg.view.find(this, 'layout_game/show_area');
let touch_area = pg.view.find(this, 'layout_game/touch_area');
show_area.x = position.x;
show_area.y = position.y;
touch_area.x = position.x - 643.196;
touch_area.y = position.y + 476.596;
}
......
...@@ -648,7 +648,7 @@ let pg = { ...@@ -648,7 +648,7 @@ let pg = {
}); });
}) })
}, },
getAudioClip(audio_url) { getAudioClip(audio_url):Promise<cc.AudioClip> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!audio_url) return resolve(null); if (!audio_url) return resolve(null);
cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => { cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => {
......
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