view source

Things that equal false

November 9th, 2007 . by polyGeek

So I’m writing this Flex widget/app/thingy that will let people vote for things on my site. I need to test that the user has selected one of the radio buttons when they hit the submit vote button. The code for that is:

Pretty simple stuff. In the if-statement I’m essentially saying, “if votingChoices.selectedValue DOESN’T exist then do this stuff, else do this other stuff.

Like I said, simple. Right?

But there’s a catch. I wasn’t paying attention and gave the radioButtons data values of, wait for it, numbers starting with zero. And you know what that means: no one will be able to submit the first choice in the list.

Okay, so simple fix. I shouldn’t use numbers, especially starting with zero, for the radioButton values.

But better yet I could just change the if-statement to:

if( votingChoices.selectedValue == null ) { ...

That would be the smart thing to do. And I tell myself that all the time. Don’t test for the existence of something in an if statement by just placing a reference. Go ahead and write out undefined or null. Because that’s what you’re really testing.

I can’t count the number of times I’ve screwed that up. So now every time I do that I’ll force myself to come back to this post and comment that no I can’t learn from my mistakes. :-)

So here are a few other things that equate to false. From Moock’s Essential ActionScript 3.0.

Original data Result after conversion
undefined false
null false
NaN false
0 false
Infinity true
-Infinity true
Other numeric value true
Nonempty string true
Empty string (”") false
Object true

Here is a table of the default values for the various types.

Datatype Default value
String null
Boolean false
int 0
uint 0
Number NaN
All other types null

Links: Mathematical themes with Actionscript

August 26th, 2007 . by polyGeek

One of my hobbies is playing around with mathematical algorithms in Actionscript. Here are a few sites I have found useful.

Code Optimization

AS3 Speed tests
This is a community maintained page of speed optimization techniques for use with ActionScript3.

AS3: Rethink your old assumptions
A bit on code optimization using constants and variables.

Fast and accurate sine/cosine approximation
More code optimization to do sine/cosine operations without using the Math class.

Bitwise gems - fast integer math
Using bitwise operations to speed up calculations.

Actionscript

Using molecular mechanics in Flash
Cool motions based on equations of attraction and repulsion.

Making Bitmap Art with Flash CS3 and WikiPedia
Art from equations.

Sites

Wolfram’s MathWorld
The web’s most extensive math resource.

Superfractals
The site resource for the book Superfractals by Professor Michael F. Barnsley

Good Math / Bad Math
Finding the fun in good math. Squashing bad math and the fools who promote it is their tagline. I just hope he never writes about me.

Articles

Iterated Function Systems and Attractors
Covers the mathematical iteration I used here.


right-click HERE!

August 20th, 2007 . by polyGeek

Update: Paulius Uza has run an extensive user test on the right-click code and concludes that the code is reliable 86% of the time across the major browsers.

Today will be a day long remembered. It has seen the end of the Javascript incompatibilities, it will soon see the end of the Flash context-menu.

We hope.

Paulius Uza has created a right-click testing script and he needs your help. Please visit the test page, right-click on the SWF and then vote yes or no to let him know if the code is working correctly. The script will automatically send browser type, version and OS back to Paulius for analysis.

You can visit the project home on Google Code at: http://code.google.com/p/custom-context-menu/


Google Code project for custom context-menu in Flash/Flex

August 18th, 2007 . by polyGeek

I have created a project on Google Code for the custom context-menu.

http://code.google.com/p/custom-context-menu/

I have copied all the comments from my original posting over to the issues tab, uploaded the code, made a few wiki entries and such. This is the first time I’ve ever used Google Code so please feel free to make any improvements to what I’ve started.

There isn’t much that I can do beyond this point other than to ask everyone interested to blog about this so that hopefully some Javascript experts can jump on this and get this working reliably.

I’m going to look into creating an interactive chart that we might be able to use to track testing. Hopefully I’ll be adding a custom right-click to the charting app real soon. :-)


360Flex Seattle brain dump

August 17th, 2007 . by polyGeek

As always the best part of a conference is what happens outside the sessions. Not to disparage the speakers at all. There were some great sessions, more about that later, and some good sessions. And think this might be the first conference I’ve been to where none of the sessions I attended were bad.

John Wilker and Tom Ortega did a fabulous job. I guess the best compliment I can pay them is that I plan to be going to the next 360Flex conference that’s on the West coast.

There were lots of great sessions. If I had to pick my favorite I’d go with Doug McCune’s Building Custom Components. His message was that we need to keep an eye on existing open source projects that we can use to wrap into our projects when ever possible. His tileUI is mosly open source code that he mashed together.

I thought it was brilliant that his session itself was sort of open source. He got things started and then mediated a discussion from various members of the audience. Deepa Subramaniam from Adobe, Adam Flater from effectiveUI, David Coletta from Buzzword, and a few others that I didn’t know made significant contributions to the discussion. And on many occasions Doug himself asked questions of the audience like, “this is what I do. What do you do?”

I think a lot of presenters would be afraid of letting go of control over the session but Doug handled it well. Others should consider a similar approach.

Bugs

I think Adobe should hand out buttons or something that say, “Submit a bug.” I heard that phrase countless times. So, if you find something you think is a bug in Flex then go to bugs.adobe.com/flex/ and do a search to see if anyone else has submitted a bug on what you’ve found. If they have then vote for it. The more votes a bug gets the more likely it is to get fixed. If there isn’t already a bug report then submit one.

The most common bug you’ll find are properties/methods that are private should should be public. So if you find something in the framework that you want public submit a bug. Deepa also offered that these are the easiest bugs to fix and they do have a quota. So don’t be shy.

I personally don’t know what the problem is. I just did a search and replace across all the entire flex framework on my system and make everything public. Problem solved. Okay, I’m kidding. But that would solve the problem. :-)

Tid-bits

The good news: Flex 3 will ship as open source.

The bad news: Flex 3 won’t ship until early ‘08. I think we were all hoping for a ship date around MAX07. Bummer.

AIR 1.0 will ship with Flex 3.

Flex 3 will have much better text support with kernaling, font height, etc.

Blogs to read

Here are a few blogs to read that you may not have seen before.

Alex’s Flex Closet ( http://blogs.adobe.com/aharui/ )

Flexophile: fount of Flex factoids ( http://blogs.adobe.com/gosmith/ )

Deepa said that she was going to start blogging before MAX07. If she doesn’t I’m going to file a bug. :-)

Coldfusion 8 hosting

I talked to Ted Patrick about who was offering shared hosting of a Coldfusion 8 with LiveCycle setup. He said that they were working hard to get that option out in hosting community for us. I’m sure that as soon as someone has that available then someone at Adobe will make it known to the community.

Video

The video encoder that comes with Flash is good. But if you want the best results look for On2Flix or Sorenson’s encoders. They do two pass encoding which the Adobe encoder doesn’t do.

Nity-gritty

If you use setStyle in your Flex apps do it on initialization not creationComplete. That will help speed up your apps rendering time.

Debugging

It never occurred to me that those ugly exceptions warnings that pop up in Flex apps only work in the debug version of the player that we Flex developers have. That’s a relief.

In Flex 3 you will get the option in the code editor to right-click and select watch variable/property during your debugging sessions.

You can make Eclipse preview in a browser other than your default by going to: Window -> General -> Web Browser.

Nested functions are not a member of any class. So there is really no telling what this is.

Custom Components

Metadata is for code hinting purposes. They don’t actually serve any purpose in code execution.

If you have a custom event you use clone to keep those custom properties attached as the event bubbles. Otherwise the event bubbles but loses your custom property.

Have your heard of the tag [Mixin]. You should check it out at AdamFlater’s blog.

List of speakers

This is a good page to keep handy if you want to go back and find a presenter’s website/blog someday.

http://www.360conferences.com/360flex/2007/05/speaker-to-session-list.html


Custom Right-click in Flash

August 16th, 2007 . by polyGeek
Update: I’ve created a project on Google Code for this in the hopes of getting some Javascript experts to make the code work reliably in all the browsers that support the Actionscript External Interface.

By using a few lines of Javascript, and the External Interface I’ve been able to replace the default context-menu in Flash with a customized one. In my examples I use a simple color wheel in Flash and the color-picker component in Flex.

You can check out the pages here: Flash version, Flex version, Download code.

Here is the testing matrix so far:

  • Internet Explorer 7, Windows XP, passed
  • FireFox 2, Windows XP, passed

If you have a configuration that is different than any shown above then please comment and let me know the results. I’ll update the list as comments come in.

If it doesn’t work on your system it would be great if you took a crack at modifying the Javascript to support your browser. I’m pretty much out of the loop from here on out because I don’t have anything left to test on.

Below is the list of browser/OSes that the Flash External Interface works on. I would like to make sure that the Javascript works on all of the browser/OSes that are supporeted by the External Interface - listed below.

External Interface browser support
Browser Operating System
Internet Explorer 5.0 and later Windows  
Netscape 8.0 and later Windows Macintosh
Mozilla 1.7.5 and later Windows Macintosh
Firefox 1.0 and later Windows Macintosh
Safari 1.3 and later   Macintosh

The gist of how this works

It’s really pretty simple. We’ve all seen sites that have Flash content that have disabled the right-click altogether. I just took that idea and modified it by capturing the onMouseDown in Javascript and if it’s a right-click then notifying Flash via the External Interface. Flash then does its work which is very basic Actionscript.

Hopefully this will work on most browser/OSes and so encourage Adobe to just give us access from within Flash to fully customize the context-menu and not have to muck about in Javascript.

Sorry about disparaging Javascript. It isn’t Javascript’s fault that Internet Explorer is such a pain in the frakking ass. It took me 15 minutes to get this all to work in FireFox. It took 4 hours to get it to work in IE. (Insert Yosemite Sam cursing tirade here.)


Random generation of the Sierpinski triangle

August 14th, 2007 . by polyGeek

On the first page of the book Superfractals is one of those cool mathematical curiosities that makes math geeks like me go all Archimedian. (No, I didn’t run naked to the bookstore counter and buy the book. But I did go home and order it at Amazon.)

Here’s the gist of the curiosity, which I created an algorithm for below:

Pick 3 points on the xy-plane and label them A, B, and C. (As a group I’ll call them ABC.)

Now pick a fourth point and label it X0. Now randomly pick a point from the ABC group. From X0 make a new point that is the mid-point between your random ABC selection and X0. Label that point X1.

Now from X1 do the same thing again. Pick randomly between ABC group and then from X1 go to the mid-point between X1 and your random selection.

Repeat that a few millions times.

You might think that eventually you’ll fill up the points in between the ABC group and have a solid triangle. But no. You’ll end up with a skewed Sierpinski triangle as you can see below.

Serendipitous errors

In my first version of the algorithm I made a mistake in the equation that calculates the mid-point. The equation should be as such:

xmid = ( x1 + x2 ) / 2

And the same for the y-coordinate.

In my haste I wrote the incorrectly and calculated a difference instead of a sum between the two values.

Now that sould produce something random because what on earth could the relationship of the difference between the first and second points divided by two have anything to do with?

But you don’t get a random dispersion. You get something that closely resembles the inverse of the Kosh snowflake. Why? Ask a mathematician because I have no idea.

Grdenizing the code

This is exactly the sort of algorithm where code optimization can make a big difference. John Grden just wrote a wonderful post on optimizing Actionscript by using bitwise operations and a few other tricks that I applied here.

In the application above you can check/uncheck the optimized code checkbox to select which version of the method gets used. Then you can see the results displayed and judge for yourself how much code optimization can benefit you in cases where you are iterating over large sets of data.

View source code here.

The obvious place to optimize is with the mid-point equation. Instead of dividing by two I’m using the equivalent bitwise operation: >> 1.

That helped but using int(num) instead of Math.floor(num) made a huge difference. I’ll never use Math.floor() again that’s for sure.


Taking my cars for a spin with Actionscript 3.0

August 12th, 2007 . by polyGeek

These cars were design in the Forza2 racing game for the XBox360. First off let me give credit where credit is due. I did not do the designs on the polyGeek car or the Seahawks car. That was done by Lee Nicholls the Global Solutions Director for Getronics, World domination hobbyist, woo-er of beautiful women, driver of outrageously fast cars, and Forza2 designer extraordinar.

You can view the source code here. ( The download zip is a little over a meg because I left in the images of the Adobe Fararri for you to play with.) I’ve commented the code pretty thoroughly so I don’t think there’s much point in going into details. I’ll just give a quick overview of how this works.

First off, there are XML files that describe a series of images. The code goes through and loads each image and places them one on top of the other.

As soon as all the images are loaded it runs through the series of images to display them each in turn giving the illusion of the car, or whatever you have photos of, rotating. That’s serves the purpose of letting the user know that they can rotate through the images. Just displaying a mouse-hand probably isn’t enough of an indication.

The mouse wheel also works to scroll through the images.

The images are loaded one after the other instead of all the requests going out at once. I found that there were some odd errors with making a request for 40 images all in one shot. Sometimes they would load and sometimes they wouldn’t. Loading in sequence solved all that.

As your drag your mouse across the image space it uses the width of the image and the number of images in the stack to calculate how much you need to drag your mouse before the next image displays. That way if you start from one side of the image space and drag all the way across it will rotate through all of the images. No matter how many there are.

Modulo to the resuce

There are few occasions to use modulo in Actionscript. That’s the mathematical operation using the “%” sign. Modulo gives you just the remainder after division. Example: 34 % 10 = 4 because 10 goes into 34 three times with 4 left over.

Modulo was very handy for solving an otherwise messy problem. References to each image are stored in an array. As you drag your mouse across the image space the code figures out which image needs to be visible at that point.

So what happens if the user is dragging through the last few images in the array and then needs to start back at the beginning of the array? You can imagine lots of messy if() statements to handle all that or, I can just take the modulo of the image I think they need to see with the length of the array and they will always see the correct image.

Example: suppose there are 30 images in the array ( 0-29). The user is dragging across the image space and displaying the last few images, 27, 28, 29, 30 . . . But there is no image 30. But if when I go to display an image I use the calculated image number modulo 30 ( length of the array ) then I’ll always be in the range of the image array.

So when I try to display image 30 I’ll get 0, and for 31 I’ll get 1 and so forth. Problem solved and no messing if() statements.


    Next Entries »




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites