Dynamic text click counter for AS3

15 posts

Flag Post

I’m trying to make a game where when you click a certain button, the dynamic text goes from 0 to 1, I know how to program this in AS2 but not AS3, also, the game is made in Flash CS5, for. mobile, so Im using Flash Lite 4.

 
Flag Post

AS3 solution:

1) Take the AS2 solution and remove any _
2) ???
3) Profit.

 
Flag Post

Ideally:
- The dynamic text is an instance of a class which extends TextField and contains an additional ‘value’ variable. It also contains a public ‘increment’ function which simply does this.value++; and this.text = value.ToString();
- The button dispatches an event when clicked, which bubbles up to the appropriate level.
- At the aforementioned appropriate level, something catches it and calls the dynamic textfield increment() function.

Edit: /\ And here I feared I might sound slightly condescending. Thanks Draco for putting things in perspective. ^^

 
Flag Post

It doesn’t work because you need to apply the codes to the button and things

 
Flag Post

I don’t know if you’ve coded in AS3 before, but in AS3 we don’t use the on() method, we use listeners. Also, how are you adding your button? Are you adding it through code or directly adding it to stage?

Here is my solution:
1) Add a new Flash AS3 document (.fla).
2) Draw a button, add the button to the library, and delete the button.
3) Click on Library. (If you cannot find the Library panel, switch to Small Screen mode (Click on the top-right corner of the screen, where there is a drop-down panel, and click on Small Screen) and the Library Panel is second from the top on the right-hand side.)
4) Go down to the button’s symbol name, and click on its corresponding AS linkage column.
5) Type in the AS linkage column “ImportantButton”.
6) You AS3 Flash Document is ready. Press Ctrl+Enter. You should see a blank stage. This is because we have not added any code yet. If there is something on the stage, delete it.
7) Open Properties, and in the Class text box, type Main.
8) Close the Flash document, and find its file location. Place it in a folder, name the folder whatever you want.
9) Open Flash, and open a new AS3 Class. Name the Class Main.
10) Place the Main Class in the same file location as the Flash document you just made. Do not seperate the documents into nested folders. You should be able to open the documents from the same place, rather than diving into different folders to get them. This step is very important.
11) Now, for the actual code. I have made annotations, you can delete them if you understand this code.

 package{
	
	//importing the necessary inbuilt classes that Flash has
	import flash.display.Sprite;
	import flash.display.TextField;
	
	public class Main extends Sprite{

		//declaring a variable for the button and corresponding text field and text field's corresponding text format	
		private var importantButton:ImportantButton = new ImportantButton();
		private var textField:TextField = new TextField();
		private var textFormat:TextFormat = new TextFormat();
		
		public function Main{
			//add the text field and button to stage
			//The textfield is two hundred pixels underneath the button
			//so don't make the button too big
			//then the properties of the text field and text format are defined
			addChild(importantButton);
			addChild(textField);
			textField.y = 200;
			textField.text = "0";
			textField.width = 100;
			textField.height = 500;
			textField.wordWrap = true;
			textField.type = TextFieldType.DYNAMIC;
			textFormat.font = "Arial";
			textFormat.size = 30;
			textFormat.color = 0x0000FF;
			//apply the text field text format
			textField.setTextFormat(textFormat);
			//if the button is clicked, perform this function
			importantButton.addEventListener(MouseEvent.CLICK, changeText);
		}
		
		private function changeText(e:Event):void{
			//change the text field text
			textField.text = "1";
			//then disable the clicking function because it is no longer needed
			importantButton.removeEventListener(MouseEvent.CLICK, changeText);
		}
	}
}

Copy, paste and run (Ctrl+Enter). If you have followed these steps correctly, the button will change the text field text from 0 to 1. Note that it does not do anything else. Also note to be careful when using dynamic text boxes, they can be altered on some operating systems. To be on the same side, make the text field static. Change this line of code:

 textField.type = "DYNAMIC";

to this line of code:

textField.type = "STATIC";

Otherwise, WYSIWYG. A button that changes text field text from 0 to 1. No more.

Hope this helps. :D

 
Flag Post

Never mind – jasonjie88 already covered everything I was going to post, except in more detail and with more explanation.

 
Flag Post
Originally posted by Draco18s:

AS3 solution:

1) Take the AS2 solution and remove any _
2) ???
3) Profit.

In general, not directed at this cute and lovable dragon, if anyone asks a question about an old and depreciated language like AS2, just nicely recommend that they use AS3 and then try to help them. Don’t just tell them that AS3 is what they should use.

 
Flag Post

Nice. Didn’t realize that bc. formats code at all. Thanks Jason.

<pre>

trace(“hi”);

bc.

trace("hi");
 
Flag Post

Actually, I do have corrections to make to jasonjie88’s post, mostly with respect to text fields.


textField.wordwrap is not properly capitalized and therefore doesn’t compile. Use textField.wordWrap instead.


DYNAMIC” is not an acceptable value for textField.type (it causes a fatal exception). You have to use “dynamic” instead. Or better yet, use TextFieldType.DYNAMIC (which also has the value “dynamic”, but you’ll be told immediately if you get capitalization wrong).
Or even better, just delete that line. Text fields are dynamic by default.

STATIC” is also not an acceptable value for textField.type. Neither is “static”, actually. TextFieldType contains two values: DYNAMIC and INPUT. Static text fields are defined as text fields that can’t be manipulated by code, so it would make no sense to be able to create one via code.

For the record, input text fields can be edited, but dynamic text fields cannot, regardless of operating system. It can be selected if you don’t set textField.selectable to false, but it can’t be edited.


Calling setTextFormat() changes existing text to match the text format, but it doesn’t change future text. If you add or replace text later, the new text will be reverted to the original formatting.

Also, setTextFormat() does not store a reference to the TextFormat object. Instead, it takes the relevant values and applies them as they are at that point. If you call setTextFormat() before setting the relevant properties of the text format, no formatting will be applied at all.

I tested the code, by the way, and as you may have figured out, both the “0” and the “1” are small and black (the default), rather than being large and blue (the settings Jason was trying to apply). Moving the setTextFormat() call to the end made the “0” large and blue, but the “1” was still small and black.

To solve this, set defaultTextFormat = textFormat;. Any new text added will use this default format, which means both numbers will be large and blue (and in Arial, though that isn’t as big a difference).


You know what? Just read the documentation. It covers all of this and more.

Or stick to defining text fields in the IDE if this is too much to take in. (Text fields can take a huge amount of setup. Don’t worry, pretty much everything else in AS3 requires less work to define.)

 
Flag Post

Thanks for the corrections, player_03. The code has been corrected. My laptop was under repair, so I had to code blindly :( Thanks again.

 
Flag Post
Originally posted by lSWATLLAMA:
Originally posted by Draco18s:

AS3 solution:

1) Take the AS2 solution and remove any _
2) ???
3) Profit.

In general, not directed at this cute and lovable dragon, if anyone asks a question about an old and depreciated language like AS2, just nicely recommend that they use AS3 and then try to help them. Don’t just tell them that AS3 is what they should use.

<cute />

See, the funny thing about my post is that I thought he wanted to know how to change a dynamic textbox in AS3. Not how to make a button in AS3 (read his post, he’s asking how to make a dynamic textbox change and that he isn’t sure how, just that he wants it to happen when a button is clicked). Thus my response is technically correct.

:3

 
Flag Post
Originally posted by jasonjie88:
2) Draw a button, add the button to the library, and delete the button.

Holy… Is that really the standard way of using the Adobe flash builder? I need to send an e-mail to the FD developers apologizing for all the times I thought their IDE was cumbersome.

 
Flag Post
Originally posted by Ace_Blue:
Originally posted by jasonjie88:
2) Draw a button, add the button to the library, and delete the button.

Holy… Is that really the standard way of using the Adobe flash builder? I need to send an e-mail to the FD developers apologizing for all the times I thought their IDE was cumbersome.

You can also go Insert → New Symbol and draw your button that way. It doesn’t end up on the stage this way.

 
Flag Post

Thanks mate~!

 
Flag Post

1- Do a dynamic text with an instance name. Example : texto
2- Do a button with other instance name. Example : buton
3- Open the actions window and write:

var anumber:Number = 0;

buton.addEventListener(MouseEvent.CLICK, counter);

function counter(MouseEvent){
anumber =+ 1; //or += 1
texto.text = anumber.toString();
}

// if you want delete the button function just write:
buton.removeEventListener(MouseEvent.CLICK, counter);