小弟自己玩as3
光是創建點擊會變色的按鈕,就想破我的頭
不好意思直接問該怎麼寫
花了好久時間,今天終於寫出來
但覺得我的寫法,好像每按一次物件就越來越多
想請教各位先進,應該怎麼修改,程式會比較好呢
var s:int = 15;//使用者輸入數字
var I:int = 1;
var textFormat:TextFormat = new TextFormat("Adobe 繁黑體 Std B",30);
textFormat.align = "center";
var btn_name:String;
var squarehit = new Sprite();
squarehit.graphics.beginFill(0xFFFFBB);
squarehit.graphics.drawRoundRect(0,0,60,60,20);
squarehit.graphics.endFill();
for (var j=1; j<=s/5; j++)
{
for (var i = 1; i <=5; i++)
{
var square = new Sprite();
square.graphics.beginFill(0xCCCCCC);
square.graphics.drawRoundRect(0,0,60,60,20);
square.graphics.endFill();
var num:TextField = new TextField();
num.defaultTextFormat = textFormat;
num.text = -5 + i + 5 * j;
num.x = 0;
num.y = 9;
num.width = square.width;
num.height = square.height;
var mc:MovieClip = new MovieClip();
mc.addChild( square );
mc.addChild( num );
var btn:SimpleButton = new SimpleButton();
btn_name = String(I);
btn.name = btn_name;
btn.upState = mc;
btn.overState = mc;
btn.downState = mc;
btn.hitTestState = mc;
btn.x = i * 80 - 30;
btn.y = (stage.stageHeight/2-square.height/2+(j*80))-s/5*40;
addChild( btn );
I++;
}
}
addEventListener(MouseEvent.CLICK, btnclick);
function btnclick(event:MouseEvent):void
{
var num:TextField = new TextField();
num.defaultTextFormat = textFormat;
num.x = 0;
num.y = 9;
num.width = square.width;
num.height = square.height;
num.text = event.target.name;
var mc:MovieClip = new MovieClip();
mc.addChild( squarehit );
mc.addChild( num );
var btn:SimpleButton = new SimpleButton();
btn.upState = mc;
btn.x = event.target.x;
btn.y = event.target.y;
addChild( btn);
}
//以下是使用者輸入5的倍數,還沒想好...
/*function makeClickListener(I:int) {
return function(evt:MouseEvent) {
trace(I);
var subj:int;
subj=(I+5-i)/5;
num.text = String( -5 + i + 5 * subj);
mc.addChild( squarehit );
mc.addChild( num );
btn.x = i * 80 - 30;
btn.y = (stage.stageHeight/2-square.height/2+(subj*80))-s/5*40;
addChild( btn );
};
}*/
/*for (var k=1; k<=s%5; k++)
{
var square1:Sprite = new Sprite();
square1.graphics.beginFill(0xCCCCCC);
square1.graphics.drawRoundRect(0,0,60,60,20);
square1.graphics.endFill();
square1.x = k * 80 - 30;
square1.y = (stage.stageHeight/2-square1.height/2+(j*80))-s/5*40;
addChild(square1);
var num1:TextField = new TextField();
num1.defaultTextFormat = textFormat;
num1.text = -11 + i + 5 * j + k;
num1.x = k * 80 - 50;
num1.y = (stage.stageHeight/2-square1.height/2+(j*80))-s/5*40+8;
addChild(num1);
fanhaotextArray.push(num1);
}*/
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 120.105.145.158
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Flash/M.1438069512.A.82E.html
※ 編輯: siesst50213 (120.105.145.188), 07/28/2015 15:47:42
1F:推 KawasumiMai: 你要用SimpleButton疊可以,但你可以將mc拉出來外面 07/28 17:59
2F:→ KawasumiMai: 弄成全域變數方便呼叫,可以用陣列管理 07/28 18:00
3F:→ KawasumiMai: 另外,除非你想要用"數值"控制顏色,不然如果只有 07/28 18:00
4F:→ KawasumiMai: 幾種顏色要變,你可以先在mc做好然後gotoAndStop 07/28 18:00
5F:→ KawasumiMai: 再把那個mc疊進去就好了 07/28 18:01
6F:推 KawasumiMai: 啊,不行,忘記MovieClip在疊進去的過程中會有issue 07/28 18:23
7F:→ KawasumiMai: (會啟動他的play) 之前有針對這個問題處理過= = 07/28 18:23
8F:推 silase0201: 你只想改顏色的話可以試mc.transform.colortransform 07/28 18:48
9F:推 viceversa56: 不用寫這麼多程式,直接物件拉一拉就好了 07/28 19:48