Have fun and help me to get to 1,000,000 downloads!

APE – Actionscript Physics Engine tutorial – Part 2

Emanuele Feronato Flash

I’ve been playing a lot with APE in the last days, so I am ready to write another tutorial.

I strongly recommend you to read the first part before continuing.

The first thing I wanted to do is having the APE environment working on Flash CS3 instead of Flex.

It’s very easy: when you create a new Flash file (Actionscript 3), just go to “Publish Settings”, then “Flash -> Settings” then “Browse to Path” and then select the path to your ape_a045\source directory

Look at the picture and follow the 6 steps:

Flex to Flash

Now you are redy to execute APE files on Flash CS3, but it’s not over. In order to get the funniest features of APE, you have to update the current alpha 0.45 to the one you will find in the “trunk” of the code repository. Simply go to this link, and overwrite your files with the ones you find there.

Now you are really ready to rumble!

In the experiment I made I created 3 Movie Clips in my library: two squares with a gradient fill to use as background (linked as back and back_win), and a circle with another gradient to represent the wheel (linked as tire).

Then, as usual, my actionscript, all in the first frame:

Lines 1-4: Importing required libraries (only the one at line 1 is not included in the CS3 package and it’s APE itself)

Line 7: Adding the enter frame listener as explained in the first part

Lines 8-9: Adding listeners to keyboard events: I want to determine when a key is down and when a key is up. When it’s down, I will call the key_pressed function and if it’s up I will call the key_released function.

Line 12: Assigning the back object to a new sprite called backgr

Line 13: Same thing for the back_win object assigned to backr_win sprite

Line 14: Putting the backr sprite on stage

Lines 17-18: Creating to new objects of tire type (a new type invented by me) as instances of the tire MovieClip I created. They will represent tires graphic

Lines 21-22: Initializing APE engine as seen on the first part.

Line 23: This is the same of adding a massless force as explained in first part, but in the “trunk” version of APE addMasslessForce does not work anymore so you have to use VectorForce. The three parameters define if the force is massles (false) or not (true), the horizontal force and the vertical one.

Lines 24-44: Adding particles and constraints as seen in the first part, with the exception of line 32 where I call one rectangle particle as “floor”.

Lines 47-52: Setting the styles of display of the particles I created. Let’s examine the setStlye parameters:

lineThickness:Number (default = 0)

lineColor:uint (default = 0x000000)

lineAlpha:Number (default = 1)

fillColor:uint (default = 0xffffff)

fillAlpha:Number (default = 1)

Lines 53-54: Attaching tire sprites to wheels. Let’s examine the setDisplay parameters:


offsetX:Number (default = 0)

offsetY:Number (default = 0)

rotation:Number (default = 0)

Line 56: Adding the group to the APE engine as seen in first part.

Line 57: Setting a boolean win variable to false. It’s a game, so I have to check if the player wins

Lines 60-61: this listener is a brand new feature of the “trunk” version. I am listening if one of the wheels collide to another particle, and if true I call the check_coll function.

Line 63: Beginning of the check_coll function

Line 64: A variable called “collided” is set to the name of the particle the wheel collided with

Line 65: If a wheel collided to the rectangle object called “floor” (line 32)…

Lines 66-69: Set the win variable to true and display the other background, the one I reserved for the victory. Please notice that when you win, you won’t see the simulation anymore, but it’s still running. I’ll explain how to stop and reset the simulation in a future tutorial.

Line 73: Beginning of the function to be executed every time a key is pressed

Lines 74-77: If the player presses the “UP” key, I set the angular velocity of both wheels to 0.1

Lines 78-81: If the player presses the “DOWN” key, I set the angular velocity of both wheels to -0.1

Line 84: Beginning of the function to be called every time a key is released

Lines 85-86: Setting the angular velocity of both wheels to zero

Lines 89-92: Beginning of the simulation itself as explained in first part.

And it’s done! In this game you have to drive your “car” with UP and DOWN arrows and touch with a wheel the red platform

If you fall off the screen, you will have to reload the window.

This is the source code of this little game and this is the trunk version of APE in case you’re too lazy to download it by yourself. Give me feedback while I prepare the next round…

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 45

  1. Pingback: APE - Actionscript Physics Engine tutorial : Emanuele Feronato - blog of an italian geek

  2. Post
  3. abhilash

    i was so cool! but it only works in flash CS3
    :( will u stop doing tutorials in AS 2 becoz i have mx 7 and i can’t d’load flash CS3..

  4. Post
    Emanuele Feronato

    I won’t stop writing tuts in AS2 because at the moment most people have an AS2 version.

    But I think this is going to change in about a year.

  5. abhilash

    a year… i think all will get flash cS3 till then,right now i can’t d’load the trial too,coz that will be of more than 100mb and can u expect someone to d’load 100mb at a downloading rate on 10kb per second.. :)

  6. Henry

    I have run into a strange problem. I have been working with APE on one of my computers in CS# and everything is working perfect. But when I moved my project to another machine, I got this error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    I have the APE class on the new machine, so I can’t seem to think of what might be causing this. Have you run ino this yet?

  7. Post
    Emanuele Feronato

    adam: you get this error if you use an APE version different than the one in the trunk.

    Check the final part of the post to understand what I am talking about

  8. Paul

    no matter how hard I try I cannot get your second experiment to play.. I know you talked about the trunk and all of that but could you elaborate more?

  9. seb

    hello ther i do have the same message

    1046: Type was not found or was not a compile-time constant: CollisionEvent.
    …in french but it is actually the same message.
    I just downloaded the ape 0.45 trunk but i don’t understand why this doesn t work..
    Maybe some classes were removed… but i still can’t make it work

  10. RonB

    Very useful tutorial. Thanks! In part 2 you should mention that the docs for the original 0.45 are no longer entirely accurate for the current cvs version. And, since the author doesn’t say so in the docs, that the rotation of a rectangle particle is measured in radians.

  11. James

    Hi. i was gettign the same 1046 error. i put the shape i wanted to test for a collision in a class like this and it worked fine.

    package src

    import org.cove.ape.*;

    public class Exit extends Group

    public function Exit ()

    var exit:RectangleParticle = new RectangleParticle(650, 444, 50, 100, 0, true);
    addParticle (exit);
    exit.addEventListener (CollisionEvent.COLLIDE, checkColl);
    private function checkColl (e:CollisionEvent):void
    trace (“hit”);

  12. clark


    I get it to run but nothing happens, no balls roll off the screen or anything…

    if you get the error:

    Scene1,Layer “layer 1″, Frame 1, Line64

    the error that you get 1046:Type was not found or was not a compile-time constant:CollisionEvent

    you havent set up the paths to APE correctly
    to fix this:

    publish settings -> actionscript 3.0 settings -> add the destination to the APE classes.

    should fix the problem


  13. stan

    Great tutorial, thanks! Tell me if this make sense: I noticed that a circle and a wheel with the same properties will accelerate differently on the same downward slope and constant gravity force – the circle lags behind. I think this is due to the lack of ‘traction’ for the wheel.

    Is there a setting that I am missing somewhere that cause the circle to lag? Do you see any downside of using disconnected wheels instead of circles (for free moving round objects)?

    Thanks, Stan

  14. maliha

    Very Interesting tutorial :-)
    Actually im doing my collegeproject on a simulation of traffic flow along a road in flash ..:-(( and was wondering if i could have some guidelines from you concerning that..id be very grateful! Thank you

  15. Pingback: Flash physics engines galore : Emanuele Feronato - italian geek and PROgrammer

  16. brad

    Thanks, these are great starter tutorials.

    As far as the trunk files go, I replaced my APE files and it would not compile. I was getting the same error as others.

    However, when I deleted and redid my AS3 class path to the APE files it worked.

  17. Pingback: Tutoriel pour débuter avec le moteur physique 2D APE part2 | pandaZen

  18. Tobi

    thanks for the great tutorial, but unfortunaltely the collision event doesn’t works for me. I use Flash CS4 an the trunk version of APE. Error 1046 appears (line 63)…
    Didn’t get it to work.


  19. Nkaati

    Having problems with
    import org.cove.ape.*;

    It’s not importing them and as a result I get the error:
    1046: Type was not found or was not a compile-time constant: CollisionEvent.

    function check_coll(e:CollisionEvent):void {

  20. Tzenmoroth

    Love the tutorial

    Read all comments

    Keep getting same error as everybody else, noob CollisionEvent problem, downloaded trunk version or wtvr, i set up the path like the tutorial said, and im using flash CS3 Pro ftw =D

    I hope a real solution to this problem gets posted, I’ll keep trying

    ~ Farewell people I dont know ~

  21. Lucas Haley

    For the 1046 errors:

    For some reason, some of the ape_a045 distributions did not come with CollisionEvent.as. I’m not sure if that’s the issue, but maybe it’ll help to grab that file…

  22. WILEz

    Ok i solved…
    But now:

    C:\EasyPHP3.1\www\APETry\APE\org\cove\ape\RimParticle.as, riga 50 1137: Numero di argomenti non valido. Ne è previsto un massimo di 0.

    0 arguments for Vector??

  23. Pingback: flex??:APE?????? | flex??|flex ????|flex ??|flex java|jinFlex

  24. Thiago Bardez

    Hi, I’m a brazillian programmer and follower of your site EMANUELE FERONATO and I would like know how apply MOUSE EVENTS in the APE code … sorry for my ENGLISH, thanks.

  25. Bhuwan

    How to make it work with CS6?
    I did all the changes that were been told to do on this page
    and also changed Vector to ApeVector in each class….as it was conflicting the Flash Vector Class
    but still the example of the source code attached on that page doesnt runs on CS6

Leave a Reply

Your email address will not be published. Required fields are marked *