Very simple Flash question?

Parrot

New member
This will probably be incredibly simple for anyone who knows Flash, but I know nothing about it, so I need help.

I have a button, a variable called "value", and a dynamic text box in which "value" is displayed. All I want to do is increase "value" by 1 every time the button is clicked, and have the new number displayed in the text box after every click.

In the first frame of the movie, I have the following code:

var value = 0;

In the button, I have the following code:

on (press) {
value = value + 1;
box1.text = value;
}

It seems simple enough, but it doesn't work. Nothing happens.

Any ideas? Thanks in advance.
Thanks a ton, doumbek, that worked perfectly. Thanks also for the explanations... I appreciate it.
 
First, try to avoid words that may or may not conflict with reserved words. "Value" is not one of them, but better safe than sorry.

Second, a text value is by default a string data type. If you managed to get that to work, you might wind up with "01" then "011" then "0111" . . . the + concatenates the value in string data types. So you need to make sure the value is parsed as an integer.

Third, learn to use trace to test your objects. If you were to do this

on (press) {
trace('button clicked');
// etc
}

You will see the trace window doesn't open so your code is not correctly assigned to the button object, or receiving the event. Add a trace to my code below, you will see it does.

------------------------------------------------------------
Try this, step by step, tested, and works.

Create new document.

Create button symbol, name it "myButton." Check "link for Javascript" box.

Draw a box in frame 1 of the button symbol, it doesn't have to be pretty for the test.

Return to the main stage, put an instance of myButton on the stage. In the property inspector, name it "myButton" (this is fine, symbol and instance the same name.)

Create a text box on the stage. Enter a zero into it. In the property inspector, make sure to set the type as "dynamic text" not "static text." Also in the propery inspector, name the instance "myText."

Return to the timeline, and create a new layer or put the following in frame 1 of the existing layer.

_root.myButton.onRelease = function () {
var val = int(_root.myText.text);
val++;
_root.myText.text=val;
}

Note that "int" parses whatever the value of myText is. If you were to enter fsgfsdg in this box, text values are parsed as zero, so the initial value would be zero anyway (so first click=1.)

++ and -- are increment and decrement operators, respectively. Without a value, they inc./dec/. by one. You can also do

val+=2;
val-=5;


Run the program, test it, it works.
 
Back
Top