Sebastian Thrun is Awesome

I’m doing the Stanford online AI class this year, taught by Professor’s Sebastian Thrun and Peter Norvig. And up in week 5, unit 11 is Markov Decision Processes (MDPs), Hidden Markov Models (HMMs) and State Transition Probabilities. Sounds dry, right?

Check this out…

Oh. My. God.

As a teacher myself, and one who genuinely tries hard to interest and engage students, Sebastian is a role-model. How absolutlely RARING-TO-GO is he?! To paraphrase: “I CAN’T WAIT to teach you all about this, and that, and this other cool shit! And you can use it for all kinds of neat stuff! Woo-hoo!” =DDD

And yeah, the course isn’t easy, and it requires time and effort and commitment – but it’s fun! And it’s rewarding, and I’m inspired as a student, and blown-away as a teacher.

If more teachers had this kind of passion, I sincerely believe that the world would be an entirely different place.

An introduction to ActionScript 3.0 – week 6

Week 6 of the ActionScript intro carries on with the theme of coding rather than just talking about coding and as such again comes as a word document instead of slides. This week is where we start to have some fun with classes and dynamic creation of symbol instances. We start off by binding a bubble image to a simple class, and then tie it into the mouse cursor position, add in some colour transforms to liven things up and and stop our instances from giving you eye cancer by making them fade in and out through some manipulation of the each instance’s alpha property.

In this bundle I’ve combined both Week 6 Lessons 1 and 2 into a single document and provided the full source code to the exercises, but I highly recommend that you either start from scratch using the document as a guide or work from the 1st example source code and only peek at the completed source code if you get stuck because if you’re reading this then you want to learn to code, and you’re not going accomplish that by copying and pasting alone.

By the end of this tutorial, you’ll be able to build this:

Not a bad starting point for moving onto some way cooler stuff, eh?

Download link: An Introduction to ActionScript 3.0 – Week 6 (both lessons combined)
Audience: Beginners who know a little about variables, functions, objects and how to perform some basic programming math.
Format: PDF
Content License: This material is released under a creative commons non-commercial attribution share-alike 3.0 license by me (r3dux) and comes with no guarantee of correctness, fitness for purpose or anything of the sort.

Comments/feedback always welcome.

Bonus: You can find the code for the above animation with the start/stop on click code here.

ActionScript 3: How to use Event Listeners to Call Functions with Parameters

By default, you can’t. But there’s a way around it so you can by creating a custom event class! In this case, I’m going to deal with extending the Timer class to create a custom timer class with additional properties that we can use to pass data to functions.

The Problem

To demonstrate the problem, consider the following simple timer code:

This will work just fine – it’ll call the greet function once to output “Hello!” three seconds after the timer was started. But what if I wanted to say “Hello, SOME_NAME_STRING”, like “Hello, Bob!”, well you might think you could just do something like this:

But you CAN’T! ActionScript 3 doesn’t allow you to pass parameters other than the TIMER event (i.e. the timer going off) to functions which are bound to a timer. And even changing the order of the parameters, or using a line such as the one below simply won’t work:

The Solution

Now, there are things you can do like use intermediate functions which might call a second function, or using anonymous in-line functions (there’s a good forum thread on it here if you want to investigate that route) – but there’s a better way of doing it: We can just create our own custom Timer class!

For this simple text example we could use something like this:

Custom Timer Class (String version) Code:

And then we could use our custom timer like this:

Flash File Code:

This will print out “Hello, Bob!” just fine using the additional property in our custom timer class! :)

Now that’s all well and good, but most people want to modify some manner of DisplayObject (Sprite, MovieClip etc.) when the timer fires, so for that we can just modify our custom timer class to expect a DisplayObject like this:

Custom Timer Class (DisplayObject version) Code:

Flash File Code:

Note: The above code assumes you’ve created a symbol with the class name “Circle” – to do so just draw a circle on the stage, stab F8 to convert it to a symbol, and check the Export for ActionScript checkbox and enter Circle as the class name before hitting the [OK] button.

Just like our String example, adding the additional property to our custom timer class allows us to bind the timer to a specific object which we can then manipulate from the function called when the timer fires! So in this case, two seconds after the timer starts our fadeIn function will be called and our circle instance will have its alpha property set to 1 (so it’s fully opaque – hence visible!).

Wrap Up

I came up against this problem whilst helping out some of my ActionScript students who are first-time coders, so wanted to code more Procedurally than Object-Oriented. When you use OOP there are better ways of going about things, but this is still a useful trick to know when you’re dealing with Event Listeners and Timers. I hope it helps out anyone who’s been banging their head against this problem! Cheers!

ActionScript 3.0: A Dynamic Frame Rate Switching Class to Lower CPU Usage

Flash gets a lot of negative press because it’s seen as using a heap of CPU time and bogging everything down. And it’s a fair cop. Most flash will eat up your CPU cycles even when it’s sitting there doing nothing. But this isn’t a fault of flash, but rather of flash developers. Let me explain…

When you start a piece of flash work, you assign it a frame rate at which you want it to run, so it’ll update the screen, say, 30 times a second. This is all fine and good for when you’re animating things on the stage. But what about when you’re not? Well, it’s still running at 30 frames per second and chewing up your CPU like a crazy melon farmer. This is Not A Good Thing. So, anyhow, I’m watching this video about SWF Framerate Optimisation, and the guy’s showing how you can modify your code to lower the frame rate when there’s not a lot happening, and bring it back up when you’re animating. So I had a crack at it, and lo & behold, it works fine for the specific piece of flash I’d coded it into, so I wondered if I couldn’t just go and make a RateController class. This way, I could add a RateController object to any project to dynamically change the project’s frame rate depending on whether the mouse was over the stage or not.

And after much swearing about not having global access to the stage properties, I found that I COULD!!!

Here’s a working example placed into the attracting particles code I wrote yesterday:

Note: The animation starts at full speed for two seconds on startup. It’ll drop to the sleeping rate (5 fps) two seconds after the mouse leaves the stage, and then ramps back up to its waking rate (30 fps) instantly when the cursor is back over the stage. The FPSCounter shows intermediate numbers because it’s based on an average.

To add a RateController to any flash project, you can just use something like:

Not bad, eh?

Full class code & file downloads after the jump…

Continue reading ActionScript 3.0: A Dynamic Frame Rate Switching Class to Lower CPU Usage