AS3 in Adobe Animate CC - actionscript-3

Scene 1, Layer 'Layer 1', Frame 1, Line 2, Column 12 1067: Implicit coercion of a value of type String to an unrelated type Number.
Name of Dynamic text in Stage : "benzin_txt"
benzin_txt.text -= 1;

Setting the text of a textfield requires a string. You're trying to give it a number.
If you want to interpret the value of a TextField as a number and then subtract its contents by 1 then you must first parse the string. Then turn it back into a string.
var origionalText:String = benzin_txt.text;
var asNumber:Number = parseInt(origionalText);
asNumber--;
benzin_txt.text = asNumber.toString();
//or
benzin_txt.text = String( int(benzin_txt.text) - 1);

Content of a textField is "String" not "Number". So you can't do numeric operations on it. First you have to Convert it to Number or a related type:
number = benzin_txt.text as Number;
or
number = Number(benzin_txt.text);
then reconvert it to String so you can use it as text for your textfield:
benzin_txt.text = String(number);
or
benzin_txt.text = number.toString();
Simply:
benzin_txt.text = String(Number(benzin_txt.text)-1);
Regards.

Related

My input field is cleared automatically after four digits

I am trying to convert integers to Indian currency and return them back to the same input tag but it works well for four digits after that when I enter the fifth digit it again starts from the fifth digit. Please help me out. I am new to StackOverflow.
function currency(element) {
let ele = document.getElementById(element.id);
let num = ele.value
let number = (parseInt(num))||0;
console.log(number.toLocaleString('en-IN'));
var int = number.toLocaleString('en-IN');
document.getElementById(element.id).value = int.toString();
}
it gives output 1,234 for 1234
but when I enter 12345 it returns 1 to the input tag
After you type 1234, your function changes the <input>'s value to 1,234. But when you type a 5, your function tries to read an integer from 1,2345. The comma isn't a valid digit in base 10, so parseInt stops parsing at 1 (step 13 in the parseInt algorithm). You'll need to sanitize the <input>'s value before passing it to parseInt:
let num = ele.value.replace(/[^\d]/g, ''); // remove all non-digits from num
let number = (parseInt(num))||0;

AS3 event.target and var

I have an editable text field (c) in a movieClip, well 3 movielips like this actually named a1, a2 and a3. Movieclips are already on stage. The path to text field in each MC is mc.a1.c, mc.a2.c and mc.a3.c
The initial value for each textfield is set by XML which is also stored in variables with the same names and the movieclip(a1,a2,a3). If the user updates a textfield a CHANGE event listener triggers checkValue function. If the value is greater than my maxValue I want my function to return the text field to its original value and give the user an error message. So if textfield c in mc.a1.c is updated, I'm currently taking the name of its parent (a1) and then trying to reference the variable with the same name so that textfield c will be returned to the initial value held in var a1 (I'll only know which var to reference once a textfield update has been attempted.. hope that makes sense)
I've tried several things but always end up with the variable name, and not its value in the textfield. So, for now I've reverted to populating the field with 0 until I can find an answer.
example code:
aH.t1 is the predefined max value
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
if (thR =="a"&&thN>int(aH.t1.text)){
event.target.text = 0; //I want the reference var a(x)and have its value in the text field
aH.errorMsg.text = "The number cannot be greater than 10 so the original value has been restored";
}
}
As you can probably tell my my code, I'm not a developer and I've already looked here in search of but can't seem to get grasp it...Is it me?
reference variable AS3
AS3: Using string as variable
Is what I'm trying to do achiveable in AS3?
Thanks to guidance from dene the solution looks like this:
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
var thN:int = (event.target.text);
var thov:int = root[event.target.parent.name];
if (thR =="a"&&thN>int(aH.thrsh.t1.text)){
event.target.text = thov;
aH.errorMsg.text = hclbl[12];
}
}
Use event.target in the listener function to reference the text field that changed:
var maxValue = 5;
myTextField.addEventListener(Event.CHANGE, textListener);
function textListener(event:Event)
{
var tf = event.target as TextField;
var currentValue = parseFloat(tf.text);
if (currentValue > maxValue) {
tf.text = getOriginalValue(tf);
}
}
function getOriginalValue(tf:TextField) : Number
{
// Assuming the textfield's parent is named "a" + number (eg. a1, a2 etc.)
// Get the number of the parent by ignoring the character at index 0
var parentName = tf.parent.name;
var parentNumber = parentName.substring(1);
// Now you can use parentNumber to access the associated variable (a1, a2, etc)
// Assuming these variables are defined on the root (main timeline).
var originalValue = root["a" + parentNumber]
// If the variables are stored as Strings, this line is needed to convert it to a Number type
originalValue = parseFloat(originalValue)
return originalValue;
}

how to increase a variable which name You give

how do i create a function where i put the name of the variable I want to make Math with,
what i mean is :
so somewhere in a enterFrameFunction i call effectOne and give its 1st parameter to be with the name of "myGravity"
private function effectOne(TriggeringVariable:String, minYposition:Number = 300, maxYposition:Number = 100):Number
{
var myReturnVariable:Number = minYposition + maxYposition;
TriggeringVariable +=10;
if(TriggeringVariable > 980) TriggeringVariable = 980;
if(TriggeringVariable > 500) minYposition = minYposition / 2;
if(TriggeringVariable > 980) maxYposition = maxYposition / 2;
return myReturnVariable;
}
but it gives me ->
1067: Implicit coercion of a value of type String to an unrelated type Number.
how do i make this ?
you can do it like this:
this[triggeringVariable]+=10;
That will get the variable by the name you gave, so if triggeringVariable is "x", it will increase the x value of your instance.
But that will only work when that variable actually exists, and that cannot be checked at compile time. So if you make mistakes, you'll get runtime errors.

Accessing a random Object AS3

I have 9 different movie clips and they are called MC1, MC2, MC3,...,MC9. Then I want to add them randomly. I made a randomizer and it generate numbers from 1 to 9 randomly. And now how can I add them using the random number that I generate. Example:
var box11:MC[the random number] = new MC[the random number], where in the place of [the random number] will stay that number, for ex: var box11:MC2 = new MC2.
And would it be also possible to do the same with box value? For example box[i][j] for different values od i and j will become box11, box32...?
You can use flash.utils.getDefinitionByName() for this.
Example:
var theClass:Class = getDefinitionByName("MC" + randNum) as Class;
var instance = new theClass();
For dynamic istances (the box), see my answer to this question:
How to push instantiated MC into Array dynamically?

action script 3.0 a string variable that should have only number

In action script var x:String="123abc" I have to check any character, for that string.
i.e. here "abc" is that string so I give an alert that this string should contain only numbers.
How can I do that?
Do you mean to say that you would like to dispatch an alert if a string contains letters
var testVar:String = '123abc';
var pattern:RegExp = /[a-zA-Z]/g;
if( testVar.search(pattern) == -1 )
{
//all good there's no letters in here
}
else
{
//Alert, alert, letter detected!
}
the "pattern" variable is a RegularExpression that's adaptable. Here I'm only checking for letters... If you need more control, get more info about RegularExpressions or come back here with the specific filter you'd like to implement.
I think you are looking for Regular Expression support in AS3.
If the user is inputting text via a TextField then you can set the restrict property to limit the characters that can be entered into the textfield:
textFieldInstance.restrict = "0-9";
TextField.restrict documentation:
http://livedocs.adobe.com/flex/3/langref/flash/text/TextField.html#restrict