當我的隨機詞出現時,用戶必須記住它並點擊正確的相應圖像。我試圖編寫運行的代碼,如果用戶選擇正確的圖像。我在我的陣列中配對了我的文字和圖像。我只是不確定如何去調用這個函數。如果點擊的圖像等於其匹配的單詞,我該如何調用一個函數? Actionscript3
這是我到目前爲止嘗試過的,但這不起作用。我對actionscript3很陌生,所以請原諒缺乏知識,因爲我試圖自我教導。
所有幫助非常感謝!
當我的隨機詞出現時,用戶必須記住它並點擊正確的相應圖像。我試圖編寫運行的代碼,如果用戶選擇正確的圖像。我在我的陣列中配對了我的文字和圖像。我只是不確定如何去調用這個函數。如果點擊的圖像等於其匹配的單詞,我該如何調用一個函數? Actionscript3
這是我到目前爲止嘗試過的,但這不起作用。我對actionscript3很陌生,所以請原諒缺乏知識,因爲我試圖自我教導。
所有幫助非常感謝!
這是可以做到這一點的一種方法: 見代碼註釋
basket.visible = false;
//------ Home Button ------\\
backhome1btn.addEventListener(MouseEvent.CLICK, goback1Click);
function goback1Click(event:MouseEvent):void{
gotoAndStop("homepage");
}
//-------------------
var score:int = 0;
var items:Array = new Array(); //store all food items in array
var wordsToShow:Array = new Array(); //store all words to show in array - this is the array that will keep track of which has been asked (or rather not asked yet)
//to reduce redundant code, call this with each food item (below)
function initFoodItem(item:MovieClip, word:String):void {
item.word = word; //forget the array, just store the word on the image as a dynamic property
item.addEventListener(MouseEvent.CLICK, foodClicked);
items.push(item); //add to array
wordsToShow.push(item); //add to array
item.visible = false;
}
initFoodItem(oc, "Orange Juice");
initFoodItem(sand, "Sandwich");
//...repeat for all other food items
//now randmize the words to show array:
wordsToShow.sort(function(a,b):int {
return(Math.random() > .5) ? 1 : -1;
});
var curAnswer:MovieClip; //a var to store the current correct answer
//this does the next question per se
function displayWord():void {
if(wordsToShow.length < 1){
//they've all been asked
gotoAndPlay("gameoverpage");
return;
}
curAnswer = wordsToShow.pop(); //assigns the last item in the array to the cur asnwer, and pop also removes that item from the array (so it won't get asked again)
randomword.text = curAnswer.word; //assign the text to the word value of this item
randomword.visible = true;
remember.visible = true;
}
remember.addEventListener(MouseEvent.CLICK, readyClick);
//when you click your ready button
function readyClick(e:MouseEvent):void {
//we have an array of all items, let's loop through it and change them all to be visible
for (var i:int = 0; i < items.length; i++) {
//items[i].alpha = 1; //alpha values are 0 - 1 in AS3
items[i].visible = true; //use visible instead of alpha if just toggling visibility, it's more efficient
}
randomword.visible = false;
remember.visible = false;
bask.visible = true;
notepape.visible = false;
//! another reason to use visible instead of alpha = 0, is alpha = 0 items will still be clickable! visible = false items cannot be clicked.
}
function foodClicked(e:MouseEvent):void {
if (e.currentTarget == curAnswer) {
//if the current target (the item clicked) is the same item as what we stored in curAnswer, you answered correctly, so do this:
score += 10; //or however much you get for answering correctly
gotoAndStop("listpage"); //not sure what this does?
displayWord();
}else {
//wrong
gotoAndStop("gameoverpage");
}
}
感謝您的幫助@LDMS :)不幸的是它沒有工作,它沒有顯示隨機單詞,我不能再走了,生病不停地亂搞!乾杯 – user3594463
你需要比「沒有工作」更具體。你有錯誤嗎?你不能複製和粘貼這段代碼,你仍然需要設置你的'randomword'和'remeber'按鈕等。 – BadFeelingAboutThis
現在,除了說'//...repeat for all other items items',你可以用我的代碼代替你的代碼。如果它不起作用,那很可能是因爲你還沒有包括其他東西。例如你的其他框架等。 – BadFeelingAboutThis
你如何創建你的圖片?它們只是實例名稱的時間軸上的對象嗎? – BadFeelingAboutThis
您可以詳細瞭解您要完成的任務嗎?所以你有這些圖像,你可以點擊它們。當你點擊一個,發生了什麼?什麼是'listAry'?框架'listpage'發生了什麼? – BadFeelingAboutThis
我的照片是動畫片段。一個隨機的字出現在屏幕上,幾秒鐘後消失,出現9個圖像。用戶必須點擊與該單詞相同的圖像,例如單詞可能是 - 餅乾,當圖片出現時,用戶應點擊餅乾圖像 – user3594463