var h = new Array(); var j = new Array(); var xbCursor = 0; function xbinit() { xbinitName(); xbRectChoose(); xbRectChoosePic(); xbSetVisible(0); Laya.stage.on(laya.events.Event.KEY_DOWN,this,xbOnkeyDown); } function xbinitName() { var d1 = new laya.display.Text(); d1.text = "男性"; d1.x = 400; d1.y = 300; d1.fontSize = 20; d1.color = "red"; Laya.stage.addChild(d1); var d2 = new laya.display.Text(); d2.text = "女性"; d2.x = 400; d2.y = 330; d2.fontSize = 20; d2.color = "red"; Laya.stage.addChild(d2); } function xbRectChoose() { for(var i = 0 ;i < 2 ;i++){ h[i] = new laya.display.Sprite(); h[i].graphics.drawRect(400, 300 + 30 * i, 2 * 20 + 5 + 10, 20, "white"); h[i].alpha = 0.25; h[i].visible = true; Laya.stage.addChild(h[i]); } } function xbRectChoosePic() { for(var i = 0 ;i < 2 ;i++){ j[i] = new laya.display.Sprite(); j[i].loadImage("rectChoosePic.png",400 + 2 * 20 + 5 , 305 + 30 * i, 10,10); j[i].visible = true; Laya.stage.addChild(j[i]); } } function xbOnkeyDown(e) { switch(e.keyCode){ case 38:{ if ( (xbCursor - 1) <= 0 ) { xbCursor = 0; }else{ xbCursor--; } break; } case 40:{ if ( (xbCursor + 1) >= 1 ) { xbCursor = 1; }else{ xbCursor++; } break; } } xbSetVisible(xbCursor); console.log(xbCursor); } function xbSetVisible(index) { for(var i = 0;i < 2; i++){ if (i == index) { h[i].visible = true; j[i].visible = true; }else{ h[i].visible = false; j[i].visible = false; } } }