Hello I have a problem with validating identical values in input boxes. Event called must be "on change" value of input box, but I have a problem with numbers.I want to write for example "17", but before there was value "1" already inputted in previous input box and it will report error because before we write 17 there is "1"- 7. So there is my question: is there a option to check identical values with this event type while dodging this error?
var textInput:Array = new Array();
for(var a:Number = 0;a < 2; a++){
textInput[a] = new TextField();
textInput[a].type = "input";
textInput[a].y = 10+20*a;
this.addChild(textInput[a]);
textInput[a].addEventListener(Event.CHANGE, changeListener);
}
function changeListener (e:Event):void {
if(Number(e.target.text)>22){
e.target.text = "0";
}
//problem area
else{
if(Number(textInput[1].text) == Number(textInput[0].text)){
e.target.text = "0";
}
}
}
There is simple code with just 2 input boxes but in my project it more complex. How do define problem area to have possibility write "17" when we have "1" already in 1st input box.
You can't both allow and disallow the same thing at the same time, obviously.
What you can do is validate the field on CHANGE and only mark it as valid or invalid (perhaps with some error styling) and on FOCUS_OUT you reset the text if it's not valid.
Something like this:
var valid:Boolean = true;
input.addEventListener(Event.CHANGE, validate);
input.addEventListener(FocusEvent.FOCUS_OUT, commit);
function validate(e:Event):void {
var input:TextField = e.currentTarget as TextField;
var value:Number = Number(input.text);
if(value > 22){
valid = true;
input.backgroundColor = 0xff0000; // error style
}else{
valid = false;
input.backgroundColor = 0xffffff; // normal style
}
}
function commit(e:FocusEvent):void {
if(!valid){
var input:TextField = e.currentTarget as TextField;
input.text = "0";
input.backgroundColor = 0xffffff; // normal style
}
}
Also I would recommend that you encapsulate this stuff in a class that extends TextField.
Related
I make a textfield named 'nama' and button 'ayo'. I want to make the button disable when the textfield is empty. But when I try this code my button not disable and still working. I want the button disable before the textfield filling.
stop();
menumulaikuis();
var namasiswa:String;
var nama:TextField = new TextField();
namasiswa = nama.text;
nama.addEventListener(Event.CHANGE,handler);
function handler(event:Event){
if (nama.text) {
ayo.enabled = true;
ayo.visible = true;
} else {
ayo.enabled = false;
ayo.visible = false;
}
}
You have some little problems in your code :
You should add your text filed to the stage using addChild() :
var nama:TextField = new TextField();
addChild(nama);
If your text field is for user's input, so its type should be input :
nama.type = 'input';
To verify if a text field's text is empty, you can simply do :
if(nama.text == ''){ /* ... */ }
So your text field's change handler can be like this :
function changeHandler(event:Event): void
{
if (nama.text != '') {
ayo.enabled = true;
ayo.visible = true;
} else {
ayo.enabled = false;
ayo.visible = false;
}
}
Hope that can help.
If the Text Field isn't for user input, you cannot use the Event.CHANGE listener.
I suggest changing Event.CHANGE to Event.ENTER_FRAME; that should fix your problem.
I have a problem with formatting the text field. I have buttons + and - to size of text. TextField class has property defaultTextField for new text formatting. And when I change defaultTextFormat size property - whole text's size changes. I have searched for solution everywhere and I haven't found it yet. Text editor WISWYG (I am not sure if name is right) is working well with just changing defaultTextFormat property while I have issue. Maybe it happens because of difference between flash and AIR (editor on flash and my app on AIR). Please help.
Here code to set/get TextFormat:
public function set selectionTextFormat(value:TextFormat):void {
var begin:int = _textField.selectionBeginIndex;
var end:int = _textField.selectionEndIndex;
if (begin == end)
{
_textField.defaultTextFormat = value;
}
else
{
_textField.setTextFormat(value, begin, end);
}
}
public function get selectionTextFormat():TextFormat
{
var begin:int = _textField.selectionBeginIndex;
var end:int = _textField.selectionEndIndex;
if (begin == end)
{
return _textField.defaultTextFormat;
}
return _textField.getTextFormat(begin, end);
}
And code to change format:
private function setFormat(property:String, value:*):void
{
var tf:TextFormat = TextFormatter.TF.selectionTextFormat;
tf[property] = value;
TextFormatter.TF.selectionTextFormat = tf;
}
EDIT : IMAGE ON DROPBOX FOR EXPLANATION:
https://dl.dropboxusercontent.com/u/237572639/Capture.PNG
EDIT 2: IMAGE OF WHAT I NEED (CODE IS ABSOLUTELY SAME!) (WYSIWYG editor)
https://dl.dropboxusercontent.com/u/237572639/WYSIWYG.PNG
To change all future typed text, you can try this code ( the result is visible here ):
var text_input:TextField = new TextField();
text_input.border = true;
text_input.type = 'input';
text_input.text = 'hello world!';
addChild(text_input);
var new_fmt:TextFormat = new TextFormat();
btn_color.addEventListener(
MouseEvent.CLICK,
function(e:MouseEvent):void {
// set the new text color
new_fmt.color = 0xFF0000;
}
)
btn_size.addEventListener(
MouseEvent.CLICK,
function(e:MouseEvent):void {
// set the new text size
new_fmt.size = int(txt_size.text)
}
)
text_input.addEventListener(Event.CHANGE, function(e:Event):void {
text_input.setTextFormat(new_fmt, text_input.caretIndex-1);
})
Of course, this is just a manner to do what you want, you have to adapt it to your need and improve it.
I am making a calculator with a dynamic text box, buttons from 0-9, and +,-,/,*,=, and clear button. but for some reason everytime I press the buttons on my calculator, they don't show up in my dynamic text box like I need them to. My problem specifically is how do I make it that when the number buttons on my calculator are pressed, the numbers show up in my dynamic text box, like a proper calculator. I would really appreciate your help.
Here is my code:
import flash.events.MouseEvent;
var numbers:Array= [btnNum0,btnNum1,btnNum2,btnNum3,btnNum4,btnNum5,btnNum6,btnNum7,btnNum8,btnNum9];
var operations:Array = [btnAdd_,btnSubtract_,btnMultiply_,btnDivide_,btnEqual_,btnClear_];
var o:String;
var number1:Number;
var number2:Number;
function addListeners():void
{
for(var i:uint = 0; i < numbers.length; i++){
numbers[i].addEventListener(MouseEvent.CLICK, pressNumber);
}
for(i = 0; i < operations.length; i++){
operations[i].addEventListener(MouseEvent.CLICK, pressOperations);
}
btnClear.addEventListener(MouseEvent.CLICK, clearAll);
btnDot..addEventListener(MouseEvent.CLICK, addDot);
}
function pressNumber(event:MouseEvent): void
{
// find name of button pressed
var instanceName:String = event.target.name;
// get the number pressed fom the instanceName
var getNum = instanceName.charAt(6)
if(output.text == "0"){
output.text = "";
}
output.appendText(getNum);
}
function pressOperations(event:MouseEvent): void
{
var instanceName:String = event.target.name;
var currentOperator:String;
currentOperator = instanceName.slice(3,instanceName.indexOf("_"));
trace(currentOperator)
}
function clearAll(event:MouseEvent): void
{
output.text = "";
number1 = NaN;
number2 = NaN;
}
function addDot(event:MouseEvent): void
{
if(output.text.indexof(".") == -1){
output.appendText(".");
}
output.text = "0";
addListeners();
}
You shouldn't try to store information in the instance name. Use objects stored in arrays for the buttons. This is how you can handle everything like this.
Using objects stored in arrays to handle your buttons, the basic code to make a new button and set its properties (store information about the button in the instance):
var aButtons:Array = new Array();var i: int = 0;
for (i = 0; i < 10; i++) {
var mcButton: McButton = new McButton();
mcButton.iButtonValue = i;
mcButton.sButtonName = "Button Number " + i;
aButtons.push(mcButton);
}
Then you can reference the buttons by
aButtons[i]
aButtons[i].iButtonValue
You have first to set a value to your text field, because appendText() method can't append text to an empty text field (nothing will appear).
At the begining of your code:
output.text = '0';
To use property "name" you must set this property after creating instance. Like this:
var btnNum0:Button = new Button();
butNum0.name = "btnNum0";
If you don't do this "name" property will be empty that looks like your case.
UPDATE:
Helloflash is right in his comment to question. At first time I did not see that but in code more mistakes than I thought.
First of all you need to put off your init code from function addDot() to the begining of the code. At now this code will be never called because you try to add listeners into listener addDot().
output.text = "0";
addListeners();
Also change indexof to indexOf and remove extra dot from statement btnDot..addEventListener(MouseEvent.CLICK, addDot);. And big advice/request: use some formatting rules when you write a code (specially about brackets). Your code will be more clean and readable.
i have a problem witch i cant solve, and i wanted to ask what am i doing wrong. The idea should be that when i create the textfield i want to read from it, but it doesnt
function click2(e:MouseEvent):void{
e.currentTarget.removeEventListener(MouseEvent.CLICK, click2);
fx=e.target.x+400;
fy=e.target.y+300;
var i:int;
i=2;
trace(str);
trace(e.target.name);
var line:Shape = new Shape();
line.graphics.lineStyle(1,0xFF0000,2);
line.graphics.moveTo(sx,sy);
line.graphics.lineTo(fx,fy);
this.addChild(line);
var inputField:TextField = new TextField();
inputField.border = true;
inputField.type = TextFieldType.INPUT;
inputField.width = 23;
inputField.height = 18;
inputField.x = (sx+fx)/2;
inputField.y = (sy+fy)/2;
inputField.multiline = false;
inputField.maxChars = 3;
inputField.restrict = "0-9";
str=inputField.text;
addChild(inputField);
}
In this code i create a line, and near it appears a textfield , where you need to input the value of the line, but i can`t get it , when i want to trace the STR value, it is null,the text should be written by the user and i should read it ...
If you want to check the data the user added to the textinput you have to listen for the change-event. After that you can access the provided text.
function click2(e:MouseEvent):void{
...
inputfield.addEventListener(Event.CHANGE, checkInput);
}
function checkInput(e:Event):void {
//receive input value and validate it
var textfield:TextField = e.target as TextField;
var str:String = textfield.text;
...
}
I'm trying to pass the current value of a variable when an a dynamically generated navigation 'node' is clicked. This needs to just be an integer, but it always results in the last node's value.. have tried some different methods to pass the value, a custom event listener, a setter, but I suspect it's a closure problem.. help would be appreciated ;-)
function callGrid():void {
for (var i:Number = 0; i < my_total; i++) {
var gridnode_url = my_grid[i].#gridnode;
var news_category= my_grid[i].#category;
var newstitle = my_grid[i].#newstitle;
var news_content = my_grid[i]..news_content;
var news_image = my_grid[i]..news_image;
var gridnode_loader = new Loader();
container_mc.addChild(gridnode_loader);
container_mc.mouseChildren = false;
gridnode_loader.load(new URLRequest(gridnode_url));
gridnode_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gridLoaded);
gridnode_loader.name = i;
text_container_mc = new MovieClip();
text_container_mc.x = 0;
text_container_mc.mouseEnabled = false;
var textY = text_container_mc.y = (my_gridnode_height+18)*y_counter;
addChild(text_container_mc);
var tf:TextSplash=new TextSplash(newstitle,10,0,4 );
container_mc.addChild(tf);
tf.mouseEnabled = false;
tf.height = my_gridnode_height;
text_container_mc.addChild(tf);
var text_container_mc_tween = new Tween(text_container_mc, "alpha", Strong.easeIn, 0,1,0.1, true);
gridnode_loader.x = (my_gridnode_width+5) * x_counter;
gridnode_loader.y = (my_gridnode_height+15) * y_counter;
if (x_counter+1 < columns) {
x_counter++;
} else {
x_counter = 0;
y_counter++;
}
}
}
function gridLoaded(e:Event):void {
var i:uint;
var my_gridnode:Loader = Loader(e.target.loader);
container_mc.addChild(my_gridnode);
_xmlnewstarget = my_gridnode.name;
//||||||||||||||||||||||||||||||||||||||||
//when a particular grid node is clicked I need to send the current _xmlnewstarget value to the LoadNewsContent function...
//||||||||||||| ||||||||||||||||||||||||
my_tweens[Number(my_gridnode.name)]=new Tween(my_gridnode, "alpha", Strong.easeIn, 0,1,0.1, true);
my_gridnode.contentLoaderInfo.removeEventListener(Event.COMPLETE, gridLoaded);
my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
}
function loadNewsContent(e:MouseEvent):void {
createNewsContainer();
getXMLNewsTarget();
news_category = my_grid[_xmlnewstarget].#category;
var tfnews_category:TextSplash=new TextSplash(news_category,20,16,32,false,false,0xffffff );
tfnews_category.mouseEnabled = false;
newstitle = my_grid[_xmlnewstarget].#newstitle;
var tftitle:TextSplash=new TextSplash(newstitle,20,70,24,false,false,0x333333 );
news_container_mc.addChild(tftitle);
tftitle.mouseEnabled = false;
news_content = my_grid[_xmlnewstarget]..news_content;
var tfnews_content:TextSplash=new TextSplash(news_content,20,110,20,true,true,0x333333,330);
news_container_mc.addChild(tfnews_content);
tfnews_content.mouseEnabled = false;
news_image = my_grid[_xmlnewstarget].#news_image;
loadNewsImage();
addChild(tfnews_category);
addChild(tftitle);
addChild(tfnews_content);
var news_container_mc_tween = new Tween(news_container_mc, "alpha", Strong.easeIn, 0,1,0.3, true);
news_container_mc_tween.addEventListener(Event.INIT, newsContentLoaded);
}
I'm not going to try to read your code (try to work on your formatting, even if it's just indenting), but I'll provide a simplified example:
for (var i = 0; i < my_total; i++) {
var closure = function() {
// use i here
}
}
As you say, when closure is called it will contain the last value of i (which in this case would be my_total). Do this instead:
for (var i = 0; i < my_total; i++) {
(function(i) {
var closure = function() {
// use i here
}
})(i);
}
This creates another function inside the loop which "captures" the current value of i so that your closure can refer to that value.
See also How does the (function() {})() construct work and why do people use it? for further similar examples.
Umm, as mentioned above, the code is a bit dense, but I think you might have a bit of type conversion problem between string and integers, is the "last value" always 0? try making these changes and let me know how you get on.
// replace this gridnode_loader.name = i;
gridnode_loader.name = i.toString();
// explictly type this as an int
_xmlnewstarget = parseInt(my_gridnode.name);
// replace this: my_tweens[Number(my_gridnode.name)] = new Tween(......
my_tweens[parseInt(my_gridnode.name)] = new Tween();
Oh and I think it goes without saying that you should massively refactor this code block once you've got it working.
Edit: after further study I think you need this
//replace this: my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
var anonHandler:Function = function(e:MouseEvent):void
{
loadNewsContent(_xmlnewstarget);
};
my_gridnode.addEventListener(MouseEvent.CLICK, anonHandler);
Where your loadNewsContent has changed arguements from (e:MouseEvent) to (id:String)
Firstly, you do not need to call addChild for the same loader twice (once in callGrid) and then in (gridLoaded). Then you can try putting inside loadNewsContent: news_category = my_grid[int(e.target.name)].#category;instead of news_category = my_grid[_xmlnewstarget].#category; As _xmlnewstarget seems to be bigger scope, which is why it is getting updated every time a load operation completes.