Odd 1084: Syntax error: expecting rightbrace before end of program - actionscript-3

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);
}

Related

Encountering an error while attempting to create a game

I'm trying to create a crossword puzzle game using Actionscript 3.0 in Adobe
Animate. For the game coding, I followed an online tutorial and now I'm encountering an error. The error occurs whenever I'm in the actual game scene, no errors are encountered on the home page or instruction page. can anyone help me? my coding skills are below basic.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at CROSSWORD_CODE_fla::MainTimeline/entFrame().
stop();
quitbtn.addEventListener(MouseEvent.CLICK, quitt);
function quitt(evt: MouseEvent): void {
gotoAndStop("start");
}
var hippopotamus: Number = 0;
var horse: Number = 0;
var eel: Number = 0;
var frog: Number = 0;
var ant: Number = 0;
var turtle: Number = 0;
var tiger: Number = 0;
var bird: Number = 0;
var penguin: Number = 0;
var orca: Number = 0;
var octopus: Number = 0;
var owl: Number = 0;
var snail: Number = 0;
var duck: Number = 0;
var kangaroo: Number = 0;
var iguana: Number = 0;
var ALLwords:Number = 0;
var Total:Number = 0;
{
stage.addEventListener(Event.ENTER_FRAME, entFrame);
function entFrame(evt:Event):void {
if ((hypoH.text == ("h").toString())
&& (hypo1.text == ("i").toString())
&& (hypoP.text == ("p").toString())
&& (hypoP2.text == ("p").toString())
&& (hypoO.text == ("o").toString())
&& (hypoP3.text == ("p").toString())
&& (hypoO2.text == ("o").toString())
&& (hypoT.text == ("t").toString())
&& (hypoA.text == ("a").toString())
&& (hypoM.text == ("m").toString())
&& (hypoU.text == ("u").toString())
&& (hypoS.text == ("s").toString()))
{
hippopotamus = 1;
trace("hippopotamus");
}
}
{
if ((hrsO.text == ("o").toString())
&& (hrsR.text == ("r").toString())
&& (hrsS.text == ("s").toString())
&& (hrsE.text == ("e").toString()))
{
horse = 1;
trace("horse");
}
}
{
if ((eelE.text == ("e").toString())
&& (eel1.text == ("l").toString()))
{
eel = 1;
trace("eel");
}
}
{
if ((penP.text == ("p").toString())
&& (penE.text == ("e").toString())
&& (penN.text == ("n").toString())
&& (penG.text == ("g").toString())
&& (penU.text == ("u").toString())
&& (pen1.text == ("i").toString())
&& (penN2.text == ("n").toString()))
{
penguin = 1;
trace("penguin");
}
}
{
if ((brdB.text == ("b").toString())
&& (brdR.text == ("r").toString())
&& (brdD.text == ("d").toString()))
{
bird = 1;
trace("bird");
}
}
{
if ((octO.text == ("o").toString())
&& (octC.text == ("c").toString())
&& (octT.text == ("t").toString())
&& (octO2.text == ("0").toString())
&& (octP.text == ("p").toString())
&& (octU.text == ("u").toString()))
{
octopus = 1;
trace("octopus");
}
}
{
if ((orcR.text == ("r").toString())
&& (orcC.text == ("c").toString())
&& (orcA.text == ("a").toString()))
{
orca = 1;
trace("orca");
}
}
{
if ((owlO.text == ("o").toString())
&& (owlW.text == ("w").toString()))
{
owl = 1;
trace("owl");
}
}
{
if ((nailS.text == ("s").toString())
&& (nailA.text == ("a").toString())
&& (nail1.text == ("i").toString())
&& (nailL.text == ("l").toString()))
{
snail = 1;
trace("snail");
}
}
{
if ((dkD.text == ("d").toString())
&& (dkU.text == ("u").toString())
&& (dkC.text == ("c").toString())
&& (dkK.text == ("k").toString()))
{
duck = 1;
trace("duck");
}
}
{
if ((groA.text == ("a").toString())
&& (groN.text == ("n").toString())
&& (groG.text == ("g").toString())
&& (groA2.text == ("a").toString())
&& (groR.text == ("r").toString())
&& (groO.text == ("o").toString()))
{
kangaroo = 1;
trace("kangaroo");
}
}
{
if ((frgF.text == ("f").toString())
&& (frgR.text == ("r").toString())
&& (frgG.text == ("g").toString()))
{
frog = 1;
trace("frog");
}
}
{
if ((ignG.text == ("g").toString())
&& (ignU.text == ("u").toString())
&& (ignA.text == ("a").toString())
&& (ignN.text == ("n").toString()))
{
iguana = 1;
trace("iguana");
}
}
{
if ((antA.text == ("a").toString())
&& (antN.text == ("n").toString()))
{
ant = 1;
trace("ant");
}
}
{
if ((trtT.text == ("t").toString())
&& (trtU.text == ("u").toString())
&& (trtR.text == ("r").toString())
&& (trtT2.text == ("t").toString())
&& (trtL.text == ("l").toString()))
{
turtle = 1;
trace("turtle");
}
}
{
if ((tgr1.text == ("i").toString())
&& (tgrG.text == ("g").toString())
&& (tgrE.text == ("e").toString()))
{
tiger = 1;
trace("tiger");
}
}
ALLwords = hippopotamus + horse + eel + frog + tiger + octopus + penguin + bird + orca +
turtle + duck + snail + ant + kangaroo + iguana + owl;
Total = 16 - ALLwords;
if (Total == 0)
{
trace("Gameover");
}
incorrectTXT.text = ("you have" + Total+ " incorrect words remaining").toString();
}

How can I make the generated numbers never to be the same?

I'm a beginner in programming and I have been trying to make my 3 random generated numbers and the answer never to be the same, but no matter how I tried I didn't get the result that I wanted. I would be very thankful if someone would put me on the right path :)
This is a piece of code for the first randomly generated number, the other two are exactly the same.
//Random number 1
{
btnAns1.label = "" + random1;
if(mathOperationL1 == 1)
{
random1 = Math.floor(Math.random()* 24) + 1;
do
{
random1 = Math.floor(Math.random()* 24) + 1;
}
while((random1 > 24) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
btnAns1.label = "" + random1;
}
else if (mathOperationL1 == 2)
{
random1 = Math.floor(Math.random()* 11) + 1;
do
{
random1 = Math.floor(Math.random()* 11) + 1;
}
while((random1 > 11) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
btnAns1.label = "" + random1;
}
else if (mathOperationL1 == 3)
{
random1 = Math.floor(Math.random()* 144) + 1;
do
{
random1 = Math.floor(Math.random()* 144) + 1;
}
while((random1 > 144) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
btnAns1.label = "" + random1;
}
else if (mathOperationL1 == 4)
{
random1 = Math.floor(Math.random()* 12) + 1;
do
{
random1 = Math.floor(Math.random()* 12) + 1;
}
while((random1 > 12) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
btnAns1.label = "" + random1;
}
}
There are no errors in the code and everything else is working perfectly.It is just the line of code that is supposed to make the numbers never to be the same just doesn't work and after running the code for few times I eventually get numbers that are the same. while((random1 > 24) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
Thanks for your help in advance! :)
Your condition while((random1 > 24)... is always false because you generate numbers like 1 to 24 and they NEVER exceed 24.
Lets do it algorithmically.
var aList:Array = new Array;
// Put the user's input here. Don't forget that
// TextField.text contains String value rather than int.
aList[0] = 5;
// Each line adds a random element different from
// the elements that are already in the Array.
aList[1] = smartRandom(1, 10, aList);
aList[2] = smartRandom(4, 15, aList);
aList[3] = smartRandom(9, 20, aList);
// Let's see what we get this time.
trace(aList);
// Generates a random int from "min" to "max" inclusive,
// while avoiding all the numbers from the "avoid" Array.
// Note that it doesn't actually checks if it is possible
// to do so, thus smartRandom(1, 1, [1]) will result in the
// infinite loop because conditions will never be satisfied.
function smartRandom(min:int, max:int, avoid:Array):int
{
var result:int;
do
{
result = min + Math.random() * (max - min + 1);
}
// The Array.indexOf(...) method returns -1 if the argument
// is not on the given Array or it returns a 0-based
// index of the element that is equal to the given argument.
while (avoid.indexOf(result) > -1);
return result;
}
while((random1 > 24) || (random1 === answerL1) || (random1 === random2) || (random1 === random3));
The && operator insists that all the tests are true if it is to run again. The || is the logical OR, so any combination tested will cause the loop to run again.
Edit: I should also say, there seems to be alot of duplicated effort in this code.. perhaps once you are comfortable with your operator's behaviour you will be able to simplify the flow.

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;
}
}

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.

Can't get functions to work properly

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