Harnassing the Power of Timers in AS3

Anyone who has had the experience of trying to time events for web via client side scripting before can readily appreciate the power and ease of using the robust Timer class in Actionscript 3. Similar to the idea of using JavaScript’s setTimout and/or setInterval methods, we can use this class to set a timer for a millisecond count that will simply trigger a TimerEvent when the timer has reached its end. The beauty of it, though, is that when the timer is created, we can explicitly declare how many times we want the timer to run! No more sloppy looping of intervals or timeouts: just set your timer to X number of loops and start it running.

So, obviously, if we create a timer to run once, it is the equivalent of running a setTimeout function. As with the aforementioned JavaScript functions, we set the timer with a number of milliseconds we want to wait before triggering its event. As an optional second argument, we pass in the number of cycles we wish the timer to run. Also, since the timer is an object, even if we set the timer to run only once, we can call the start method on it and manually run that single timer multiple times throughout our script. With a little creative thinking, I’m sure you can already come up with dozens of ways this could be useful.
Continue reading

Photography Section Finally Added

Well, I suppose one might say this is long overdue — after all, I am a web developer, right? No matter how much work I’m able to get done, I seldom have time to actually add to my personal sites, so when I found out just how easily zenPhoto could be added to my site, I jumped. In a matter of an hour (give or take), I had this new section up and running, primed with many of the photos I’ve had sitting around just waiting to be posted somewhere. Needless to say, I won’t have to blog as many of my pictures anymore, since I’ll be able to link directly to their galleries now. So, when you have a moment, follow the link at the top of this page, or just go here to see the new gallery. As time allows, I will be working on a theme for both the blog and photo gallery that is more uniform and unique — but who knows when that will actually be completed…

Enjoy!

The Dead Horse

So, I typically strive to avoid any political debates on here, but I got an email this morning that was just too good to pass up. I have often wondered how printing more money, further decreasing its inherent value, is supposed to stimulate the economy — well, let’s learn a lesson from Chuck:

Young Chuck in Montana bought a horse from a farmer for $100.
The farmer agreed to deliver the horse the next day.

The next day the farmer drove up and said, “Sorry son, but I have some
bad news… the horse died.”

Chuck replied, “Well, then just give me my money back.”

The farmer said, “Can’t do that. I went and spent it already.”

Chuck said, “Ok, then, just bring me the dead horse.”

The farmer asked, “What ya gonna do with him?”

Chuck said, “I’m going to raffle him off.”

The farmer said, “You can’t raffle off a dead horse!”

Chuck said, “Sure I can, Watch me. I just won’t tell anybody he’s
dead.”

A month later, the farmer met up with Chuck and asked, “What happened
with that dead horse?”

Chuck said, “I raffled him off.
I sold 500 tickets at two dollars a piece and made a profit of $998.”

The farmer said, “Didn’t anyone complain?”

Chuck said, “Just the guy who won. So I gave him his two dollars back.”

Chuck grew up and now works for the government. He’s the one who figured out how this “bail-out” is going to work.

That’s good stuff.

Optimizing Code Reusability and Minimizing Your Footprint

One of the most common questions I am asked is also one of the biggest problems I see in code as I read it: lack of optimization. I often have people asking questions about how they can consolidate the generic actions they want multiple objects to have access to perform without duplicating the code. In Actionscript, just like any other coding language, the duplication of code is typically a red flag that you are doing something wrong. No, not wrong in the sense of broken, but wrong in the sense of best practice and code optimization. It is much easier to have one handler function to debug and keep up than to have that same handler individually executing in a dozen different class objects.

Quite possibly the single most useful thing I have discovered in optimizing some of the graphical effects I use over and over again is simply making use of the event object in my callback functions. When you attach an event listener to an object and the event is triggered, the event passed into the callback function has a target attribute that is a reference to the object which initially triggered the event. Accessing the object this way lets us generalize handlers (such as a fadeOut function) that will consolidate our code and give us uniformity in execution.
Continue reading

What a Difference a Flash Makes

While flash photography to some may simply be that which causes red-eye, I have been trying to read and learn some of the nuances of lighting and flash usage in my pictures. My biggest difficulty has been the lack of a “real” flash, but I am happy to say that I was able to get one for my birthday over the weekend. The Nikkon SB-600 Speedlight is pretty much the base model in good flashes for my D90, and I’ve been waiting to find a good sale. Pricing doesn’t vary much on them, apparently, so I jumped on the chance to get one from the Circuit City liquidation pricing at almost half off retail.

As with so many other things in life, I had seen and understood a part of the difference a good flash can make, but until you actually use one, it’s hard to really grasp the significant difference in the shots. I thought I would share my first comparison with you. Keep in mind that one of the biggest differences (besides the obvious brightness difference) between using the on-board flash and the hot shoe one is the ability to be able to direct the light and reflect it off different surfaces to keep from the harsh direct lighting typical to home photography. The following two photos show this poignant difference illustrated.
Continue reading

Consistent Omnidirectional Movement in AS3

One of my biggest gripes in Flash games to date is that of simulated motion that is by no means realistic. True, this may be acceptable for some games, but as a whole, there is something that just doesn’t feel right when your side scrolling shooter plane moves up one unit when you press UP, left one unit when you press LEFT but moves both up and left one entire unit when you have both keys depressed. Logically, by moving in two directions at once, your character or vehicle should not be granted an additional bonus to their movement speed. Not only does it offer unfair advantages to the competent player who learns to use this loophole to his advantage, but it also makes your control somewhat unwieldy. The human brain is able to estimate precise movements and predict where things should go, and some people, though they may not know the precise reasons, will gravitate to those games that their minds can accurately predict the outcome of a motion.

If you have read my last few posts, you understand that this more realistic motion has driven me to all sorts of physics and algorithmic studies in the past weeks. Today, though, I decided to write on something a bit more fun. Those of you who remember the great old days of R. C. Pro-Am, Super Off Road or other top view racing games can attest to both the challenge and creativity about learning to control a car driving in an omnidirectional world without having to sit directly behind the wheel.
Continue reading

Pendulums, Gravity and Angular Acceleration

Continuing my personal studies on simulating true gravitational acceleration in Actionscript, a couple days ago I arrived at one of the more conceptually challenging tasks I have tackled to date: the pendulum. I say conceptually challenging, because I tried to create a realistic pendulum in Flash a year or so ago and gave up after giving it a go. Now I realize that not only was my understanding of Actionscript limited then, but my realization of what true pendulum physics entails was also somewhat amiss.

As with any new task I set out to complete (especially those requiring a significant level of new knowledge on my part), I start with an array of Google searches — search terms like pendulum math, pendulum physics and actionscript pendulum tutorial to name a few. What intrigued me is that, while the results for such searches were incredibly numerous, very few of them actually tackled the topic of realistic pendulum movement. Instead, the majority of them (like this informative tutorial) create a facade to display a fairly believable pendulum motion by setting some random oscillation variables to the rotation of the movie clip. While this may suffice for the average SWF presentation, I wanted to find a much more realistic look that actually allows the pendulum object to react to forces acting upon it.
Continue reading

Simulating Gravitational Forces in AS3

So, I may have bitten off a bit more than I could chew in this post, but it is something that has been an intriguing thought to me for some time: how can I simulate the effect, or pull, of gravitational forces between different objects on my stage? Obviously, if you read my previous post, you know that I have had some fascination with orbits and circular motions, but I am much more interested in paths that appear to be natural — almost evolving, if you will.

This got me to contemplating that, in our universe, every object acts upon every other in some way: keeping our planets in orbit around the sun while helping each other to retain their individual satellites and keep from hurtling into outer space. Embodying the interactions of the various bodies on one another and trying to come up with a simple enough algorithm that will simulate a fluid motion while not overloading the Flash player with unnecessary overhead was my goal. I have some work yet to do, but I thought I would at least share my concepts with you and see what improvements can be made whilst still keeping things lightweight.
Continue reading

WordPress Upgrade

Well, it’s overdue, and I’ve been slacking with the software a bit, but I finally got around to upgrading the blog to use WordPress 2.7. The admin interface is a definite improvement over the last few upgrades, and once again, when I’ve done something right, it makes me wonder what took me so long to carry out the upgrade. Anyway, it shouldn’t really affect you, the reader, at all, but things will hopefully be a little more streamlined now as we get under way for a new year of posting.

Enjoy!

The Woes of Tax Season

Isn’t it amazing how much we learn during tax season? It seems to me that, no matter how long I pay taxes, I always discover a new way to give the government more of my money each time this season rolls around. This year has proven no different. We made the move from Buffalo, NY to the Seattle area mid-year, and we were thrilled to see that we no longer had state income tax withholdings, because Washington state has no income tax. Well, that bubble burst in the last 24 hours when I discovered that good ol’ New York state won some controversial court case that allows them to tax — get this — nonresident income if the source of the income is in NY state. So, in essence, I get to pay income tax to NY state for all the money I’ve earned in the last 6 months while living in a stat with no income tax, and to top it off, as I’m filling out the forms in TurboTax, I get the nice little note that in addition to the taxes I need to pay, I may be hit with an underpayment penalty, since we didn’t pay it all through the year — wow.

Needless to say, I’m not too excited about the scenario, but what is there to do? The most frustrating thing is that the flux between the refund it seemed we were due to the amount we now owe is nearly a $3,000 spread… not fun. Anyway, I suppose I will end my little rant now, but I cannot justify paying taxes to a state when I no longer live there. Perhaps I should just be thankful that we now live in a state with no income tax, or we may well have had to pay both states what they claim is their due.