summing the numbers after appendText to a textField - actionscript-3

as3 using appendText. I'm simply stringing out numbers like entering from a button press.
key2.addEventListener(MouseEvent.MOUSE_DOWN, thisButkey2);
function thisButkey2 (e:MouseEvent):void{
displayNums.appendText("2") ;
}
key3.addEventListener(MouseEvent.MOUSE_DOWN, thisButkey3);
function thisButkey3 (e:MouseEvent):void{
displayNums.appendText("3") ;
}
How do I total the string text into one number? I'd like to find out if it is > 100.
I solved it - It simply needed to be turned into a Number.
keyEnter.addEventListener(MouseEvent.MOUSE_DOWN, thisButEnter);
function thisButEnter (e:MouseEvent):void{
totalSum = Number(displayNums.text)
if ( totalSum > 100){
clearNums();
}
}

Here's a way to do this:
stage.addEventListener(KeyboardEvent.KEY_DOWN,processentry)
var entry:int;
var sum:int;
function processentry(e)
{
trace ("hello "+e.keyCode)
entry = e.keyCode - 48;
sum += entry;
trace(sum);
}
The first 'trace' simply makes clear what's happening when you press a key. We subtract 48 from the keyCode because the ASCII keyCode for '0' is '48' and the other digits' keyCodes go up sequentially. It should be easy for you to adapt this to your own case. Just let your textfield append the current 'entry'. You can put the 'sum' into another textfield or do something else with it.

Related

Input text changes an integer? AS3

I am making a small AS3 function of the Collatz conjecture.
This is the code:
import flash.events.Event;
import flash.events.MouseEvent;
var numb:int=0
var amount:int=0
button.addEventListener(MouseEvent.CLICK, buttonclick)
function buttonclick(buttonclicked:MouseEvent):void{
numb=int(input.text)
trace(numb)
amount=0
}
stage.addEventListener(Event.ENTER_FRAME, equate)
function equate(equate:Event):void{
dynam.text=amount.toString() + "\n" + numb.toString();;
if(numb !=0 && numb !=1){
if(numb % 2 == 0){
numb=numb/2
amount+=1
}
else{
numb=numb*3+1
amount+=1
}
}
}
However, sometimes the textinput becomes a negative number.
123123123123 turns into -1430928461
12312312312321312 turns into -1715645152
And so on.
I do not know why, but I think it has to do with
numb=int(input.text)
But I do not want to do absolute value, because then the equation would make the wrong results.
If you want it to stop on the negative number (and test it yourself) do this:
if(numb !=0 && numb !=1 && numb>1)
Instead of
if(numb !=0 && numb !=1)
12312312312321312 turns into -1715645152
That amount of digits won't fit into integer data type. Replace each int with Number and test again.
ie : var numb:Number=0; and var amount:Number=0; and numb=Number(input.text);

which method should i use to create a new keyboard using adobe flash

I'm trying to create a new keyboard somehow, for educational purposes.
I've written this code using actionscript 3.I've created an input text field (named it t1) .when the user presses q button on keyboard(which has an ASCII aquals 81 ) I want the letter b to be printed out on the text field so i've written this code :
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressing);
function pressing(event:KeyboardEvent):void
{
//trace(event.keyCode);
if(event.keyCode==81)
t1.replaceSelectedText("b");
}
the problem was that the method replaceSelectedText prints the tow letters on the screen(q&b) which method can i use instead?
Any help would be appreciated.
When using the replaceSelectedText method, you first need to select the text you want to replace. This can be done with the "setSelection" method. This from the adobe help website:
setSelection(beginIndex:int, endIndex:int):void
"Sets as selected the text designated by the index values of the first and last characters, which are specified with the beginIndex and endIndex parameters."
At the moment, since you don't have any text selected, it appears to just be adding the text "b" as it's replacing nothing. Therefore, you should try first selecting the "q".
Alternatively, you can just use a different method. from the adobe help website:
replaceText(beginIndex:int, endIndex:int, newText:String):void
"Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter."
This would cut out an extra line of code.
I haven't actually done this myself, so if that doesn't work, here's the link to the adobe help page for Text Fields: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html
I think that to do what you are looking for ( replace a char when it's typed ), a KeyboardEvent.KEY_DOWN is not enough, because when that event is fired, the text is not yet changed, so any change that you did in its handler to your text field will not cancel the insertion of the current typed char. Also, using KeyboardEvent.KEY_UP ( in addition to KeyboardEvent.KEY_DOWN ) will not resolve the problem, because you can fire n times a KeyboardEvent.KEY_DOWN event with the KeyboardEvent.KEY_UP event fired once !
So, I think that the best event that can do the job is the Event.CHANGE event which is fired every time the text of your text field is changed, so you can do like this :
// is there a char to replace ?
var replace_char:Boolean = false;
// the position of the char that we want to replace
var char_position:int = -1;
var text_input:TextField = new TextField();
text_input.type = 'input';
text_input.border = true;
text_input.addEventListener(Event.CHANGE, onTextChange);
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0){
text_input.replaceText(char_position, char_position + 1, 'b');
replace_char = false;
}
}
addChild(text_input);
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
function _onKeyDown(e:KeyboardEvent):void {
if(e.keyCode == 81) {
replace_char = true;
char_position = text_input.selectionBeginIndex;
}
}
EDIT :
To use a list of keys and their equivalents, you can use an object to stock your keys like this :
// list of all keys (chars) and their equivalents
var chars:Object = {
81: 'b', // q => b
83: 'v', // s => v
68: 'c' // d => c
// other chars
}
var char_to_replace:int = -1;
// other instructions
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0 && char_to_replace >= 0){
// get the equivalent of the pressed key from chars object using : chars[key_pressed]
text_input.replaceText(char_position, char_position + 1, chars[char_to_replace]);
replace_char = false;
}
}
// other instructions
function _onKeyDown(e:KeyboardEvent):void {
if(chars[e.keyCode]) {
replace_char = true;
// save the last pressed key to get its equivalent, or save this last one directly, to replace it next
char_to_replace = e.keyCode;
char_position = text_input.selectionBeginIndex;
}
}
Hope that can help.

Count how many times a function is called

I'm making a game in AS3.
I was wondering if it's possible to count how many times a function is called and do something when it has been called 5 times for exemple. (and then it's restarting to count at 0).
If we take an exemple, what would it be ?
Somethink like :
movieClip.addEventListener(MouseEvent.CLICK, functionExemple, false, 0, true);
function functionExemple(e:MouseEvent):void{
//do something;
count 1;
if (count = 5){
doThat();
count = 0;
}
So the function doThat would be called every 5 times.
I know the code is wrong. It's just in order to explain as good as possible what I meant.
Thank you for your help,
There are some errors in your example.
var count:int=0; //variable declaration
function functionExemple(e:MouseEvent):void{
//do something;
count++;//<=>count 1;
if (count == 5){//<=>if (count = 5){
doThat();
count = 0;
}
}
My initial guess would be to set a global variable that acts as a counter then when the function is called - increment its value. When value hits 5 take action. I would also guess using some form of "getter" to get the counter value would also help. This is my initial stab at it.

Checking for same values using if statement in actionscript?

I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}

is number between 2 values

What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.
Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}
Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.