看板 Flash 關於我們 聯絡資訊
※ 引述《morphise (QQQQQ)》之銘言: : 目前老師出了一個設計小遊戲的題目 : 老師要我們在畫面上繪製幾個方塊 : 然後每個方塊從紅橙黃綠藍靛紫七種顏色中隨機挑選 : 每個方塊被按到的時候就會按照上面的顏色順序變換 : 變到紫色後就在跳回紅色 : 然後我希望是使用者要在這七個方塊的顏色搭配上達到一定的組合後 : 才可以進行下一步 : 舉例來說就是要一直按方塊來改顏色達到像是這個組合才成功 ▉▉▉▉▉▉▉ : 目前我用tweenLite to 來變換顏色 : 但是卻沒辦法設定哪一個方塊顏色被改了 而且改成甚麼 : 所以也沒辦法用if來看是否達到正確組合 : 板上有人可以指點一下方向應該是大概要怎麼做嗎? : (因為才剛上4,5堂課 : 所以老師只教了基本的東西 : 我想破頭都想不到要怎麼用老師教的東西做這個作業) 我用了最陽春的笨方法寫出來了 以下是程式內容 不知道有沒有人知道有什麼方法可以不要寫五個functions來跑?XD package { import flash.display.Sprite; import flash.events.MouseEvent; import com.greensock.*; import com.greensock.easing.*; import com.greensock.plugins.*; public class Main extends Sprite { public var allBlocks:Array; public var totalItems:uint; public var red:Number = 0xff0000; public var green:Number = 0x00ff00; public var blue:Number = 0x0000ff; public var yellow:Number = 0xffff00; public var pink:Number = 0xff00ff; public var color:Array; public var answer:Number = 0; public function Main() { allBlocks = new Array(); color = new Array(); totalItems = 5; for (var i:uint; i < totalItems; i++) { var randomNum:Number = Math.round(Math.random()); if (randomNum <= .2) color[i] = red; if (.2< randomNum && randomNum <= .4) color[i] = green; if (.4< randomNum && randomNum <= .6) color[i] = blue; if (.6< randomNum && randomNum <= .8) color[i] = yellow; if (.8< randomNum && randomNum <= 1) color[i] = pink; var tempBlocks:DrawBlocks = new DrawBlocks (color[i]); tempBlocks.x = 30 + stage.stageWidth/5 * i; tempBlocks.y = stage.stageHeight/2; allBlocks.push(tempBlocks); addChild (allBlocks[i]); } trace("To unlock, you need to find the combination below"); trace("Red Green Blue Yellow Pink"); if (color[0] == red) answer = answer +1; if (color[1] == green) answer = answer +1; if (color[2] == blue) answer = answer +1; if (color[3] == yellow) answer = answer +1; if (color[4] == pink) answer = answer +1; endGame(answer); trace(answer); allBlocks[0].addEventListener(MouseEvent.MOUSE_DOWN, firstMouseClickHandler); allBlocks[1].addEventListener(MouseEvent.MOUSE_DOWN, secondMouseClickHandler); allBlocks[2].addEventListener(MouseEvent.MOUSE_DOWN, thirdMouseClickHandler); allBlocks[3].addEventListener(MouseEvent.MOUSE_DOWN, forthMouseClickHandler); allBlocks[4].addEventListener(MouseEvent.MOUSE_DOWN, fifthMouseClickHandler); } public function firstMouseClickHandler (e:MouseEvent) : void { TweenPlugin.activate([TintPlugin]); var checktimes:Boolean = true; if (color[0] == pink) { color[0] = red; checktimes = false; } if (color[0] == yellow) color[0] = pink; if (color[0] == blue) color[0] = yellow; if (color[0] == green) color[0] = blue; if (color[0] == red && checktimes == true) { color[0] = green; answer = answer - 1; } if (color[0] == red) answer = answer + 1; checktimes = true; TweenLite.to(e.target, .1, {tint:color[0]}); trace(answer); endGame (answer); } public function secondMouseClickHandler (e:MouseEvent) : void { TweenPlugin.activate([TintPlugin]); var checktimes:Boolean = true; if (color[1] == pink) { color[1] = red; checktimes = false; } if (color[1] == yellow) color[1] = pink; if (color[1] == blue) color[1] = yellow; if (color[1] == green) { color[1] = blue; answer = answer - 1; } if (color[1] == red && checktimes == true) color[1] = green; if (color[1] == green) answer = answer + 1; checktimes = true; TweenLite.to(e.target, .1, {tint:color[1]}); trace(answer); endGame (answer); } public function thirdMouseClickHandler (e:MouseEvent) : void { TweenPlugin.activate([TintPlugin]); var checktimes:Boolean = true; if (color[2] == pink) { color[2] = red; checktimes = false; } if (color[2] == yellow) color[2] = pink; if (color[2] == blue) { color[1] = yellow; answer = answer - 1; } if (color[2] == green) color[2] = blue; if (color[2] == red && checktimes == true) color[2] = green; if (color[2] == blue) answer = answer + 1; checktimes = true; TweenLite.to(e.target, .1, {tint:color[2]}); trace(answer); endGame (answer); } public function forthMouseClickHandler (e:MouseEvent) : void { TweenPlugin.activate([TintPlugin]); var checktimes:Boolean = true; if (color[3] == pink) { color[3] = red; checktimes = false; } if (color[3] == yellow) { color[1] = pink; answer = answer - 1; } if (color[3] == blue) color[3] = yellow; if (color[3] == green) color[3] = blue; if (color[3] == red && checktimes == true) color[3] = green; if (color[3] == yellow) answer = answer + 1; checktimes = true; TweenLite.to(e.target, .1, {tint:color[3]}); trace(answer); endGame (answer); } public function fifthMouseClickHandler (e:MouseEvent) : void { TweenPlugin.activate([TintPlugin]); var checktimes:Boolean = true; if (color[4] == pink) { color[4] = red; checktimes = false; answer = answer - 1; } if (color[4] == yellow) color[4] = pink; if (color[4] == blue) color[4] = yellow; if (color[4] == green) color[4] = blue; if (color[4] == red && checktimes == true) color[4] = green; if (color[4] == pink) answer = answer + 1; checktimes = true; TweenLite.to(e.target, .1, {tint:color[4]}); trace(answer); endGame (answer); } public function endGame ( corrects:uint) : void { if ( corrects == 5 ) trace("UNLOCKED!"); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 76.124.81.21
aquarianboy:比較簡單的,就是只要寫一個event handler就行了 03/14 09:24
aquarianboy:然後在event handler裡用switch判斷是哪顆按鈕被按了 03/14 09:24
aquarianboy:高級一點點的話,可以用strategy pattern來代替switch 03/14 09:26
zephyrhymn:數量不多 用pattern反而顯得累贅 03/14 11:03
peacedove:寫個class?? 03/14 12:41
peacedove:我沒很仔細看code 可是如果做的事情都一樣 不是一個 03/14 12:43
peacedove:function就可以了嗎?? 03/14 12:43
dsmwang:可以用7的次方數來當作確認標準...XD 03/14 12:51
dsmwang:就不需要一堆if了 03/14 12:51
KawasumiMai:為什麼不要用btn包mc,然後呼叫click event就好 03/14 16:51
KawasumiMai:外觀的顏色不要當作判斷標準,用陣列比對就好 03/14 16:52
etrexetrex:你的code 很有趣XD 03/14 20:20
zephyrhymn:可以用多型的概念來寫...重複code太多 03/16 17:08
zephyrhymn:同一階層的判斷switch會比if好很多尤其判斷數量多時 03/16 17:09
morphise:哈哈 會這麼寫是因為老師還沒教到button跟mc 03/17 09:55
morphise:我也希望可以不要用那個多function來判斷 可是我試很多次 03/17 09:56
morphise:都不知道要怎麼用for loop來跑 03/17 09:57
morphise:為什麼我的code很有趣阿XD 03/17 09:57
morphise:要怎麼樣才可以判斷是哪個方塊被按了阿? 03/17 10:29
aquarianboy:試試 event.currentTarget 03/17 11:52
morphise:可是老師希望我們用他之前上課教的東西就好 03/17 12:25
morphise:他說用他教過的東西就可以寫出來了 可是大家給的建議他都 03/17 12:26
morphise:沒教過... 03/17 12:26
morphise:我現在最大的問題是不知道怎麼讓系統知道我點的方塊是第 03/17 12:27
morphise:幾個方塊 如果這個問題解決的話 程式就會精簡很多 03/17 12:28