-1

我是 Actionscript 的新手,我正在创建一个简单的加法游戏,玩家将点击屏幕底部的数字 1 到 9 来解决加法问题。我应该在底部创建单独的按钮或影片剪辑吗?

如何向按钮/影片剪辑添加事件侦听器,以便能够判断玩家是否单击了第二个按钮而不是屏幕上的其他按钮。谢谢!

截屏

4

2 回答 2

1

我要做的是制作一个带有文本字段的电影剪辑。例如,我有一个影片剪辑(链接名称为)和一个名为(确保嵌入数字或您需要的任何其他字符NumClip)的动态文本字段。numText然后一个简单的 for 循环应该可以解决问题。

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);
}

我没有测试过这段代码,但它看起来不错,应该可以解决问题

于 2013-03-01T00:45:28.543 回答
1

与 Ronnie 的解决方案非常相似(我几乎完成了这个,所以不得不发布)并且它已经过测试:

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);
}
于 2013-03-01T00:52:01.073 回答