When does 1 != 1???
I was trying to compare two values, if they were the same, then their associated movie clips had been correctly linked to each other.
Trouble was, according to flash, one movie clip's 1 was not the same as the other movie clip's 1, even after I had used Number() to convert them both to numbers.
According to Flash, Number(1) = NaN!!!
Explanation:
One of the movie clips had been given a number from a for loop, the other had been given a number from an array. (Still doesn't really make sense to me, I'm sure I've done this before and never had this problem, but anyway...)
Solution:
Convert the value to a string, then convert that to a number.
e.g. Number(String(mc.val))
Weird...
A diary about programming problems - to help me more easily learn from (and Google) past mistakes.
Monday, 7 November 2011
Monday, 3 October 2011
How to Shuffle an Array in One Line
I often have to get a randomised version of an array, and I used to do it like this:
Create an empty array for the shuffled version
(Within a for loop) {
Get a random index using Math.floor(Math.random() * array.length)
Put the value from the unordered array into the ordered array, using the random index
Remove the value from the unordered array, again using the random index.
}
But now:
(Within a for loop) {
array.push(array.splice(Math.floor(Math.random() * array.length),1));
}
One line, no need for an empty array to be created.
Create an empty array for the shuffled version
(Within a for loop) {
Get a random index using Math.floor(Math.random() * array.length)
Put the value from the unordered array into the ordered array, using the random index
Remove the value from the unordered array, again using the random index.
}
But now:
(Within a for loop) {
array.push(array.splice(Math.floor(Math.random() * array.length),1));
}
One line, no need for an empty array to be created.
Thursday, 29 September 2011
No text? Are you sure?
I just spent about half an our trying to get Flash to recognize a blank text field as 0.
First I wrote a function to strip out the blanks (maybe Flash already has this, but anyway...), but when I tried an if statement to check if the empty input box.text was == to empty (''), I kept getting false.
It turned out that the html option was on, so even an empty text input box has a sort of text associated with it, the html tags.
How irritating - but now I have switched off the html option, so it is just a plain old text box, and an empty space is just empty space.
First I wrote a function to strip out the blanks (maybe Flash already has this, but anyway...), but when I tried an if statement to check if the empty input box.text was == to empty (''), I kept getting false.
It turned out that the html option was on, so even an empty text input box has a sort of text associated with it, the html tags.
How irritating - but now I have switched off the html option, so it is just a plain old text box, and an empty space is just empty space.
Subscribe to:
Posts (Atom)