This component is used to customize variables. You can specify the name, type, and value of a custom variable in <Var>. The custom variable will be added to global variables for reference by other functions or expressions.
In addition, you can use <Var> to generate an array variable whose index is named in Array variable name (name) + [Index value (index)] format. For example, if name and index in <Var> are respectively set to sss and 1, the value of sss[1] is used. The index value starts from 0. For a common variable (that is, a non-array variable), type cannot be set to number[] or string[]. If type is left empty, the default value number is used.
<Var name="" expression="" type="" threshold="" persist="" index="" values="" const=""/>
Parameter | Type | Mandatory | Description |
|---|---|---|---|
name | String | Yes | Variable name, which is also the index name of a global variable. In expressions, you can use the #Variable name format to reference the current value of the variable. |
expression | Expression | No | Expression or constant corresponding to a variable. A string constant must be enclosed by single quotation marks ('), for example, expression="'my string'". |
type | String | No | Variable type. The options are number, string, number[] (numeric array), and string[] (string array). The default value is number. |
threshold | Number | No | Trigger threshold. When a variable value is changed to be greater than or equal to the threshold, the trigger nested in the variable is executed. |
persist | String | No | Indicates whether to persist a variable. The default value is true. If a variable does not need to be persisted, explicitly set this parameter to false when declaring the variable. After variable persistence is enabled, the system preferentially checks whether a local variable with the same name has a value. If no, the value of expression or the default value is used. If yes, the value of expression is ignored. |
index | String | No | Array variable index. This parameter is mandatory for an array variable. The index value starts from 0. If index is set to 0, the value of name[0] is used. |
values | String | No | Values to be assigned, which are separated by commas (,). Based on the values, the class automatically calculates the array variable size and assigns a value to each common variable. The output value varies according to the value of type. For example, if values is set to 10,20 and type is set to number, the output value is 10.0,20.0. If type is set to string, the output value is 10,20. |
const | String | No | Indicates whether a variable value is a constant. The options are true and false. A constant value will not change after being assigned to a variable. |
Example 1: Use <VariableAnimation> to achieve the effect that the value of a global variable named sss changes from #ani_begin_x to 0 within 300 milliseconds and then remains unchanged.
<Var name="sss">
<VariableAnimation>
<AniFrame value="#ani_begin_x" time="0"/>
<AniFrame value="0" time="300"/>
<AniFrame value="0" time="100000000000000"/>
</VariableAnimation>
</Var> Example 2: Set a threshold and enable the visibility attribute of an image to update to toggle when the value of expression is greater than or equal to the threshold.
<Image name="img" x="540" y="300" w="200" h="200" src="123.jpg"/>
<Var name="timeTest" expression="#second % 2" threshold="1">
<Trigger>
<Command target="img.visibility" value="toggle"/>
</Trigger>
</Var> Array variable
An array variable can be generated in <Var> through declaration, which facilitates loop operations and reduces development time. An array variable has similar functions as a common variable, and is therefore defined using <Var>.
Methods for declaring an array variable:
1. Declare an array variable and assign a value to one common variable.
<Var name="sss" type="number[]" index="0" expression="1" size="4" const="true" /> <Var name="sss" type="number[]" index="1" expression="2" const="true" /> <Var name="sss" type="number[]" index="2" expression="3" const="true" /> <Var name="sss" type="number[]" index="3" expression="4" const="true" /> <Text x="100" y="400" textExp="#sss[0]+' '+#sss[1]+' '+#sss[2]+' '+#sss[3]" size="30" color="#ffffff" />
In this example, the name parameter indicates the name of an array variable, and the size parameter indicates the array variable size. The size must be declared before the array variable is used.
The value number[] of type indicates a numeric array. You can replace it with string[] to declare a string array variable as required. The index parameter indicates the array variable index, and the expression parameter indicates the value to be assigned to a common variable with the specified index value in the array variable.
2. Declare an array variable but do not assign a value.
<Var name="sss" type="number[]" size="4" const="true" />
In this example, a numeric array variable named sss is declared. The size of the array variable is 4. The const parameter is set to true, indicating that the value assigned to a common variable is a constant and cannot be changed later.
3. Declare an array variable and assign values to all common variables.
<Var name="_Num" type="number[]" values="100,200,300,666,888" /> <Text x="200" y="500" textExp="#_Num[0]+' '+#_Num[1]+' '+#_Num[2]+' '+#_Num[3]+' '+#_Num[4]" size="30" color="#FFFFFF" />
In this example, the values parameter specifies all values to be assigned. Based on the values, the class automatically calculates the array variable size and assigns a value to each common variable.