I wanted to make a little gas calculator in Flash with AS but i am getting the error "NaN" in my textfield even BEFORE i enter anything inside the textfield. Any ideas where the problem is? Many thanks in advance. Here is my actionscript code:
km_txt.restrict = ".0-9";
liter_txt.restrict = ".0-9";
priceliter_txt.restrict = ".0-9";
stage.addEventListener(Event.ENTER_FRAME, calculate);
function calculate(param1:Event)
{
if (liter_txt.text != "" && km_txt.text != "")
{
usage_txt.text = String(100 * Number(liter_txt.text) / Number(km_txt.text));
}
if (liter_txt.text != "" && km_txt.text != "" && priceliter_txt.text != "")
{
cost_txt.text = String(Number(liter_txt.text) / Number(km_txt.text) * Number(priceliter_txt.text));
}
if (liter_txt.text != "" && priceliter_txt.text != "")
{
total_txt.text = String(Number(liter_txt.text) * Number(priceliter_txt.text));
}
}
You are casting to Number a few times from TextField objects but those at that point don't contain anything so the cast resolve to NaN:
String(100 * Number(liter_txt.text) / Number(km_txt.text));
Now trying to add/multiply/divide Number and NaN together still resolve to NaN.
You need to check for value first and maybe set to 0 if you get NaN, store in variables to make things easier:
var value:Number = Number(liter_txt.text);
if(isNaN(value))
{
//this is not a number so substitute with 0?
value = 0;
}
Be sure about initial text values of your input TextFields since you are checking value of "". At the top of your block, write;
km_txt.text = "";
liter_txt.text = "";
priceliter_txt.text = "";
And it will be a better code if you listen to TextEvent.TEXT_INPUT events on TextFields and make calculations there.
Related
so I'm getting an error at line 342 which is
myTextField.text = object[curPart]["text"]
it says that a term is undefined. Tracing curPart gives me a null value, although in code that runs literally moments for that one, curPart is part3. Nothing changes it. This is what runs before it, a type writing effect function.
private function addCharackter():void
{
if (!textFinished)
{
// add the charackter to the Textfield
myTextField.text = object[curPart]["text"].substring(0,counter)
counter++
trace(curPart) // prints "part3"
// if you reached the end of the String stop Timer
if (counter == object[curPart]["text"].length+1 && !textFinished)
{
textFinished = true;
trace("Text finished:",textFinished)
clearInterval(sInt)
counter = 0;
if (object[curPart]["text"].indexOf("What do you do?") >= 0)
{
trace("H!!!")
choosing = true
createOptions();
}
if (object[curPart]["text"].indexOf("What do you do?") >= 0)
{
dead = true;
}
}
}
Another weird thing is that the error doesn't happen if I finish the type writing effect by pressing space which is done here:
public function onEnter(key:KeyboardEvent):void
{
if (key.charCode == 32)
{
if (dead && textFinished && !choosing)
{
curPart = object[curPart]["lastPart"]
textFinished = false
dead = false;
myTextField.text = ""
counter = 0;
sInt = setInterval(addCharackter, textSpeed)
}
if (!textFinished && !choosing && !dead)
{
trace(textFinished)
trace("finishing")
trace(curPart)
myTextField.text = object[curPart]["text"]
if (object[curPart]["text"].indexOf("What do you do?") >= 0)
{
trace("H!")
choosing = true
createOptions();
}
if (object[curPart]["text"].indexOf("You have died") >= 0)
{
dead = true;
}
clearInterval(sInt)
textFinished = true;
}
else if (textFinished && !choosing && !dead)
{
trace("Hi!!!!")
curPart = object[curPart]["nextPart"]
myTextField.text = ""
counter = 0;
sInt = setInterval(addCharackter, textSpeed)
textFinished = false
}
}
}
I've looked through my code for hours. text is inside part3 and part3 DOES exist, considering it works if I skip the typing process by pressing space. (it also doesn't happen if I were to skip the typing process before part3.)
edit: Another thing that I just realised, line 342 should never be ran because textFinished should equal true (which it is moments before, but when I press space, it magically isn't. I don't change it to false anywhere except in parts that aren't running when the error happens)
Never mind. Found why it wasn't working. Stupid user error.
Yeah so here's my code
if(defaultmeter.visible = true)
{
meter1.visible = true;
meter1.x = 124.10;
meter.y = 63.10;
jizz.visible = false;
}
Thing is the things inside the { }
work but the the if statement doesnt apply
Like when defaultmeter is not visible, the stuff inside the {} still applies :C help please
If you do mc.visible = true so you assign the true value to mc.visible so you make mc visible which normally always true.
To compare in this level, we use == (equal) operator to check if two values are equal or the != (not equal, different ) to check if two values are not equal.
So in your case you can do :
if(defaultmeter.visible == true){
// instructions here
}
Or
if(defaultmeter.visible != false){
// instructions here
}
Or simply
if(defaultmeter.visible){
// instructions here
}
Well of course, you need to cover the other case as well, like this:
if(defaultmeter.visible == true)
{
meter1.visible = true;
meter1.x = 124.10;
meter.y = 63.10;
jizz.visible = false;
}else{
meter1.visible = false;
jizz.visible = true; //btw, jizz, seriously?
...
}
use like this if (defaultmeter.visible == true){}
I have twenty eight instances of a two-frame MovieClip (frame1 = off - frame 2 = on) to select PDFs to send. The following code works fine, but I am looking to tighten it up and make it less verbose and easier to read. I include only one reference to an instance for space and sanity sake.
function PDFClick(e:MouseEvent):void {
targetPDF = e.target.ID;
trace("targetPDF " +targetPDF);
if (targetPDF == "PDF1")
if (pdf.pcconnectionPDF1.currentFrame == 1)
{
pdf.pcconnectionPDF1.gotoAndPlay(2);
PDF1 = 1;
trace("PDF1 is "+PDF1);
}else{
pdf.pcconnectionPDF1.gotoAndPlay(1);
PDF1 = 0;
trace("PDF1 is "+PDF1);
}
Thanks! trying to learn
You'll want to generalize your calls to your ID, that way you don't need special code for each condition.
function PDFClick(e:MouseEvent):void {
var ID:String = e.target.ID;
var mc = pdf["pcconnection" + ID];
if (mc.currentframe == 1) {
mc.gotoAndPlay(2);
this[ID] = 1;
} else {
mc.gotoAndPlay(1);
this[ID] = 0;
}
}
How about this:
function PDFClick(e:MouseEvent):void {
targetPDF = e.target.ID;
trace("targetPDF " +targetPDF);
if (targetPDF == "PDF1") {
var frame:int = pdf.pconnectionPDF1.currentFrame;
pdf.pconnectionPDF1.gotoAndPlay( frame == 1 ? (PDF1 = 1)+1 : (PDF1 = 0)+1 );
}
}
I think that's about what you are looking for.
I am actually trying to check whether a game's score is a high score. And then if it is, the score will be added to the leaderboard.
However I got this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at FlashGame_fla::MainTimeline/checkBananaHS()
In my game, in that particular frame, this is the code that would link to the checkBananaHS():
function rpslsWon():void
{
gameOverBananaMC.visible = true;
bananaWinLose.visible = true;
bananaWinLose.text = "Kampai " + cPlayerData.pName + "! You are totally bananas!! \nYour Score: " + pBananaScore;
toMenuBtn.visible = true;
rollAgainBtn.visible = true;
toMenuBtn.addEventListener(MouseEvent.CLICK, Click_backtoMain);
rollAgainBtn.addEventListener(MouseEvent.CLICK, backtoBanana);
saveItBtn.addEventListener(MouseEvent.CLICK, checkBananaHS);
cPlayerData.pBananaScore = pBananaScore;
saveData();
tracePlayerData();
}
And this is the piece of code in the high score's frame:
var rpslsHighScore:int;
var rpslsHSName:String;
rpslsHighScore = 0;
rpslsHSName = "";
//rpslsHighScore = 0;
bananaWinnerDisplay.text = " ";
bananaScoreDisplay.text = "0";
function checkBananaHS(event:MouseEvent):void
{
if ((cPlayerData.pBananaScore > rpslsHighScore ||
rpslsHighScore == 0) && cPlayerData.pBananaScore > 0)
{
trace("There's a new high score for Banana");
rpslsHighScore = cPlayerData.pBananaScore;
rpslsHSName = cPlayerData.pName;
bananaScoreDisplay.text = "" + rpslsHighScore;
bananaWinnerDisplay.text = rpslsHSName;
saveData();
}
}
I just can't manage to fix the error. Can anyone help me out? Thanks alot!
One of your variables within checkBananaHS() are not set when it is called. So when you try to access a property of that object, it errors out because nothing exists.
Wit that in mind, that means one of the following objects are not yet set in your function:
cPlayerData
bananaScoreDisplay
bananaWinnerDisplay
Run a trace on each of those, one at a time at the beginning of your function, and see which one doesn't return [Object object]
I got an input textfield in the UI.
When user key in "GIRAFFEEE", "GIRAFEAAA" OR "GIRAFFE123" and submit. The score value should be 0. However it returns 1.
How do I compare case sensitive string correctly?
qns1 = qns1_txt.text.toLowerCase();
qns1Ans = "giraffe"
//.toLowerCase();
if (qns1 == qns1Ans)
{
score = 1;
}
else
{
score = 0;
}
If you test following:
var correct:String = "giraffe";
var userAns:String = "giraffeaaaa";
trace(correct == userAns);//false - as expected
it means that string comparison works:)
I assume that your test code is in the CHANGE event of the textfield which may result in false positive as user may type part of correct answer, I think you should do a function:
function validate()
{
qns1 = qns1_txt.text.toLowerCase();
qns1Ans = "giraffe"
score = 0;
if(qns1 == qns1Ans)
{
score = 1;
}
}
and call it when user will hit submit, you could also compare the length of strings but equal operator will do just fine.