Teaching Flixel

A few weeks ago, I started teaching some kids how to make games with Flixel and other free tools. I'm offering my time up as a volunteer, at a progressive school I came across through this inspiring blog. Here is an email I just wrote to my students:

Hi class,

I'm really tired right now. I should be sleeping. But I'm writing these instructions to you now because of something that is more urgent to me than sleep. River sent me an email asking me again to explain how to finish the animation and change where the bullet shoots from. He apparently wanted to figure this out enough that he bothered to remind me about it. He didn't have to do that. I don't have to do anything for you guys. But he wanted to. And because he wanted to, I will do whatever it takes to help him get there, even though I'm really busy, even though I'm tired. Because this is the whole reason that I decided to try teaching a class at PSCS. I came here on the chance that I might be able to help someone get somewhere they really want to go but can't get to by themselves. I'm not an expert in most things, but this opportunity to share what I can with people who want it is very motivating for me.

And I've been waiting for this. And now I see the beginnings of it.

I'm not attached to you wanting to learn game programming, or game design, or whatever. You love playing games, and I think there is a chance that you may come to love creating games as well, once you get more of a taste of it. It is an acquired taste. It might not be your thing. Which is fine with me. I don't really care personally either way. But if it is, and if there is a chance that it might be, I will do whatever I can to catch that spark in you, and help you kindle it into a steady flame, if you so choose. Just let me know.

Yeah. So, those instructions...

You added some code in the beginning of update() that said this:

var moved:Boolean = false;

Then you added code to each if-statement for the arrow keys, to changed moved to true, if you press an arrow key during the update.

moved = true;

So you have a virtual bucket, sitting in your computer's memory, which you call moved, in which you can put either true or false. But it doesn't do anything by itself. It just sits there.

However, it is useful. Because you can look at it. If it has true inside it, you know that an arrow key was pressed, because you put true in it whenever an arrow key is pressed. Right? If it has false inside it, you know that no arrow key was pressed.

Say you wanted to do something only if an arrow key was pressed, and another thing if no arrow key was pressed. Like play a walk animation, or play an idle animation. If you wanted to do that, then it might be helpful to look into this moved bucket, and see what's inside.

You could say...

If moved has true inside of it, then play the walk animation.
Otherwise (if it is false), play the idle animation.

And you could translate that into actual AS3 code, like this:

if (moved)

And you would put that code in update(), after all the arrow keys and such have been checked.

Some notes:

if (moved) is the same as if (moved == true) - because what you put between the parentheses in an if-statement is checked to see if it is equal to true. If it is equal to true, the code in the curly braces is run. Otherwise, it runs the code for else.

Why does if (FlxG.keys.LEFT) work to see if the left arrow key is pressed? Because FlxG.keys.LEFT is a virtual bucket that holds true if the left arrow key is pressed and false otherwise. Get it?

Also, there is no way to just stop an animation. You must have another animation to replace it. For example, you could create an animation, with _box.addAnimation(), that is called "idle" and that has only one frame in its list of frames. Then when you play that animation, the _box would stay on that one frame forever, unless you play a different animation later on.

You might also wonder whether telling the _box to play an animation, telling it again and again every frame, might make it start over from the beginning every frame and not get anywhere. There is a way to do that. But fortunately, Flixel will not restart the animation if it's already playing. So this works.

There you have it - the instructions for how to make an animation only play while moving. It will take a bit of work, a bit of thought, a bit of failure and frustration and trial-and-error in order to get it to work in your particular program. But this is the price we pay.

I do not demand success from you. But I do demand that you try, or I cannot help you. And I suggest that you try more than once, even if you fail at first. You have unlimited lives, when it comes to programming. Remember that.


Making bullets shoot from somewhere other than the top left corner of the box sprite.

This is easier.

Find the code where you first create the bullet. It looks something like this:

var bullet:FlxSprite = new FlxSprite(_box.x, _box.y);

Remember? That creates a new FlxSprite, and gives it the name bullet, and puts it at a certain spot on the screen. That certain spot on the screen happens to be the upper left corner of the _box sprite.

If the box sprite has its corner at the coordinates (5, 10) and it is 50 pixels wide and 50 pixels high, then where is its center?

I'll tell you the answer. Its center is at the coordinates (5 + 50 / 2, 10 + 50 / 2), which is the same as (5 + 25, 10 + 25), which is (30, 35).

To put it more generally, the box's center is at (_box.x + 25, _box.y + 25). Just add an offset of (25, 25) and you get the center. Right?

What if you wanted to put the bullet at the center of the box?

var bullet:FlxSprite = new FlxSprite(_box.x + 25, _box.y + 25);

Would that work? I suggest you try it out and see.

What if you want it to appear somewhere other than the center? Well, try using some other numbers, instead of 25.

Once you get that working (which I'm sure you will), try making an animation for the bullet. Can you do it?

Show me on Monday.

If you have any questions, let me know before the weekend.

Good luck.

1 comment:

axcho said...

...and thanks to teaching this class I've also just discovered that writing things out like this doesn't work, however clear the explanations may seem to someone who already understands programming.

What we need is a game, something like Gamestar Mechanic but for actual programming instead of game design. Teaching it the way it's meant to be taught - that is interactively, through experimentation on the computer, with the feedback amped up and progression carefully tuned.

Something like this might already exist, to a degree, but nothing really accessible. If I could make something like this in Flash, for anyone to try out on the web, that would be something.

Much to do. How come all the things worth doing are so hard to do? :p