Can't get functions to work properly - function

So I just got started with auto hotkeys and I'm having a bit o trouble getting my functions and variables to play nicely..
This is what I have:
PSbri0 = C:\Controls\Set_0Bri_PS.bat
PSbri50 = C:\Controls\Set_50Bri_PS.bat
PSbri100 = C:\Controls\Set_100Bri_PS.bat
HPbri0 = C:\Controls\Set_0Bri_HP.bat
HPbri50 = C:\Controls\Set_50Bri_HP.bat
HPbri100 = C:\Controls\Set_100Bri_HP.bat
Run %PSbri0%
current_setting := 0
current_power := 0 ;Power Saver = 0 High performance = 1
setPower(){
global current_power
if(%current_power% == 1){
MsgBox Pow 1
%current_power% := 0
}else{
MsgBox pow 2
%current_power% := 1
}
}
getChange(direction)
{
global current_power
MsgBox dir %direction%
if (%current_power% == 0){
;MsgBox run 0
getChangePS(%direction%)
}
else if (%current_power% == 1){
;MsgBox run 1
getChangeHP(%direction%)
}
}
getChangePS(direction)
{
global current_setting
;MsgBox %current_setting%
MsgBox Direction: %direction%
if(direction == 1){
;MsgBox %current_setting%
if(%current_setting% == 0){
}
else if(%current_setting% == 50){
%current_setting% := 0
Run %PSbri0%
}
else if(%current_setting% == 100){
%current_setting% := 50
Run %PSbri50%
}
}
else if(direction == 0){
;MsgBox %current_setting%
if(%current_setting% == 100){
}
else if(%current_setting% == 50){
%current_setting% := 100
Run %PSbri100%
}
else if(%current_setting% == 0){
%current_setting% := 50
Run %PSbri50%
}
}
}
getChangeHP(direction)
{
global
if(direction == 1){
;MsgBox %current_setting%
if(%current_setting% == 0){
}
else if(%current_setting% == 50){
%current_setting% := 0
Run %HPbri0%
}
else if(%current_setting% == 100){
%current_setting% := 50
Run %HPbri50%
}
}
else if(direction == 0){
;MsgBox %current_setting%
if(%current_setting% == 100){
}
else if(%current_setting% == 50){
%current_setting% := 100
Run %HPbri100%
}
else if(%current_setting% == 0){
%current_setting% := 50
Run %HPbri50%
}
}
}
^F5:: getChange(0)
^F6:: getChange(1)
!^P:: setPower()
I've been looking over the documentation and through other posts online but I cannot seem to find out what I'm doing wrong..
My intended goal is to be able to switch power profiles easily I have the 6 profiles declared in the beginning with thee levels of high performance (HP) and three Power Saver levels(PS)
I want to use Alt+Ctrl+P to change between power options and the F5/F6 to add brightness or dim the screen.
The bat files work perfectly and they change my settings as they should so I know that isn't an issue..
I've already tried without the global declaration in the functions first but that didn't work and neither does it with the declaration.
Thanks for your help guys!

Sorry to say but you are using the % in way too many places
Its use depends on where you are using the variable if it in an expression you do not use % around variables, in most other places you do, next thing is then to know when you're typing in something that accepts an expression, that take a little time to learn but here are two links that may help
http://ahkscript.org/docs/Variables.htm#Expressions
http://www.autohotkey.com/board/topic/118109-hard-rules-when-using-variables/
Here is a little example of how it needs to look
setPower(){
global current_power
if (current_power == 1){
MsgBox Pow 1
current_power := 0
}else{
MsgBox pow 2
current_power := 1
}
}
getChange(direction)
{
global current_power
MsgBox dir %direction%
if (current_power == 0){
;MsgBox run 0
getChangePS(direction)
}
else if (current_power == 1){
;MsgBox run 1
getChangeHP(direction)
}
}
Hope it helps

Related

How to delete/hide in IF-ELSE statment previouse statements in HTML

How can I make that so it will work only one condition at one time? Basically there is a time when A,B, and C can be pick at one time all of them. Example: So if you pick A there should be only one posibility and it is "else if (A != null)", but if there is A and B it should display only "else if (Model.B != null)", but if it is A,B, and C it should display only "else if (Model.SoldOut != null)". So is there any way to do that? Or is there any way to delete A when it display B, and delete A and B when it displays C?
#if (A == null & B == null & C == null)
{
#Resources.Undefined;
}
else if (A != null)
{
#Resources.ResourceManager.GetString(Model.A.GetDisplayValue());
}
else if (Model.B != null)
{
#Model.B
}
else if (Model.SoldOut != null)
{
<p>-</p>
}
You can do it by changing the order of else if statements
#if (A == null & B == null & C == null)
{
#Resources.Undefined;
}
else if (Model.SoldOut != null)
{
<p>-</p>
}
else if (Model.B != null)
{
#Model.B
}
else if (A != null)
{
#Resources.ResourceManager.GetString(Model.A.GetDisplayValue());
}
My attempt at what you're describing is as follows. It's an inversion of your logic.
// IF THEY'RE ALL PICKED
// it should display only "else if (Model.SoldOut != null)"
#if(A && B && C)
{
<p>-</p>
}
// it should display only "else if (Model.B != null)"
else if(A && B)
{
#Model.B
}
// there should be only one posibility and it is "else if (A != null)"
else if(A)
{
#Resources.ResourceManager.GetString(Model.A.GetDisplayValue());
}
else
{
#Resources.Undefined;
}
But, like I said in my comment, it's incredibly difficult to figure out the logic when you're not describing all of the potential cases. E.g. What if 'C' is the only one that's picked? You know?

as3 - Changing position checking for in IF statement

I am trying to catch X.Y positions with an IF statement, and IF the coordinates are true I want it to go on to the next set of coordinates. In the code below I attempted my best but I keep getting the "Cannot assign to a non-reference value." Error.
public function onDd(event:TimerEvent):void
{
if (this.rootClass.world.strMapName == "test" && a1.x = a1.x = 327.1 && a1.y = 249.65)
{
a1.x = 360.7;
a1.y = 204.25;
}
else if (a1.x = 410.15 && a1.y = 204.25)
{
a1.x = 360.7;
a1.y = 204.25;
}
}
You have used a wrong comparison operator
If you want to compare two values you must use == or ===
So your code will become:
public function onDd(event:TimerEvent):void {
if (this.rootClass.world.strMapName == "test" && a1.x == 327.1 && a1.y == 249.65) {
a1.x = 360.7;
a1.y = 204.25;
}
else if (a1.x == 410.15 && a1.y == 204.25) {
a1.x = 360.7;
a1.y = 204.25;
}
}

Odd 1084: Syntax error: expecting rightbrace before end of program

I need a help. It is about rightbrace. Seems there is a problem. I don't know where is the mistake as previous code does not have a problem, but then latter there is a problem. With 1084: Syntax error: expecting rightbrace before end of program.
if (counterEnergy == 1){
energyFlame1.gotoAndStop(1)
energyFlame2.gotoAndStop(2)
energyFlame3.gotoAndStop(2)
energyFlame4.gotoAndStop(2)
energyFlame5.gotoAndStop(2)
}
else if (counterEnergy == 2){
energyFlame1.gotoAndStop(1)
energyFlame2.gotoAndStop(1)
energyFlame3.gotoAndStop(2)
energyFlame4.gotoAndStop(2)
energyFlame5.gotoAndStop(2)
}
else if (counterEnergy == 3){
energyFlame1.gotoAndStop(1)
energyFlame2.gotoAndStop(1)
energyFlame3.gotoAndStop(1)
energyFlame4.gotoAndStop(2)
energyFlame5.gotoAndStop(2)
}
else if (counterEnergy == 4){
energyFlame1.gotoAndStop(1)
energyFlame2.gotoAndStop(1)
energyFlame3.gotoAndStop(1)
energyFlame4.gotoAndStop(1)
energyFlame5.gotoAndStop(2)
}
else if (counterEnergy == 5){
energyFlame1.gotoAndStop(1)
energyFlame2.gotoAndStop(1)
energyFlame3.gotoAndStop(1)
energyFlame4.gotoAndStop(1)
energyFlame5.gotoAndStop(1)
}
else if (counterEnergy == 0){
energyFlame1.gotoAndStop(2)
energyFlame2.gotoAndStop(2)
energyFlame3.gotoAndStop(2)
energyFlame4.gotoAndStop(2)
energyFlame5.gotoAndStop(2)
}
For the sake's sake, make your code simpler...
var EA:Array = [energyFlame1,energyFlame2,energyFlame3,energyFlame4,energyFlame5];
for (var i:int = 0; i < 5; i++)
{
var EFi:MovieClip = EA[i];
EFi.gotoAndStop((i >= counterEnergy)? 2: 1);
}

AS3 Cipher Decoder & Encoder (better answer?)

Last night, my little brother asked me if it were possible to make a program that would substitute every letter for another letter in the alphabet, to turn it into a code, and also turn it back to its normal state.
So I made this: http://www.skyetheguy.com/novatranslationtool
And by all accounts, it does the job, but I feel like, code-wise, it's clunky as all get-out, and I'd really love to improve my knowledge on things like loops...
EDIT: The above has since been updated with DodgerThud's amazing answer and works like a dream. : ) Dreams DO come true!
Also, this is very pie-in-the-sky, but it'd be amazing to get this to work with longer-than-two-characters things. My code definitely wouldn't do that at all. But, you know, all those fun codes with apostrophes in them... good stuff!
stop();
// SET-UP STUFF
inputText.alpha = .5;
outputText.alpha = .5;
inputText.borderColor = 0xCCCCCC;
outputText.borderColor = 0xCCCCCC;
var textinProgress:String;
textinProgress = inputText.text;
if (textinProgress.charAt(0) == "\r") {
inputText.text = "";
}
stage.addEventListener(Event.ENTER_FRAME, refresh_frame1);
function refresh_frame1(e:Event):void {
inputScroll.update();
outputScroll.update();
}
// ORIGINAL ALPHABET
var letters_aArray:Array = new Array();
letters_aArray[0] = "a";
letters_aArray[1] = "b";
letters_aArray[2] = "c";
letters_aArray[3] = "d";
letters_aArray[4] = "e";
letters_aArray[5] = "f";
letters_aArray[6] = "g";
letters_aArray[7] = "h";
letters_aArray[8] = "i";
letters_aArray[9] = "j";
letters_aArray[10] = "k";
letters_aArray[11] = "l";
letters_aArray[12] = "m";
letters_aArray[13] = "n";
letters_aArray[14] = "o";
letters_aArray[15] = "p";
letters_aArray[16] = "q";
letters_aArray[17] = "r";
letters_aArray[18] = "s";
letters_aArray[19] = "t";
letters_aArray[20] = "u";
letters_aArray[21] = "v";
letters_aArray[22] = "w";
letters_aArray[23] = "x";
letters_aArray[24] = "y";
letters_aArray[25] = "z";
// NEW ALPHABET
var letters_bArray:Array = new Array();
letters_bArray[0] = "m";
letters_bArray[1] = "n";
letters_bArray[2] = "b";
letters_bArray[3] = "v";
letters_bArray[4] = "c";
letters_bArray[5] = "x";
letters_bArray[6] = "z";
letters_bArray[7] = "l";
letters_bArray[8] = "k";
letters_bArray[9] = "j";
letters_bArray[10] = "h";
letters_bArray[11] = "g";
letters_bArray[12] = "f";
letters_bArray[13] = "d";
letters_bArray[14] = "s";
letters_bArray[15] = "a";
letters_bArray[16] = "p";
letters_bArray[17] = "o";
letters_bArray[18] = "i";
letters_bArray[19] = "u";
letters_bArray[20] = "y";
letters_bArray[21] = "t";
letters_bArray[22] = "r";
letters_bArray[23] = "e";
letters_bArray[24] = "w";
letters_bArray[25] = "q";
// ENCODE BUTTON
encode_btn.addEventListener(MouseEvent.CLICK, encode_btnclick);
function encode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_aArray[0]) == 0) {
outputText.appendText(letters_bArray[0]);
} else if (textinProgress.charAt(b).search(letters_aArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[1]) == 0) {
outputText.appendText(letters_bArray[1]);
} else if (textinProgress.charAt(b).search(letters_aArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[2]) == 0) {
outputText.appendText(letters_bArray[2]);
} else if (textinProgress.charAt(b).search(letters_aArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[3]) == 0) {
outputText.appendText(letters_bArray[3]);
} else if (textinProgress.charAt(b).search(letters_aArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[4]) == 0) {
outputText.appendText(letters_bArray[4]);
} else if (textinProgress.charAt(b).search(letters_aArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[5]) == 0) {
outputText.appendText(letters_bArray[5]);
} else if (textinProgress.charAt(b).search(letters_aArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[6]) == 0) {
outputText.appendText(letters_bArray[6]);
} else if (textinProgress.charAt(b).search(letters_aArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[7]) == 0) {
outputText.appendText(letters_bArray[7]);
} else if (textinProgress.charAt(b).search(letters_aArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[8]) == 0) {
outputText.appendText(letters_bArray[8]);
} else if (textinProgress.charAt(b).search(letters_aArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[9]) == 0) {
outputText.appendText(letters_bArray[9]);
} else if (textinProgress.charAt(b).search(letters_aArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[10]) == 0) {
outputText.appendText(letters_bArray[10]);
} else if (textinProgress.charAt(b).search(letters_aArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[11]) == 0) {
outputText.appendText(letters_bArray[11]);
} else if (textinProgress.charAt(b).search(letters_aArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[12]) == 0) {
outputText.appendText(letters_bArray[12]);
} else if (textinProgress.charAt(b).search(letters_aArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[13]) == 0) {
outputText.appendText(letters_bArray[13]);
} else if (textinProgress.charAt(b).search(letters_aArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[14]) == 0) {
outputText.appendText(letters_bArray[14]);
} else if (textinProgress.charAt(b).search(letters_aArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[15]) == 0) {
outputText.appendText(letters_bArray[15]);
} else if (textinProgress.charAt(b).search(letters_aArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[16]) == 0) {
outputText.appendText(letters_bArray[16]);
} else if (textinProgress.charAt(b).search(letters_aArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[17]) == 0) {
outputText.appendText(letters_bArray[17]);
} else if (textinProgress.charAt(b).search(letters_aArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[18]) == 0) {
outputText.appendText(letters_bArray[18]);
} else if (textinProgress.charAt(b).search(letters_aArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[19]) == 0) {
outputText.appendText(letters_bArray[19]);
} else if (textinProgress.charAt(b).search(letters_aArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[20]) == 0) {
outputText.appendText(letters_bArray[20]);
} else if (textinProgress.charAt(b).search(letters_aArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[21]) == 0) {
outputText.appendText(letters_bArray[21]);
} else if (textinProgress.charAt(b).search(letters_aArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[22]) == 0) {
outputText.appendText(letters_bArray[22]);
} else if (textinProgress.charAt(b).search(letters_aArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[23]) == 0) {
outputText.appendText(letters_bArray[23]);
} else if (textinProgress.charAt(b).search(letters_aArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[24]) == 0) {
outputText.appendText(letters_bArray[24]);
} else if (textinProgress.charAt(b).search(letters_aArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[25]) == 0) {
outputText.appendText(letters_bArray[25]);
} else if (textinProgress.charAt(b).search(letters_aArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// DECODE BUTTON
decode_btn.addEventListener(MouseEvent.CLICK, decode_btnclick);
function decode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// RECODE BUTTON (scrambles, takes what's in the output and applies the cipher again)
recode_btn.addEventListener(MouseEvent.CLICK, recode_btnclick);
function recode_btnclick(event:MouseEvent):void
{
textinProgress = outputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
Just gonna tell you why I did all the incredibly arbitrary and loopy stuff that I did...
If you do any of the other normal replace things other people have talked about here (from what I've FOUND, at least), you end up replacing your own work at a certain point. For example if you want B to be A, but then you want A to be C, then if you type "BA", you get back "CC" instead of "AC". So my code actually takes each character and spits them all out one at a time.
I did all that tedious ".toUpperCase()" stuff to make sure that this tool was case-sensitive-- so that if I typed in a lowercase or uppercase key, it would always generate the proper one in its place. That's why RegExp didn't quite work for me.
It also takes into account things like line breaks and random symbols not included in the alphabet.
I know for SURE there's simpler, more energy-efficient ways to code this, but I just have no idea how.
I'm gonna expand on my comment and show you how can simplify your code and cut out a lot of it.
First off, you don't need to use Arrays. String objects in essence already are Arrays of byte values. So instead of creating big arrays, you can do this
var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewq";
In order to access a specific character, you can simply call the indexOf method on the string.
var indexOfJ = alphabet.indexOf("j");
trace(indexOfJ); //puts out 9
And you already now how to get the character of a string from a specific index.
var charAtIndex7:String = alphabet.charAt(7);
trace(charAtIndex7); //puts out "h"
Edit fom here:
Looks like I was only half-awake when I wrote that answer last night. The issue with the double for-loop is simply that it will always run the whole way through, even if it found an if clause that gets accepted. You already figured out on
your own that the last else-clause causes the input character to be printed 25 times. So, lets fix it.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var anyHit = false;
for(var i=0;i<alphabet.length;i++){
if(textinProgress.charAt(b).search(alphabet.charAt(i)) == 0){
result += substituton.charAt(i);
anyHit = true;
break;
}
else if(textinProgress.charAt(b).search(alphabet.charAt(i).toUpperCase()) == 0){
result += substituton.charAt(i).toUpperCase();
anyHit = true;
break;
}
else if(textinProgress.charAt(b) == "\r"){
result += "\n";
anyHit = true;
break;
}
}
if(anyHit == false){
result += textinProgress.charAt(b);
}
}
output.text = result;
}
I had a coworker test this code for me earlier today, and it should work as intended.
But that's not all we can do to simplify your code. We can cut out the second if clause by defining our alphabet and substitution variables with both lowercase and uppercase letters.
var alphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
You should also be able to put in special symbols into your alphabet and substitution string, like \r for example.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
I'm not entirely sure if this will work, but it should.
Now you have already considerably shortened your code, but we can even shorten it further. Is the inner for loop really neccessary? Because our alphabet and our substitution are the same length, maybe we can simplify this even more.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
Putting it all together.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
function encode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, alphabet, substitution);
}
function decode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, substitution, alphabet);
}
function recode_btnclick(event:MouseEvent):void{
cipher(outputText, outputText, alphabet, substitution);
}
This should be able to handle
Text of any length
Line breaks
Upper- and Lowercase letters
Undefined characters
You should search for Substitution ciphers, e.g., one of the simplest ones is the Caesar cipher.

Nested script in action script 3

Okay someone could really help me with this I've been working REALLY hard to get this working up until this point. One last thing is I need is the nextquestion1 function to bring me to the last from of my application.
I click on my button nextQuestion_btn to take me to one of the labled frames I have. I click on it and nothing happens. Also there is a lot of missing code in between the two Event Listener's , just so you know.
So...How can I make this code so I can trigger the next frame with the button?
stage.addEventListener(Event.CHANGE, checkTotal1000)
function checkTotal1000(e:Event){
var tech:Number = parseInt(tech_txt.text);
var med:Number = parseInt(med_txt.text);
var space:Number = parseInt(space_txt.text);
var genetic:Number = parseInt(ge_txt.text);
var worldComp:Number = parseInt(worldComp_txt.text);
var mars:Number = parseInt(mars_txt.text);
var worldColab:Number = parseInt(worldColab_txt.text);
var nothing:Number = parseInt(nothing_txt.text);
var zombieApocFinal:Number = zombieApoc + genetic;
var robotApocFinal:Number = robotApoc+ tech;
var plagueFinal:Number = plague + med;
var asteroidFinal:Number = asteroid + mars;
var iceAgeFinal:Number = iceAge + nothing;
var aliensFinal:Number = aliens + space;
var nukeWarFinal:Number = nukeWar + worldComp;
//var happyEverAfterFinal:Number = ;
trace(aliensFinal);
var total1000:Number = tech + med + space +
worldComp + mars + genetic + worldColab + nothing;
nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion1)
function nextQuestion1(e:MouseEvent){
if(tech && med && space && genetic && worldComp
&& mars && worldColab && nothing == 125){
gotoAndStop(7);
}
if(robotApocFinal > zombieApocFinal && plagueFinal &&
asteroidFinal && iceAgeFinal && aliensFinal &&
nukeWarFinal){
gotoAndStop("Robot");
}
else if(zombieApocFinal > robotApocFinal && plagueFinal &&
asteroidFinal && iceAgeFinal && aliensFinal &&
nukeWarFinal){
gotoAndStop("Zombie");
}
else if( plagueFinal > zombieApocFinal && robotApocFinal &&
asteroidFinal && iceAgeFinal && aliensFinal &&
nukeWarFinal){
gotoAndStop("Plague");
}
else if( asteroidFinal > zombieApocFinal && robotApocFinal &&
plagueFinal && iceAgeFinal && aliensFinal &&
nukeWarFinal){
gotoAndStop("Asteroid");
}
else if( iceAgeFinal > zombieApocFinal && robotApocFinal &&
plagueFinal && asteroidFinal && aliensFinal &&
nukeWarFinal){
gotoAndStop("IceAge");
}
else if( aliensFinal > zombieApocFinal && robotApocFinal &&
plagueFinal && asteroidFinal && iceAgeFinal &&
nukeWarFinal){
gotoAndStop("Aliens");
}
else if( nukeWarFinal > zombieApocFinal && robotApocFinal &&
plagueFinal && asteroidFinal && iceAgeFinal &&
aliensFinal){
gotoAndStop("Nuke");
}
}
}
Apparently you are using binary flags, but you use && operator which only works with booleans. This way if either of your ints is 0, no condition is satisfied. You apparently want to trigger the gotoAndStop() if one of the ints is greater than others. For this you can use the following code:
var bestFinal:int=Math.max(asteroidFinal, zombieApocFinal, robotApocFinal,
plagueFinal, iceAgeFinal, aliensFinal, nukeWarFinal);
if (bestFinal==asteroidFinal) {...}
else if (bestFinal==zombieApocFinal) {...}
...
If you are about to test if one var is greater than all the others, you cannot use what you've written, instead you use either Math.max() and check for equality, or you write many comparations and unite them via &&, like this:
if ((asteroidFinal > zombieApocFinal) && (asteroidFinal > robotApocFinal) &&
(asteroidFinal > plagueFinal) && (asteroidFinal > iceAgeFinal) &&
(asteroidFinal > aliensFinal) && (asteroidFinal > nukeWarFinal))

Categories