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.
A diary about programming problems - to help me more easily learn from (and Google) past mistakes.
Monday, 3 October 2011
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.
Monday, 22 November 2010
How to Draw a Perfect Circle with Bezier Curves
I actually needed this for Swift 3d rather than Flash. Obviously in Flash I could have just drawn a circle using the circle tool.
In Swift 3d, I could still have done that - but what I needed was a path that was circular. I wanted to film all the way round a 3d object I had made, so I needed the target camera to go in a circular path around the object.
I tried 'eyeballing' it, but when I rendered out the movie, it looked like I was zooming in and out all the time.
This page http://bezier.dodofactory.net/bezier1e_04.html had diagrams that made it easier to understand, but I now realise that the number 0.55 is just a rounded down version of a value called 'kappa' (kappa = 0.5522847498), found on http://www.whizkidtech.redprince.net/bezier/circle/.
Anyway, lets say you were drawing a circle with a radius of 6 arbitrary units, your bezier handles need to extend to (6x0.55) arbitrary units.
That should give you a pretty nice circle.
In Swift 3d, I could still have done that - but what I needed was a path that was circular. I wanted to film all the way round a 3d object I had made, so I needed the target camera to go in a circular path around the object.
I tried 'eyeballing' it, but when I rendered out the movie, it looked like I was zooming in and out all the time.
This page http://bezier.dodofactory.net/bezier1e_04.html had diagrams that made it easier to understand, but I now realise that the number 0.55 is just a rounded down version of a value called 'kappa' (kappa = 0.5522847498), found on http://www.whizkidtech.redprince.net/bezier/circle/.
Anyway, lets say you were drawing a circle with a radius of 6 arbitrary units, your bezier handles need to extend to (6x0.55) arbitrary units.
That should give you a pretty nice circle.
Sunday, 26 September 2010
Embedding Fonts
Hey, I just found out that if you embed text in a text field, it loses that horrible grainy look that text has when it is loaded from another source!
Cool, I hate that grainy look.
UPDATE:
And now I just found out that embedded fonts allow dynamic text to show through a mask - just saved me about an hour's work, that may be an overestimate...
Cool, I hate that grainy look.
UPDATE:
And now I just found out that embedded fonts allow dynamic text to show through a mask - just saved me about an hour's work, that may be an overestimate...
Saturday, 25 September 2010
Line Breaks - Apple vs Windows
Well, it's that time again...
What time?
Time to be incomprehensibly inconvenienced by stupid differences between one version of something and another version.
Why oh why is there a difference between text files in MS Windows, and Apple? It's a text file for goodness' sake - that's about as simple as it gets isn't it?
Apparently not.
First of all the text file wouldn't even load into Flash, because it had all this invisible gobbledygook at the beginning - visible in Notepad or Dreamweaver, but not MS Word...
And even when that is all deleted (I copied the text from MS Word and pasted it into Dreamweaver) there are still invisible line breaks!
So I'm going to do a search and see if I can find an alternate way of saving this so I don't have to go through and manually add unicode line breaks.
UPDATE:
OK - First I tried looking at the invisible characters and using 'Shift+Enter' and also, changing the format to 'Normal-No spaces'.
Neither worked.
And now I've tried saving it as Unicode and other txt formats, but nothing worked, the invisible characters are still there. So I will have to go through and change all the line breaks to %0d.
Oh well.
What time?
Time to be incomprehensibly inconvenienced by stupid differences between one version of something and another version.
Why oh why is there a difference between text files in MS Windows, and Apple? It's a text file for goodness' sake - that's about as simple as it gets isn't it?
Apparently not.
First of all the text file wouldn't even load into Flash, because it had all this invisible gobbledygook at the beginning - visible in Notepad or Dreamweaver, but not MS Word...
And even when that is all deleted (I copied the text from MS Word and pasted it into Dreamweaver) there are still invisible line breaks!
So I'm going to do a search and see if I can find an alternate way of saving this so I don't have to go through and manually add unicode line breaks.
UPDATE:
OK - First I tried looking at the invisible characters and using 'Shift+Enter' and also, changing the format to 'Normal-No spaces'.
Neither worked.
And now I've tried saving it as Unicode and other txt formats, but nothing worked, the invisible characters are still there. So I will have to go through and change all the line breaks to %0d.
Oh well.
Wednesday, 22 September 2010
SetInterval
I hardly ever use this, but in all honesty, it is very useful.
The code below is what I have used for a button which starts or stops the setInterval method. This is AS2 code.
var methInterval = null;
btn.onRelease = function () {
if(methInterval == null) {
methInterval = setInterval(meth,200);
} else {
clearInterval(methInterval);
methInterval = null;
}
}
There may be a more graceful way to do this, but this worked for me. If you trace the interval after clearing it, you get numbers anyway, and I think the number goes up by one each time you set it, so why not just set it to null when you are not using it?!
The code below is what I have used for a button which starts or stops the setInterval method. This is AS2 code.
var methInterval = null;
btn.onRelease = function () {
if(methInterval == null) {
methInterval = setInterval(meth,200);
} else {
clearInterval(methInterval);
methInterval = null;
}
}
There may be a more graceful way to do this, but this worked for me. If you trace the interval after clearing it, you get numbers anyway, and I think the number goes up by one each time you set it, so why not just set it to null when you are not using it?!
Thursday, 12 August 2010
DIV again
OK, next problem.
I realised that I very stupidly, and I won't say too much about the details, put, shall we say...sensitive info...right there in the viewable source of the html file. Pretty much rendering the whole thing completely useless.
Rather than make large and potentially catastrophic changes to the flash file, I want to tell the html file to hide the info when it has finished passing it to the swf, using DIV tags.
I have recorded on this blog how to change attributes, but forgot to mention how to change the stuff between the div tags! Stupid me. I know I worked it out before, but now I have forgotten and will have to do the whole thing all over again.
But this time, I will, insha Allah, record it for posterity.
That should work.
UPDATE:
OK, that code above does work - but it won't do what I want it to. You see, the page changes according to what the DIV tags say, but when you view the source, you still get to see the original.
So I will have to make some serious changes to the Flash file.
I realised that I very stupidly, and I won't say too much about the details, put, shall we say...sensitive info...right there in the viewable source of the html file. Pretty much rendering the whole thing completely useless.
Rather than make large and potentially catastrophic changes to the flash file, I want to tell the html file to hide the info when it has finished passing it to the swf, using DIV tags.
I have recorded on this blog how to change attributes, but forgot to mention how to change the stuff between the div tags! Stupid me. I know I worked it out before, but now I have forgotten and will have to do the whole thing all over again.
But this time, I will, insha Allah, record it for posterity.
<script type="text/javascript">
function alterTags() {
var tTag = document.getElementById("tElement");
tTag.innerHTML = "tag content";
}
</script>
<div id="tElement"></div>
That should work.
UPDATE:
OK, that code above does work - but it won't do what I want it to. You see, the page changes according to what the DIV tags say, but when you view the source, you still get to see the original.
So I will have to make some serious changes to the Flash file.
Subscribe to:
Posts (Atom)