添加道具功能
This commit is contained in:
@@ -4,9 +4,6 @@ const { regClass, property } = Laya;
|
||||
export class Block extends Laya.Script {
|
||||
declare owner : Laya.Button;
|
||||
|
||||
private label_show: Laya.Label;
|
||||
private tips_labels: Array<Laya.Label> = new Array();
|
||||
|
||||
@property(Number)
|
||||
public XIndex: number = 0;
|
||||
@property(Number)
|
||||
@@ -16,12 +13,23 @@ export class Block extends Laya.Script {
|
||||
@property(Number)
|
||||
public YGroup: number = 0;
|
||||
|
||||
private selected: boolean = false
|
||||
private preset: boolean = false//预设格子
|
||||
private checked: boolean = false//正确格子
|
||||
private showNumber: number = 0;
|
||||
private correctNumber: number = 0;
|
||||
private note_numbers: Array<number> = new Array()
|
||||
|
||||
private label_show: Laya.Label;
|
||||
private tips_labels: Array<Laya.Label> = new Array();
|
||||
private animator: Laya.Animator2D
|
||||
|
||||
public onInit(XIndex: number, YIndex: number, XGroup: number, YGroup: number, label_obj: Laya.Box, handler: any, func: any): void {
|
||||
this.XIndex = XIndex;
|
||||
this.YIndex = YIndex;
|
||||
this.XGroup = XGroup;
|
||||
this.YGroup = YGroup;
|
||||
|
||||
public onInit(label_obj: Laya.Box, XIndex: number, YIndex: number, XGroup: number, YGroup: number, handler: any, func: any): void {
|
||||
this.label_show = label_obj.getChildByName("label_show") as Laya.Label
|
||||
var VBox = label_obj.getChildByName("VBox")
|
||||
for (var i=0; i<VBox.numChildren; i++) {
|
||||
@@ -32,16 +40,27 @@ export class Block extends Laya.Script {
|
||||
this.tips_labels.push(Label)
|
||||
}
|
||||
}
|
||||
this.XIndex = XIndex;
|
||||
this.YIndex = YIndex;
|
||||
this.XGroup = XGroup;
|
||||
this.YGroup = YGroup;
|
||||
this.owner.on(Laya.Event.CLICK, handler, func);
|
||||
this.animator = this.owner.getComponent(Laya.Animator2D)
|
||||
}
|
||||
|
||||
public setSelected(selected: boolean): void {
|
||||
this.selected = selected
|
||||
this.updateBlock()
|
||||
}
|
||||
|
||||
public setButtonSkin(skin: string): void {
|
||||
this.owner.skin = skin
|
||||
}
|
||||
|
||||
public setFadeAnimation(): void {
|
||||
if (this.animator) {
|
||||
this.animator.play("fade_to_red")
|
||||
Laya.timer.frameOnce(40, this, ()=>{
|
||||
this.animator.stop()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public setBlock(preset: boolean, showNumber: number, correctNumber: number): void {
|
||||
this.preset = preset;
|
||||
@@ -63,18 +82,32 @@ export class Block extends Laya.Script {
|
||||
return this.showNumber;
|
||||
}
|
||||
|
||||
public getCorrectNumber(): number {
|
||||
return this.correctNumber;
|
||||
}
|
||||
|
||||
public setShowNumber(showNumber: number): boolean {
|
||||
console.log("setShowNumber >>>>>", showNumber, this.correctNumber, this.XIndex, this.YIndex)
|
||||
if (this.checked == false) {
|
||||
if (showNumber == this.correctNumber)
|
||||
this.checked = true;
|
||||
this.showNumber = showNumber;
|
||||
if (this.checked || showNumber == 0)//正确或者擦除
|
||||
this.note_numbers = new Array()
|
||||
this.updateBlock();
|
||||
}
|
||||
return this.checked
|
||||
}
|
||||
|
||||
public addNoteNumber(noteNumber: number): void {
|
||||
if (this.note_numbers.indexOf(noteNumber) < 0) {
|
||||
this.note_numbers.push(noteNumber)
|
||||
this.updateBlock();
|
||||
}
|
||||
}
|
||||
|
||||
updateBlock(): void {
|
||||
for (var i=0; i<this.tips_labels.length; i++)
|
||||
this.tips_labels[i].text = ""
|
||||
if (this.showNumber > 0) {
|
||||
this.label_show.text = this.showNumber.toString();
|
||||
if (this.checked) {
|
||||
@@ -86,6 +119,17 @@ export class Block extends Laya.Script {
|
||||
}
|
||||
else {
|
||||
this.label_show.text = "";
|
||||
for (var i=0; i<this.note_numbers.length; i++) {
|
||||
var value = this.note_numbers[i]
|
||||
var label = this.tips_labels[value-1]
|
||||
label.text = value.toString()
|
||||
if (this.selected) {
|
||||
label.color = "#ffffff"
|
||||
}
|
||||
else {
|
||||
label.color = "#000000"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,13 @@ export class Candy extends Laya.Script {
|
||||
private showNumber: number = 0;
|
||||
private left: number = 0;
|
||||
|
||||
public onInit(label_obj: Laya.Box, showNumber: number, handler: any, func: any): void {
|
||||
public onInit(showNumber: number, label_obj: Laya.Box, handler: any, func: any): void {
|
||||
this.showNumber = showNumber
|
||||
|
||||
this.label_show = label_obj.getChildByName("label") as Laya.Label
|
||||
this.label_left = label_obj.getChildByName("label_left") as Laya.Label
|
||||
this.showNumber = showNumber
|
||||
this.owner.on(Laya.Event.CLICK, handler, func)
|
||||
|
||||
this.updateCandy()
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,19 @@ export class Stage extends Laya.Script {
|
||||
public obj_candy_labels: Laya.Box;
|
||||
private candys: Array<Candy> = new Array();
|
||||
|
||||
@property(Laya.Box)
|
||||
public btn_undo: Laya.Box;
|
||||
@property(Laya.Box)
|
||||
public btn_erase: Laya.Box;
|
||||
@property(Laya.Box)
|
||||
public btn_note: Laya.Box;
|
||||
@property(Laya.Box)
|
||||
public btn_hint: Laya.Box;
|
||||
|
||||
private is_note_open: boolean = false
|
||||
private btn_note_normal: Laya.Box
|
||||
private btn_note_checked: Laya.Box
|
||||
|
||||
@property(Laya.Box)
|
||||
public obj_block_colors: Laya.Box;
|
||||
@property(Laya.Image)
|
||||
@@ -82,16 +95,26 @@ export class Stage extends Laya.Script {
|
||||
if (j==6 || j==7 || j==8)
|
||||
YGroup = 2
|
||||
var l_vbox = l_hbox.getChildAt(j) as Laya.Box
|
||||
block.onInit(l_vbox, i, j, XGroup, YGroup, this, this.onClickBlock)
|
||||
block.onInit(i, j, XGroup, YGroup, l_vbox, this, this.onClickBlock)
|
||||
}
|
||||
}
|
||||
for (var i=0; i<this.obj_candys.numChildren; i++) {
|
||||
var candy = this.obj_candys.getChildAt(i).getComponent(Candy);
|
||||
this.candys.push(candy);
|
||||
var box = this.obj_candy_labels.getChildAt(i) as Laya.Box
|
||||
candy.onInit(box, i+1, this, this.onClickCandy)
|
||||
candy.onInit(i+1, box, this, this.onClickCandy)
|
||||
}
|
||||
|
||||
this.btn_undo.on(Laya.Event.CLICK, this, this.onClickUndo)
|
||||
this.btn_erase.on(Laya.Event.CLICK, this, this.onClickErase)
|
||||
this.btn_note.on(Laya.Event.CLICK, this, this.onClickNote)
|
||||
this.btn_hint.on(Laya.Event.CLICK, this, this.onClickHint)
|
||||
|
||||
this.btn_note_normal = this.btn_note.getChildByName("normal") as Laya.Box
|
||||
this.btn_note_checked = this.btn_note.getChildByName("checked") as Laya.Box
|
||||
this.btn_note_normal.visible = this.is_note_open == false
|
||||
this.btn_note_checked.visible = this.is_note_open
|
||||
|
||||
this.obj_block_colors.visible = false
|
||||
}
|
||||
|
||||
@@ -134,7 +157,7 @@ export class Stage extends Laya.Script {
|
||||
}
|
||||
}
|
||||
|
||||
_onClickBlock(block: Block): void {
|
||||
onSetBlock(block: Block): void {
|
||||
//全部格子还原
|
||||
for (var i=0; i<this.blocks.length; i++) {
|
||||
this.blocks[i].setButtonSkin(this.img_block_normal.skin)
|
||||
@@ -166,30 +189,105 @@ export class Stage extends Laya.Script {
|
||||
}
|
||||
}
|
||||
}
|
||||
public onClickBlock(evt: Laya.Event): void {
|
||||
onClickBlock(evt: Laya.Event): void {
|
||||
if (this.selectedBlock)
|
||||
this.selectedBlock.setSelected(false)
|
||||
var block = evt.target.getComponent(Block);
|
||||
block.setSelected(true)
|
||||
this.onSetBlock(block)
|
||||
this.selectedBlock = block
|
||||
this._onClickBlock(block)
|
||||
}
|
||||
|
||||
public onClickCandy(evt: Laya.Event): void {
|
||||
|
||||
onClickUndo(evt: Laya.Event): void {
|
||||
|
||||
}
|
||||
|
||||
onClickErase(evt: Laya.Event): void {
|
||||
if (this.selectedBlock && this.selectedBlock.getIsChecked() == false) {
|
||||
var candy = evt.target.getComponent(Candy);
|
||||
var b = this.selectedBlock.setShowNumber(candy.getShowNumber());
|
||||
this._onClickBlock(this.selectedBlock)
|
||||
if (b) {
|
||||
var left = candy.getLeft();
|
||||
candy.setLeft(left-1);
|
||||
if (this.getIsComplete()) {
|
||||
console.log("完成关卡")
|
||||
this.selectedBlock.setShowNumber(0)
|
||||
this.onSetBlock(this.selectedBlock)
|
||||
}
|
||||
}
|
||||
|
||||
onClickNote(evt: Laya.Event): void {
|
||||
this.is_note_open = !this.is_note_open
|
||||
this.btn_note_normal.visible = this.is_note_open == false
|
||||
this.btn_note_checked.visible = this.is_note_open
|
||||
}
|
||||
|
||||
onClickHint(evt: Laya.Event): void {
|
||||
var list: Array<Block> = new Array()
|
||||
for (var i=0; i<this.blocks.length; i++) {
|
||||
var block = this.blocks[i]
|
||||
if (block.getIsChecked() == false)
|
||||
list.push(block)
|
||||
}
|
||||
if (list.length > 0) {
|
||||
var random = Math.random()
|
||||
var index = Math.round(random*list.length)
|
||||
if (index >= list.length)
|
||||
index = list.length -1
|
||||
var block = list[index]
|
||||
block.setShowNumber(block.getCorrectNumber())
|
||||
this.selectedBlock = block
|
||||
this.onSetBlock(block)
|
||||
}
|
||||
}
|
||||
|
||||
public onApplyCandy(candy: Candy): void {
|
||||
if (this.selectedBlock && this.selectedBlock.getIsChecked() == false) {
|
||||
var showNumber = candy.getShowNumber()
|
||||
if (this.is_note_open) {
|
||||
var relateds = this.findRelatedBlocks(this.selectedBlock)
|
||||
var find = false
|
||||
for (var i=0; i<relateds.length; i++) {
|
||||
if (relateds[i].getShowNumber() == showNumber) {
|
||||
find = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (find == false) {
|
||||
this.selectedBlock.addNoteNumber(showNumber)
|
||||
}
|
||||
else {
|
||||
for (var i=0; i<relateds.length; i++) {
|
||||
var obj = relateds[i]
|
||||
if (obj.getShowNumber() == showNumber) {
|
||||
obj.setFadeAnimation()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
var b = this.selectedBlock.setShowNumber(showNumber);
|
||||
this.onSetBlock(this.selectedBlock)
|
||||
if (b) {
|
||||
var left = candy.getLeft();
|
||||
candy.setLeft(left-1);
|
||||
if (this.getIsComplete()) {
|
||||
console.log("完成关卡")
|
||||
}
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public onClickCandy(evt: Laya.Event): void {
|
||||
var candy = evt.target.getComponent(Candy);
|
||||
this.onApplyCandy(candy)
|
||||
}
|
||||
|
||||
onKeyUp(evt: Laya.Event): void {
|
||||
var n = Number(evt.key)
|
||||
if (n && n > 0) {
|
||||
var candy = this.candys[n-1]
|
||||
this.onApplyCandy(candy)
|
||||
}
|
||||
}
|
||||
|
||||
findRelatedBlocks(obj: Block): Array<Block> {
|
||||
var list: Array<Block> = new Array()
|
||||
for (var i=0; i<this.blocks.length; i++) {
|
||||
|
||||
Reference in New Issue
Block a user