五、实现关卡相关内容:
- 在 SceneLevel.ts 文件,在 init 方法尾部继续
var mileStone: number = LevelDataManager.Shared().MileStone;
for(var i: number = 0; i < LevelDataManager.Shared().getLevelsNum(); i++) {
var icon = new LevelIcon();
group.addChild(icon);
icon.Level = i + 1;
icon.x = Math.sin(spanY*i/2/180*Math.PI)*200 + group.width/2;
icon.y = group.height - spanY * i - icon.height;
icon.addEventListener(
egret.TouchEvent.TOUCH_TAP,
this.icon_tap,
this
);
icon.enabled = i < mileStone;
this.levelIcon.push(icon);
}
private icon_tap() { }
- 来到 LevelIcon.exml 来设置,调整 up / down / disabled 三种状态的图片 image;
- 目前存在两个问题:1. 不能让用户往下滑到最底,要自动;2. 箭头没有指向当前关卡;
- 解决第一个问题:来到 SceneLevel.ts 中,在 init 方法里:
this.group_levels.scrollV = group.height - this.height;
this.img_arrow.anchorOffsetX = this.img_arrow.width/2;
this.img_arrow.anchorOffsetY = this.img_arrow.height;
this.img_arrow.touchEnabled = false;
var currentIcon = group.getChildAt(mileStone - 1);
this.img_arrow.x = currentIcon.x + currentIcon.width;
this.img_arrow.y = currentIcon.y;
this.sel_level = mileStone;
this.group_levels.addChild(this.img_arrow);
private icon_tap(e: egret.TouchEvent) {
var icon = <LevelIcon>e.currentTarget;
if(this.sel_level != icon.Level) {
this.img_arrow.x = icon.x + icon.width/2;
this.img_arrow.y = icon.y;
this.sel_level = icon.Level;
}else{
}
}
六、游戏场景:
-
创建游戏场景 EUI 组件: -
声明单例类,在 SceneGame.ts 中:
private static shared: SceneGame;
public static Shared(): SceneGame {
if(SceneGame.shared == null) {
SceneGame.shared = new SceneGame();
}
return SceneGame.shared;
}
public initLevel(level: number) { }
-
在 SceneLevel.ts 中传入关卡数组下标: -
构建 SceneGame.exml: -
构建 Word 组件:可选字体及答案; -
打开 Word.exml,拖入 rect 组件,设置宽高为 80,约束为填充上下左右; -
设置圆角; -
text 的 ID 设置为 lb_text; -
复制 label 组件的“复制自定义”到 Word.ts 中:
public lb_text:eui.Label;
protected childrenCreated():void {
super.childrenCreated();
this.init();
}
private init() {
this.lb_text.addEventListener(
egret.TouchEvent.TOUCH_TAP,
this.word_tap,
this
);
}
protected word_tap() {
SceneGame.Shared().word_click(this);
}
public setWordText(val: string) {
this.lb_text.text = val;
}
public getWordText(): string {
return this.lb_text.text;
}
-
在 SceneGame.exml 中 拖入Group 布局 设置宽高: -
在自定义组件库里面拖入 word 组件,复制 19 个出来,设置布局为网格布局: -
设置间隙和对齐方式: -
设置 ID 为:group_words -
添加一个答案区域的 group 组件; -
设置宽高,并设置 ID 为 group_answer; -
在 group 组件中添加一个 answerword 自定义组件,设置如下; -
复制三个,宽高即 y 不变,x 分别为18,103,189,277; -
编写游戏场景,在 SceneGame.exml 选中复制自定义到 SceneGame.ts,声明一个变量,用来表示所处的关卡
public btnBack:eui.Button;
public imgQuestion:eui.Image;
public group_words:eui.Group;
public group_answer:eui.Group;
private levelIndex: number = 0;
public initLevel(level: number) {
this.levelIndex = level;
var levelData = LevelDataManager.Shared().getLevel(level);
var words = levelData.answer + levelData.word;
while(words.length == 10) {
var i = Math.floor(Math.random()*LevelDataManager.Shared().getLevelsNum());
if(i != level) {
var temp = LevelDataManager.Shared().getLevel(i);
}
words += temp.answer + temp.word;
}
var wordList: string[] = [];
for(var i: number = 0; i < words.length; i++) {
wordList.push(words.charAt(i));
}
wordList = this.randomList(wordList);
for(var i: number = 0; i < this.group_words.numChildren; i++) {
var word = <Word>this.group_words.getChildAt(i);
word.setWordText(wordList[i]);
word.visible = true;
}
for(var i: number = 0; i < this.group_answer.numChildren; i++) {
var answer = <AnswerWord>this.group_answer.getChildAt(i);
answer.SetSelectWord(null);
answer.visible = true;
answer.SelectWord = null;
}
this.imgQuestion.source = "resource/assets/" + levelData.img;
}
private randomList(arr: string[]):string[] {
var array = [];
while(arr.length > 0) {
var i = Math.floor(Math.random()*arr.length);
array.push(arr[i]);
arr.splice(i,1);
}
return array;
}
public word_click(word: Word) {
var sel: AnswerWord = null;
for(var i: number = 0; i < this.group_answer.numChildren; i++) {
var answer = <AnswerWord>this.group_answer.getChildAt(i);
if(answer.SelectWord == null) {
sel = answer;
break;
}
}
if(sel) {
sel.SetSelectWord(word);
var check_str : string = '';
for(var i: number = 0; i < this.group_answer.numChildren; i++) {
var answer = <AnswerWord>this.group_answer.getChildAt(i);
check_str += answer.getWordText();
}
if(check_str == LevelDataManager.Shared().getLevel(this.levelIndex).answer) {
this.showWin();
}
}
}
private showWin() {
console.log('答案正确!');
}
|