Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

APE – Actionscript Physics Engine tutorial – Part 2

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 = 0×000000)

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:

d:DisplayObject

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…

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (22 votes, average: 4.73 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 45 comments

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

    on October 23, 2007 at 6:11 pm

    [...] 23rd update: Part 2 [...]

  2. joeri

    on October 23, 2007 at 9:09 pm

    Wow, u are so amazing cant wait for the next tuts o man u are so cool

  3. David

    on October 24, 2007 at 6:03 pm

    does this actionscript work in Actionscript 2.0?

    i have Falsh MX (7)

    briliant tutorial anyway

  4. Emanuele Feronato

    on October 24, 2007 at 6:34 pm

    It works only with Flash CS3

    Anyway, you can download a full 30 days evaluation version at Adobe site.

  5. Teal

    on October 25, 2007 at 12:42 am

    This is great! I tried it out and it worked perfectly. I even managed to change around the terrain.

  6. asdfasdf

    on October 25, 2007 at 1:05 am

    is there a way to make this work in flash 6? thanks

  7. asdfasdf

    on October 25, 2007 at 1:12 am

    look at this site
    http://www.cove.org/flade/

  8. abhilash

    on October 25, 2007 at 6:51 am

    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..

  9. Emanuele Feronato

    on October 25, 2007 at 9:49 am

    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.

  10. abhilash

    on October 26, 2007 at 12:00 pm

    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.. :)

  11. torskmunken

    on October 30, 2007 at 4:24 pm

    Can someone tell me how i can draw something and make it a particle? How do i do that?

  12. Henry

    on October 31, 2007 at 3:49 am

    Emanuele,
    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:

    -Infinity
    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?

  13. adam

    on November 3, 2007 at 11:10 pm

    ive run into a similar problem:

    1046: Type was not found or was not a compile-time constant: CollisionEvent.

  14. Emanuele Feronato

    on November 4, 2007 at 3:35 am

    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

  15. Paul

    on November 17, 2007 at 8:46 pm

    I keep getting:
    1046: Type was not found or was not a compile-time constant: CollisionEvent.

  16. Paul

    on November 18, 2007 at 8:41 am

    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?

  17. seb

    on November 25, 2007 at 12:39 am

    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

  18. RonB

    on November 26, 2007 at 4:14 am

    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.

  19. James

    on December 17, 2007 at 12:24 pm

    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”);
    }
    }
    }

  20. clark

    on January 3, 2008 at 2:36 pm

    hi,

    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

    Clark

  21. pallzoltan

    on January 9, 2008 at 2:39 pm

    clark, maybe you forgot to press (and hold) up or down key? :)

  22. stan

    on March 5, 2008 at 11:10 pm

    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

  23. stan

    on March 5, 2008 at 11:11 pm

    sorry meant to say ‘lack of traction for the circle’ in the comment above…

  24. nick

    on March 27, 2008 at 5:44 am

    id like to thank you, this just got me through the first hurdle of AS3

  25. maliha

    on March 31, 2008 at 10:24 pm

    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

  26. Burak

    on April 6, 2008 at 2:03 pm

    After a lot of search, i have found your site, and those valuable basic tutorial on “APE”… Thank you very much !

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

    on April 25, 2008 at 12:21 pm

    [...] You can also find two tutorials on my blog here and here [...]

  28. Helpful

    on May 19, 2008 at 12:29 pm

    You can get this for AS2.

  29. brad

    on October 2, 2008 at 10:41 pm

    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.

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

    on December 12, 2008 at 10:52 am

    [...] http://www.emanueleferonato.com/2007/10/14/ape-actionscript-physics-engine-tutorial/ (EN) http://www.emanueleferonato.com/2007/10/23/ape-actionscript-physics-engine-tutorial-part-2/ (EN) [...]

  31. Mario

    on December 16, 2008 at 1:56 am

    Great tutorial :) I just put together a simple example ( sort of tutorial ) on using particles and spring constraints for those whom are starting to dig into APE :)

    http://mariofalomir.com/blog/?p=33

    Cheers

  32. Tobi

    on February 17, 2009 at 6:46 pm

    Hi,
    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.

    Tobi

  33. SyntheCypher

    on September 20, 2009 at 8:18 pm

    1046: Type was not found or was not a compile-time constant: CollisionEvent.

    Same problem as another three guys any ideas how to fix this?

  34. Nkaati

    on September 23, 2009 at 10:28 pm

    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 {

  35. Tzenmoroth

    on December 30, 2009 at 6:57 am

    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 ~

  36. volando

    on January 20, 2010 at 10:19 pm

    Lovely one. Many thanks

  37. Franci

    on January 28, 2010 at 11:59 am

    Hey.

    If you replace or just download all the files from this trunk and put them in the org/cove/ape folder then it works for me.

    http://ape.googlecode.com/svn/trunk/source/org/cove/ape/

    It’s a nice physics engine and i’ll continue to use it but, where’s the support on this one, do so little people actually work with physics in flash, because it seems there is very little support online.

  38. Lucas Haley

    on May 7, 2010 at 10:39 pm

    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…

  39. WILEz

    on September 11, 2010 at 12:17 pm

    Scena 1, livello ‘Livello 1′, fotogramma 1, riga 64 1046: Tipo non trovato o non è una costante della fase di compilazione: CollisionEvent.

    :(

  40. WILEz

    on September 11, 2010 at 12:55 pm

    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??

  41. flex??:APE?????? | flex??|flex ????|flex ??|flex java|jinFlex

    on October 23, 2010 at 2:56 am

    [...] ?flash cs3????? http://www.emanueleferonato.com/2007/10/23/ape-actionscript-physics-engine-tutorial-part-2/ flash??????????? [...]

  42. all in one laser printer

    on October 12, 2011 at 8:51 am

    I try to get information of APE(Ajax Push Engine) but I found this page. T T

  43. Thiago Bardez

    on May 18, 2012 at 7:07 pm

    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.

  44. Bhuwan

    on July 3, 2012 at 2:49 pm

    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

  45. Bhuwan

    on July 3, 2012 at 2:53 pm

    Hey Its done!!!

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×