wpf flash 组件

tech2024-04-14  13

wpf flash 组件

Perhaps one of the easiest components to overlook in Flash MX 2004 is tucked away in the UI Components section of the Components Panel, and carries the uninteresting name NumericStepper.

Flash MX 2004中最容易忽略的组件之一可能藏在“组件面板”的“ UI组件”部分中,并带有无趣的名称NumericStepper。

Perhaps you’re developing experimental effects and require on-screen controls to alter dynamic feedback within those effects. Maybe you’re building complex forms that allow users to quickly select numerical data. Either way, it’s time to cast your eye over the NumericStepper component.

也许您正在开发实验效果,并且需要屏幕上的控件来更改这些效果中的动态反馈。 也许您正在构建允许用户快速选择数字数据的复杂表格。 无论哪种方式,都是时候让您着眼于NumericStepper组件。

Let’s look at an example. All the dynamics of the movie pictured below are controlled with instances of the NumericStepper.

让我们来看一个例子。 下图显示的电影的所有动态都由NumericStepper实例控制。

NumericStepper is an extremely valuable component, but at its most basic, it’s designed to step up or down through a series of numbers incrementally. There are several properties of the component that we can get and set. These extremely useful properties, which we’ll explore in this article, are as follows:

NumericStepper是一个非常有价值的组件,但从最基本的角度来说,它旨在逐步增加或减少一系列数字。 我们可以获取和设置组件的几个属性。 我们将在本文中探讨这些极其有用的属性,如下所示:

minimum: the minimum value of the component

minimum :组件的最小值

maximum: the maximum value of the component

maximum :组件的最大值

nextValue: the next incremental value of the component (read-only)

nextValue :组件的下一个增量值(只读)

previousValue: the previous incremental value of the component (read-only)

previousValue :组件的上一个增量值(只读)

stepSize: the incremental change value for the component

stepSize :组件的增量更改值

value: the currently selected value of the component

value :组件当前选择的值

Potential uses for the NumericStepper component include:

NumericStepper组件的潜在用途包括:

used as form elements to facilitate rapid numeric value selection

用作表单元素以方便快速选择数值 used as date selection elements (months, years etc)

用作日期选择元素(月,年等) used in experimental applications to control complex values

用于实验应用以控制复杂值

The main difference between the NumericStepper and ComboBox components, apart from the methods, properties and events, is to do with ease of use.

除了方法,属性和事件之外,NumericStepper和ComboBox组件之间的主要区别在于易于使用。

While you can accomplish similar effects using the ComboBox component, the NumericStepper component allows you to increment up or down a selection rapidly via the ‘up’ and ‘down’ arrows, or the up and down keys on the keyboard. Using the ComboBox, you need to click the ComboBox, wait for it to display the options, then select the desired value from a dropdown.

尽管可以使用ComboBox组件完成类似的效果,但NumericStepper组件允许您通过“向上”和“向下”箭头或键盘上的向上和向下键快速向上或向下增加选择。 使用组合框,您需要单击组合框,等待其显示选项,然后从下拉列表中选择所需的值。

The NumericStepper solution is obviously much quicker than the ComboBox, and its capabilities allow NumericStepper to be implemented and used easily and effectively in some unique applications.

NumericStepper解决方案显然比ComboBox快得多,其功能使NumericStepper可以在某些独特的应用程序中轻松有效地实现和使用。

数值步进基础 (Numeric Stepper Basics)

To get a feel for this component, let’s add a single instance of NumericStepper to the stage. We’ll then populate some dynamic text boxes with a couple of the NumericStepper component’s properties.

为了对该组件有所了解,让我们向舞台添加一个NumericStepper实例。 然后,我们将使用几个NumericStepper组件的属性填充一些动态文本框。

The source FLA file for this example is called NumericStepperBasics.fla and can be found in this article’s downloadable code archive.

此示例的源FLA文件称为NumericStepperBasics.fla ,可以在本文的可下载代码档案中找到 。

搭建舞台 (Setting the Stage)
Start by creating a new Flash document. Accept the default width, height and frame rate settings.

首先创建一个新的Flash文档。 接受默认的宽度,高度和帧频设置。

Rename the default layer Actions and add another layer beneath it called Components. Drag an instance of the NumericStepper component from the UI Components section of the Components panel into the first frame of the Components layer. Name the instance StepperMain.

重命名默认图层“动作”,并在其下面添加另一个名为“组件”的图层。 将“ NumericStepper”组件的一个实例从“组件”面板的“ UI组件”部分拖到“组件”层的第一帧中。 将实例命名为StepperMain 。

Select the NumericStepper component instance and add the following values within the Property Inspector: maximum:100, minimum:0; stepSize:5, value:10.

选择NumericStepper组件实例,然后在属性检查器中添加以下值: maximum:100 , minimum:0 ; stepSize:5 , value:10 。

Note: Here, we’ve added the component’s property values via the Property Inspector, but we can also add these values programmatically via ActionScript if we wish (more on this later).

注意:在这里,我们已经通过Property Inspector添加了组件的属性值,但是如果愿意的话,我们也可以通过ActionScript以编程方式添加这些值(稍后会详细介绍)。

Create two dynamic text boxes one above the other. Name the instances previousTextBox and nextTextBox.

创建两个动态文本框,一个在另一个上方。 将实例命名为previousTextBox和nextTextBox 。

When we click the NumericStepper component, these text boxes will be populated with the next and previous values of the component. We’ll add the ActionScript that achieves this in a moment.

当我们单击NumericStepper组件时,这些文本框将填充该组件的next和previous值。 我们将添加稍后实现此目的的ActionScript。

Add relevant labels to the component and text boxes if you wish, as shown in the above screenshot.

如需要,将相关标签添加到组件和文本框中,如上面的屏幕截图所示。

Let’s move on. Now, we’ll add the ActionScript that allows us to see the component in action…

让我们继续。 现在,我们将添加ActionScript,该脚本使我们能够查看正在运行的组件…

添加动作脚本 (Adding the ActionScript)
With the first frame of the Actions layer selected, add the following code within the Actions Panel:

选择“动作”图层的第一帧后,在“动作”面板中添加以下代码: var palletteArray = new Array(); palleteArray = ["0x7FAAD4", "0xFFDF7F"]; function setTheme() {  _global.style.setStyle("themeColor", palleteArray[0]);  _global.style.setStyle("backgroundColor", palleteArray[1]);  _global.style.setStyle("fontSize", 11);  _global.style.setStyle("fontWeight", "bold");  _global.style.setStyle("color", palleteArray[0]); } setTheme(); // Called at startup var stepsListener = new Object(); stepsListener.change = function() {  populateTextBoxes(); }; stepperMain.addEventListener("change", stepsListener); function populateTextBoxes() {  if ((stepperMain.value<stepperMain.maximum) && (stepperMain.value>stepperMain.minimum)) {    nextTextBox.text = stepperMain.nextValue;    previousTextBox.text = stepperMain.previousValue;  } }

The first thing that we are doing here is creating a new array called ‘palletteArray‘, which we populate with a couple of color codes that we’ve selected for our interface: a nice pastel yellow (#7FAAD4) and a complementary blue (#FFDF7F).

我们在这里要做的第一件事是创建一个名为' palletteArray '的新数组,该数组中填充了一些我们为界面选择的颜色代码:柔和的黄色( #7FAAD4 )和互补的蓝色( #FFDF7F )。

Note: Creating an array of colors makes it much easier to switch those colors if we need to: we simply change the values of different array members. This solution allows us to avoid having to search through the code, changing individual hexadecimal values as we find them.

注意:如果需要,创建颜色数组可以更轻松地切换这些颜色:我们只需更改不同数组成员的值即可。 该解决方案使我们不必搜索代码,而在找到它们时更改各个十六进制值。

We then have a function called setTheme(), which is called at startup. It sets the theme for the NumericStepper component (or any other Flash MX 2004 component on the stage) using the palletteArray array we set up earlier.

然后,我们有一个名为setTheme()的函数,该函数在启动时调用。 它使用我们之前设置的palletteArray数组为NumericStepper组件(或舞台上的任何其他Flash MX 2004组件)设置主题。

We then create a new listener object that listens for a change in the value of the NumericStepper component. When a change is detected, the populateTextBoxes() function is called. It pushes the nextValue and previousValue properties of the component to the relevant dynamic text boxes.

然后,我们创建一个新的listener对象,以侦听NumericStepper组件的值更改。 检测到更改时,将调用populateTextBoxes()函数。 它将组件的nextValue和previousValue属性推nextValue相关的动态文本框中。

Save the Flash document to a location of your choice and preview your work in Flash.

将Flash文档保存到您选择的位置,然后在Flash中预览您的工作。

As you can see, clicking on the NumericStepper instance increments or decrements the values of the component, and pushes the next and previous values into the dynamic text areas.

如您所见,单击NumericStepper实例可递增或递减组件的值,并将next和previous值推入动态文本区域。

This example gave us a nice simple introduction to the component. We can now build on these foundations by creating more complex creations for use in your projects.

该示例为我们提供了对该组件的很好的简单介绍。 现在,我们可以通过创建更复杂的作品以在您的项目中使用来建立这些基础。

日期选择 (Date Selection)

The source FLA file for this example is NumericStepperDates.fla.

此示例的源FLA文件是NumericStepperDates.fla 。

This example is a little different as it uses the NumericStepper component to create simple date controls within the application. These date controls might be used within an online application form, or a Flash-driven blog-searching application. In this example, we’ll just send the output of a function to the Output Panel.

这个示例有些不同,因为它使用NumericStepper组件在应用程序内创建简单的日期控件。 这些日期控件可以在在线应用程序表单或Flash驱动的博客搜索应用程序中使用。 在此示例中,我们仅将函数的输出发送到“输出面板”。

搭建舞台 (Setting the Stage)

We’ll use some of the same core code that we used in the previous example and build upon that to create a different outcome; also in this example we will populate the NumericStepper component using ActionScript rather than hard-coding its contents via the Property Inspector.

我们将使用与上一个示例相同的核心代码,并以此为基础创建不同的结果。 同样在此示例中,我们将使用ActionScript填充NumericStepper组件,而不是通过Property Inspector对其内容进行硬编码。

Create a new Flash document with default properties and rename the default layer Actions. Below it, add a further layer called Components.

使用默认属性创建一个新的Flash文档,并将默认图层Actions重命名。 在它的下面,添加另一个名为Components层。

Drag 2 instances of the NumericStepper component from the UI Components section of the Components Panel into the first frame of the Components layer. Name the instances StepperYear and StepperMonth.

将NumericStepper组件的2个实例从“组件面板”的“ UI组件”部分拖到“组件”层的第一帧中。 将实例命名为StepperYear和StepperMonth 。

Add relevant text labels above each component (e.g. Select Year, Select Month).

在每个组件上方添加相关的文本标签(例如,选择年份,选择月份)。
添加动作脚本 (Adding the ActionScript)

It’s now time to populate the components with the values we want for the month and year. When the values in either of the component instances change, the selected date is pushed to the Output Panel.

现在该用我们想要的月份和年份的值填充组件了。 当两个组件实例中的任何一个实例的值更改时,所选日期都将推送到“输出面板”。

Select the first frame of the ‘Actions‘ layer and add the following code within the Actions Panel:

选择“ Actions ”层的第一帧,然后在“动作面板”中添加以下代码:

Note: Code highlighted in bold has altered from the previous example.

注意:以粗体突出显示的代码与上一个示例有所不同。

var monthArray = new Array();   monthArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];   var palletteArray = new Array();   palleteArray = ["0x7FAAD4", "0xFFDF7F"];   function setTheme() {    _global.style.setStyle("themeColor", palleteArray[0]);    _global.style.setStyle("backgroundColor", palleteArray[1]);    _global.style.setStyle("fontSize", 11);    _global.style.setStyle("fontWeight", "bold");    _global.style.setStyle("color", palleteArray[0]);   }   setTheme();   var stepsListener = new Object();   stepsListener.change = function() {  pushDate();   };   stepperYear.addEventListener("change", stepsListener);   stepperMonth.addEventListener("change", stepsListener);   function pushDate() {    trace("The selected year is: "+stepperYear.value+      "; the selected Month is: "+monthArray[stepperMonth.value-1]);   }   function populateYearStepper() {    var now = new Date();    stepperYear.minimum = now.getUTCFullYear();    stepperYear.maximum = now.getUTCFullYear()+10;    stepperYear.stepSize = 1;    stepperYear.value = now.getUTCFullYear();   }   populateYearStepper();   function populateMonthStepper() {    var now = new Date();    stepperMonth.minimum = 1;    stepperMonth.maximum = 12;    stepperMonth.stepSize = 1;    stepperMonth.value = now.getUTCMonth()+1;   }   populateMonthStepper();

Initially, we create a new array called monthArray, which holds the names of the months of the year; we will use this later in the example to get month names from the pushDate() function.

最初,我们创建一个名为monthArray的新数组,其中包含一年中各个月份的名称。 我们将在示例的后面部分中使用它来从pushDate()函数获取月份名称。

Note that in this example, we tie both instances of the component to the same listener object. Whenever the value of either instance changes, the pushDate() function is called.

请注意,在此示例中,我们将组件的两个实例都绑定到同一个listener对象。 每当任一实例的值更改时, pushDate()调用pushDate()函数。

stepperYear.addEventListener("change", stepsListener); stepperMonth.addEventListener("change", stepsListener);

We have two functions in this example (populateYearStepper(), and populateMonthStepper()), both of which are called at startup. They dynamically populate the stepperYear and stepperMonth NumericStepper instances, respectively.

在此示例中,我们有两个函数( populateYearStepper()和populateMonthStepper() ),这两个函数都在启动时被调用。 它们分别动态地填充stepperYear和stepperMonth NumericStepper实例。

The populateYearStepper() function creates a new date object, then pushes the minimum, maximum, stepSize and value properties into the stepperYear component instance using various iterations of the getUTCFullYear() method. This produces a minimum value, which is the current year, and a maximum value, which is the current year plus 10. It sets the step size to 1 and the initial value of the NumericStepper instance to the current year.

populateYearStepper()函数创建一个新的date对象,然后使用getUTCFullYear()方法的各种迭代将minimum , maximum , stepSize和value属性推入stepperYear组件实例。 这将产生一个minimum ,即当前年份,以及一个maximum ,即当前年份加10。它将步长设置为1,将NumericStepper实例的初始值设置为当前年份。

A similar process occurs with the populateMonthStepper() function, which populates the stepperMonth instance with the month numbers (1-12). It then sets the initial value to the current month using the following code:

populateMonthStepper()函数也会发生类似的过程,该函数使用月份数字(1-12)填充stepperMonth实例。 然后使用以下代码将初始值设置为当前月份:

now.getUTCMonth()+1

The getUTCMonth() method returns an integer between 0 and 11, indicating the month value in the Date object (January will appear as 0, December as 11, etc.). We add 1 to this to obtain the number we wish to display in the NumericStepper.

getUTCMonth()方法返回0到11之间的整数,指示Date对象中的月份值(1月将显示为0,12月将显示为11, getUTCMonth() )。 我们对此加1,以获得希望显示在NumericStepper中的数字。

When either of the values of the NumericStepper instances change, the pushDate() function is called. We use this function to trace out the selected date.

当NumericStepper实例的任何一个值更改时,将pushDate()函数。 我们使用此功能来找出选定的日期。

The year is simple enough to trace out, as we can easily get the value of the stepperYear NumericStepper instance using stepperYear.value.. But, in order to convert the zero-based month value into a month name, we need to look up its position within the monthArray array we initiated earlier.

年份很容易找出,因为我们可以使用stepperYear.value.轻松获取stepperYear NumericStepper实例的值stepperYear.value. 。 但是,为了将基于零的月份值转换为月份名称,我们需要在我们之前启动的monthArray数组中查找其位置。

trace("The selected year is: "+stepperYear.value+  "; the selected Month is: "+monthArray[stepperMonth.value-1]);

Save your Flash document and preview your work.

保存您的Flash文档并预览您的工作。

When you click on either of the NumericStepper instances, the pushDate() function is called and the selected month and year are traced out to the Output Panel in a convenient manner.

当您单击任一NumericStepper实例时,将pushDate()函数,并且以方便的方式将所选的月份和年份追溯到“输出面板”。

There are multiple iterations and uses of the NumericStepper component that can be derived from this example, so I suggest that you experiment and see how you can apply this component in your own projects.

从本示例可以派生NumericStepper组件的多次迭代和使用,因此,我建议您进行实验并了解如何在自己的项目中应用此组件。

实验控制 (Experimental Control)

As we’ve already mentioned, another interesting application of the NumericStepper component is to use it as a control for experimental effects, such as the one shown in the screenshot at the beginning of this tutorial.

正如我们已经提到的,NumericStepper组件的另一个有趣的应用是将其用作实验效果的控件,例如本教程开始时的屏幕快照中所示的控件。

In this example, we’ll use the component dynamically to create multiple dynamic iterations of text fields via the createEmptyMovieClip() and createTextField() method.

在此示例中,我们将动态使用组件通过createEmptyMovieClip()和createTextField()方法创建文本字段的多个动态迭代。

The value property of the NumericStepper instance is retrieved and used to iterate through a loop, which creates a series of text fields. On each subsequent change of the component’s value, the screen is redrawn with the number of text fields reflecting the value within the component.

检索NumericStepper实例的value属性,并将其用于遍历循环,该循环创建一系列文本字段。 在组件值的每次后续更改中,将重新绘制屏幕,​​其中包含反映组件内值的文本字段数。

The source FLA file for this example is called NumericStepperDynamicText.fla and can be found in the code archive for this article.

此示例的源FLA文件称为NumericStepperDynamicText.fla ,可以在本文的代码档案中找到。

Note: The code archive includes another example of this FLA in which the dynamic creations are placed randomly on the stage. The file’s named NumericStepperDynamicRandomText.fla

注意:代码档案库包含此FLA的另一个示例,其中动态创建内容随机放置在舞台上。 该文件的名称为NumericStepperDynamicRandomText.fla

While this example shows how to dynamically control the creation and destruction of Movie Clips and text fields, I’m going to skim through the dynamic creation code. Instead, I’ll concentrate on the logic flow through the example so you can get on with dissecting the example and modifying it for your needs.

尽管此示例显示了如何动态控制影片剪辑和文本字段的创建和销毁,但我将浏览动态创建代码。 相反,我将专注于示例中的逻辑流程,因此您可以继续剖析示例并根据需要对其进行修改。

搭建舞台 (Setting the Stage)

Create a new Flash document with default properties and rename the default layer Actions. Add below it a layer called Components.

使用默认属性创建一个新的Flash文档,并将默认图层Actions重命名。 在其下添加一个名为Components的层。

Drag an instance of the NumericStepper component from the UI Components section of the Components Panel into the first frame of the Components layer. Name the instance StepperMain.

将NumericStepper组件的实例从“组件面板”的“ UI组件”部分拖到“组件”层的第一帧中。 将实例命名为StepperMain 。

With the NumericStepper component instance selected, add the following values within the Property Inspector: maximum:100, minimum:0; stepSize:5, value:10.

选中NumericStepper组件实例后,在Property Inspector中添加以下值: maximum:100 , minimum:0 ; stepSize:5 , value:10 。

From a setup point of view, that’s all we need to do. The rest of the example is controlled via ActionScript.

从设置的角度来看,这就是我们要做的。 该示例的其余部分通过ActionScript控制。

添加动作脚本 (Adding the ActionScript)

Select the first frame of the Actions layer and add the following code within the Actions Panel:

选择“ Actions层的第一帧,然后在“动作面板”中添加以下代码:

var palletteArray = new Array(); palleteArray = ["0x7FAAD4", "0xFFDF7F"];   function setTheme() {    _global.style.setStyle("themeColor", palleteArray[0]);    _global.style.setStyle("backgroundColor", palleteArray[1]);    _global.style.setStyle("fontSize", 11);    _global.style.setStyle("fontWeight", "bold");    _global.style.setStyle("color", palleteArray[0]);   }   setTheme();   var stepsListener = new Object();   stepsListener.change = function() {  removeDeadClips();    createTextBoxes();   };   stepperMain.addEventListener("change", stepsListener);  function createTextBoxes() {    if (stepperMain.value>stepperMain.minimum) {      var yPos = 30;      var yStart = stepperMain._y;      var yStep = 20;      var xStart = 150;      var textMC = _root.createEmptyMovieClip("textMC", 10);      for (var i=1; i<=stepperMain.value; i++) {        var field = textMC.createTextField("sampleText"+i, i, xStart, yStart+yPos, 200, 25);        field.multiline = true;        field.wordWrap = true;        field.border = false;        //Format Text        formatText(field, i);        var yPos += yStep;      }    }   }   createTextBoxes();   function formatText(field, number) {    var textFormatter = new TextFormat();    textFormatter.color = palleteArray[0];    textFormatter.bullet = false;    textFormatter.underline = false;    textFormatter.font = "_sans";    textFormatter.size = 12;    textFormatter.bold = true;    field.text = "Dynamic Text Creation #: "+number;    field.setTextFormat(textFormatter);   }   function removeDeadClips() {    for (z in _root) {      if (typeof (_root[z]) == "movieclip") {        _root[z].removeMovieClip();      }    }   }

As before, we set up the change event handler. This time, it fires two functions (removeDeadClips() and createTextBoxes()) when the value of the NumericStepper component changes.

和以前一样,我们设置了更改事件处理程序。 这次,当NumericStepper组件的值更改时,它将触发两个函数( removeDeadClips()和createTextBoxes() )。

After setTheme(), the first function to be called at startup is createTextBoxes(), which takes the initial value of the stepperMain instance, and creates an empty movie clip. It then iterates through a for loop using the stepperMain.value property as the limiting value.

在setTheme()之后,启动时要调用的第一个函数是createTextBoxes() ,该函数采用stepperMain实例的初始值,并创建一个空的movie clip 。 然后,它使用stepperMain.value属性作为限制值遍历for循环。

As we iterate through the for loop, a series of text fields are created and formatted using the formatText() function. This sets general properties of the text field, including color from the palletteArray array, and adds some simple dynamic text.

当我们遍历for循环时,使用formatText()函数创建并设置了一系列文本字段的格式。 这将设置文本字段的常规属性,包括palletteArray数组的颜色,并添加一些简单的动态文本。

When the stepperMain NumericStepper instance value is changed, the removeDeadClips() function is called from the event handler. It removes the empty movie clip we created to hold the text fields.

更改stepperMain NumericStepper实例值时,将从事件处理程序中调用removeDeadClips()函数。 它将删除我们创建的用于保存文本字段的空movie clip 。

Note: While only a single Movie Clip is created dynamically on the stage, and we could simply remove it using removeMovieClip(), I chose to use the function in this example. This approach provides extra benefits in that, if I wish to create multiple Movie Clips later on, I can simply alter the function to suit my needs, and point it at a particular level or nested Movie Clip instance.

注意:虽然只有一个动态Movie Clip在舞台上动态创建,并且我们可以简单地使用removeMovieClip()删除它,但在本示例中,我选择使用该函数。 这种方法提供了额外的好处,如果以后我希望创建多个Movie Clips ,我可以简单地更改功能以适合自己的需求,然后将其指向特定级别或嵌套的Movie Clip实例。

After the stage has been cleared up, we call the createTextField() function again, to create the empty Movie Clip and dynamic text fields.

清除阶段后,我们再次调用createTextField()函数,以创建空的Movie Clip和动态文本字段。

Save your Flash document and preview your work.

保存您的Flash文档并预览您的工作。

It’s as simple as falling off a bike! This example shows the basics of using the component as a starting point that triggers dynamic object creation.

这就像从自行车上摔下来一样简单! 此示例显示了使用组件作为触发动态对象创建的起点的基础知识。

只是开始... (Just the Beginning… )

While the examples presented in this tutorial have shown a few different uses for the NumericStepper component, the possibilities really are without limits. Use your imagination and the skills you’ve developed here, and you can create some interesting applications of the component.

尽管本教程中的示例显示了NumericStepper组件的几种不同用法,但可能性确实是无限的。 利用您的想象力和在这里开发的技能,您可以创建该组件的一些有趣的应用程序。

And when you do create some interesting applications using the component, don’t be shy about sharing them with the rest of the SitePoint community. We’re looking forward to seeing what you’ve done!

而且,当您确实使用该组件创建了一些有趣的应用程序时,请不要与其他SitePoint社区共享它们。 我们期待看到您所做的一切!

翻译自: https://www.sitepoint.com/numeric-stepper-component/

wpf flash 组件

相关资源:WPF 播放 Flash
最新回复(0)