Display a multiplication value between two dynamic text fields - actionscript-3

I have these variables:
dynamic text 1 called ampere.
dynamic text 2 called tesla,
and the slider called ampereSlid.
I want to show the result from a mathematical operation that happened in dynamic text 1 on dynamic text 2 just by using the slider.
Here's my code:
stop();
import fl.events.SliderEvent;
import fl.controls.TextArea;
ampere.text = "0";
ampereSlider.value = 0;
tesla.text= "0"
ampereSlider.addEventListener (SliderEvent.CHANGE, changeAmpere);
function changeAmpere (event:SliderEvent):void{
ampere.text = event.target.value+"A";
}
ampereSlider.addEventListener (SliderEvent.CHANGE, changeTesla);
function changeTesla (event:SliderEvent):void{
var tsl:Number = 1.25664;
var tslnum = event.target as TextArea;
tesla.text = event.target.value;
}

Related

How would i make the buttons pressed in my calculator show up in my dynamic text box

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.

AS3 - Cannot tab through textfields nested in Movieclips

I"m having an issue at the moment trying to tab through textfields within movieclips (called through a for loop). It no matter which one is selected, hitting tab will always select the first textfield created and won't move from there. Not even using tabbing index works
Here's the code within the movieclip (the textfields are physical objects)
import flash.text.TextField;
import flash.events.FocusEvent;
import fl.managers.FocusManager;
import flash.events.Event;
var str:String = "Default";
textf.text = str;
textf.textColor = 0x848484;
hlborder.visible = false;
var focusManager:FocusManager = new FocusManager(this);
textf.addEventListener(FocusEvent.FOCUS_IN, tffin);
textf.addEventListener(FocusEvent.FOCUS_OUT, tffout);
function tffin(e:Event):void{
textf.borderColor = 0x0066FF;
hlborder.visible = true;
if(textf.text == str){
textf.text = "";
}
}
function tffout(e:Event):void{
textf.borderColor = 0x000000;
hlborder.visible = false;
if(textf.text == ""){
textf.text = str;
}
}
Here's where they are added to the main timeline
var carr:Array = new Array();
for(var i = 0; i<10; i++){
carr.push(new custField());
carr[i].y = i*30;
carr[i].x = 30;
addChild(carr[i]);
carr[i].textf.tabIndex = i;
}
Have you tried putting each TextField in a seperate movieclip? I think that should most likely fix the problem.
Same issue. I have MovieClips with 3 TextFields and single TextFields. Helped this:
Remove all tabIndex
Set on MovieClips tabChildren = true
Now tab goes from left to right and from top to bottom even throw child TextFields.

How to make an else and if statement with dynamic text field?

I am trying to make a dynamic text field that populates with a number when clicked. If 2 is shown in the text field, and the user clicks it, then 1 should show, and if 1 is shown, and the user clicks, 1 should show again. But with my code there seems to be a problem. Instead, when 2 is shown and it gets clicked, 1 shows, but if you click it again 2 doesn't show back up. How can I solve this?
import flash.events.MouseEvent;
var onoff:Number;
onoff = 2
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
function tick(event:MouseEvent)
{
if(onoff = 1)
{
onoff = 2
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
}
else(onoff = 2)
{
onoff = 1
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
}
}
To set a value you should use =
The operator equal is ==
so when you test you should do :
if(onoff == 1)
All of this is well explained in this link

Convert percentage to decimal

I'm trying to convert a percent to a decimal once a button is clicked.
I have an input text field for user input and another under it, which is
a dynamic text field to just show the result after the button is clicked.
I have two classes: main and my utility class for the math.
My problem is I don't know how to call the function from
the util class in the main class to get this working.
I can't get this last part working. here is my code:
public class Main extends Sprite
{
private var pResult:TextField;
private var pResult2:TextField;
public function Main()
{
super();
// calling all the functions below
// adding my graphics to the display
var baseDBase = new PDBase();
this.addChild(base);
base.x = -70;
base.y = 30;
//changing the font format
var format:TextFormat = new TextFormat();//adding the object
format.size = 14;//font sizing
format.align = TextFormatAlign.LEFT;//font align
//result text field
pResult = new TextField();//adding the object
this.addChild(pResult);//displaying the object
pResult.border = true;//setting the border
pResult.borderColor = 0x30FF00;//setting border color
pResult.textColor = 0x000000;//setting the font color
pResult.x = 28;//position left or right
pResult.y = 70;//position up or down
pResult.width = 142;// changing width
pResult.height = 20;// changing height
pResult.type = TextFieldType.INPUT;//making sure the text area is for input
pResult.defaultTextFormat = format;//sets text format to defult
pResult.maxChars = 32;//text limit of characters
pResult.restrict = "0-9.";//fonts used only
pResult2 = new TextField();//adding the object
this.addChild(pResult2);//displaying the object
pResult2.border = true;//setting the border
pResult2.borderColor = 0x30FF00;//setting border color
pResult2.textColor = 0x000000;//setting the font color
pResult2.x = 28;//position left or right
pResult2.y = 96;//position up or down
pResult2.width = 142;// changing width
pResult2.height = 20;// changing height
pResult2.type = TextFieldType.DYNAMIC;//making sure the text area is for input
pResult2.defaultTextFormat = format;//sets text format to defult
pResult2.maxChars = 32;//text limit of characters
pResult2.restrict = "0-9.";//fonts used only
var button:Button = new Button("Calculate");
this.addChild(button);
button.x = 10;
button.y = 130;
button.addEventListener(MouseEvent.CLICK, btn_EventListener);
}
private function btn_EventListener(e:MouseEvent):void
{
MathUtil.percentToDecimal(percent)
this.pResult2.text = percent;
}
}
public class MathUtil
{
public function MathUtil()
{
}
public static function percentToDecimal(percentValue:Number):Number
{
var percent:Number = percentValue * 100;
var roundedDecimal:Number = Math.round(percent);
var percentResult:String = roundedDecimal;
return percentResult;
}
}
I'm also getting this error:
1067: Implicit coercion of a value of type Number to an unrelated type String.
On var percentResult:String = roundedDecimal;
this.pResult2.text = String(MathUtil.percentToDecimal(percent));
Note that your function returns a value, so if you will do this, the returned value will be assigned to textfield. "String()" is casting a returned number to string.
Edit:
Ah, and here you can use casting:
var percentResult:String = String(roundedDecimal);

I am trying to test if a text input box has focus

I am fairly new to AS3, so here is my problem.
I have two text input boxes p1 and p2. I want to do a conditional test to see if p1 has the focus. If it does not, then p2 must have the focus. Here is some code I am trying to get to work.
if ((Selection.getFocus()) == (p1totalScore.text)){
p1Score();
} p2Score();
Thanks for your help.
David
Without a FocusManager, you could test what the stage is returning for focus:
(If you have textInput1 and textInput2 on the art-board)
import flash.events.MouseEvent;
import fl.controls.TextInput;
var textInput1:TextInput;
var textInput2:TextInput;
stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(event:MouseEvent):void
{
if(stage.focus == textInput1.textField)
trace("text field 1 has focus.");
else if(stage.focus == textInput2.textField)
trace("Text field 2 has focus.");
}
I think a better approach than you're attempting is to add event handlers for focus change:
import fl.controls.TextInput;
import fl.managers.FocusManager;
import flash.events.FocusEvent;
var textInput1:TextInput;
var textInput2:TextInput;
var focusManager:FocusManager = new FocusManager(this);
textInput1.addEventListener(FocusEvent.FOCUS_IN, textInput1FocusHandler);
textInput2.addEventListener(FocusEvent.FOCUS_IN, textInput2FocusHandler);
function textInput1FocusHandler(event:FocusEvent):void
{
trace("textInput1 has focus.");
}
function textInput2FocusHandler(event:FocusEvent):void
{
trace("textInput2 has focus.");
}
Adobe will get you 99% of the way:
'http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/managers/FocusManager.html'
I just changed the function to return the name of the control that has focus, instead of the instance name that their example returns.
private function focusChange(e:FocusEvent):void {
var ti_now:InteractiveObject;
ti_now = fm.getFocus();
trace("Focus now: ", ti_now.name);
}