Monday, 7 November 2011

One of these things is not like the others...

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...

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.

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.

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.

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...

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.

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?!