Actionscript Flash

Alternativa Platform 3D test – Part 3

I explainded previously how to add textures, lighting, shadows and sky, which greatly improved the look of the test sample I’m working on. But wouln’t it be nice if we could customize the controller to detect collisions, so that we can’t actually walk through the walls or the ground ?


The basis to do so, is to extend the controller class and override / add customized features. Remember how to create a controller instance ?

Alternativa 3D provides a basic ellipsoide collider object, so we’re going to make good use of that. What we need to do is pass such a collider object to the controller, as well as the list of collidable objects, in our case the “whole world”, or everything contained in the rootContainer, where the box objects are stored.

The last 2 parameters, speedMultiplier and mouseSensitivity, are optional, but they can be used for personal adjustments.

Well, what we need now is to create the MyController class which will extend the controller class. At the same time, this will show you the basics of extending classes in AS3 – something I just learnt as I’m still a beginner in this field :

Acceleration is made default, so I need to override the accelerate method to make you walk instead of run :

Remember that the controller’s update() method is called on every frame ? This is where user actions are taken into account and position of the object – in this case the camera – is calculated and updated. We are going to override the method :

Here’s a link to the demo. I changed the starting point, so you can experience the collision on ground when falling off the wall.

Once you’ve done that, you’re in the maze and can’t climb back on the wall again. I thought it would be nice to allow that, so I included further customization to add the ability to jump up. It’s more some kind of jetpack feature as you can actually stay in the air.

First thing, I bind the space bar to the ACTION_UP hook, in the main class :

I don’t have to, of course. I could create a new hook, and attach the implementation to it, but as ACTION_UP wasn’t used any more, I thought I could override it for my own purpose :

Next, I need to calculate the effect of the jetpack, which will simply end up producing a opposite fallspeed. While falling is achieved by a negative speed, jumping will be positive. I didn’t think it very far, I just considered the jetpack power as an opposite gravitational power, which will decrease untill having no more effect. Let’s see :

That’s it, I get the wanted behavior ! Further development could be to limit the effect of the jetpack to a restrained period of time, so you wouldn’t stay in the air as long as you wished. Anyway, as usual, you can test the updated demo.


Alternativa 3D test demo list