我是Actionscript的新手,我正在創建一個簡單的添加遊戲,玩家將點擊屏幕底部的數字1至9來解決附加問題。我應該在底部創建單獨的按鈕還是動畫片段?向按鈕添加事件監聽器
如何添加事件偵聽器到按鈕/動畫片段,以便能夠判斷玩家是否點擊了第二個按鈕而不是屏幕上的其他按鈕。謝謝!
我是Actionscript的新手,我正在創建一個簡單的添加遊戲,玩家將點擊屏幕底部的數字1至9來解決附加問題。我應該在底部創建單獨的按鈕還是動畫片段?向按鈕添加事件監聽器
如何添加事件偵聽器到按鈕/動畫片段,以便能夠判斷玩家是否點擊了第二個按鈕而不是屏幕上的其他按鈕。謝謝!
非常相似羅尼的解決方案(我幾乎完成了這個所以不得不發佈),並且測試:
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
import flash.text.TextFormat;
var buttonCount = 9;
var buttonSize = 50;
var button:MovieClip;
var label:TextField;
for (var i:int = 0; i < buttonCount; i ++)
{
// Create a new MovieClip
button = new MovieClip();
// We'll use this in the event handler to identify which button was clicked
button.id = i + 1;
// Draw the background in the graphics prop of the MovieClip
button.graphics.beginFill(0x00ff00, 1);
button.graphics.drawRect(0, 0, buttonSize, buttonSize);
// Add event listener
button.addEventListener(MouseEvent.CLICK, this.clickHandler);
// Position the button
button.x = i * (buttonSize + 20); // Add some spacing
button.y = stage.stageHeight - buttonSize - 10;
// Add the button to the stage
addChild(button);
// Create the label for the button
label = new TextField();
label.text = button.id.toString();
label.selectable = false;
label.multiline = false;
label.autoSize = TextFieldAutoSize.LEFT;
label.setTextFormat(new TextFormat('Arial', 12, 0, true));
// Position the label in the centre of the button
label.x = (buttonSize - label.width)/2;
label.y = (buttonSize - label.height)/2;
// Add the label to the button MovieClip
button.addChild(label);
}
function clickHandler(event:MouseEvent):void
{
trace("Button clicked:", event.currentTarget.id);
}
我會做的是讓在它的文本字段一個影片剪輯。例如,我有一個影片剪輯(連接名稱爲NumClip
)和一個名爲(確保嵌入數字或任何其他需要的字符)的動態文本字段。然後一個簡單的循環應該做的伎倆。
var maxNum:Number = 9;
for (var i:int = 1; i <= maxNum; i++)
{
var clip:NumClip = new NumClip();
clip.x = i * (clip.width + 5);
clip.y = 50;
clip.id = i;
clip.numText.text = String(i);
clip.addEventListener(MouseEvent.CLICK, numClick);
addChild(clip);
}
function numClick(e:MouseEvent):void
{
trace("You clicked number " + e.currentTarget.id);
}
我沒有測試此代碼,但它看起來不錯,我和應該做的伎倆