Create a Flash game like Pixel Purge step 1 – player movement

Are you playing Pixel Purge?

It’s a good mix between a shooter and an avoider.

I am going to show you how to create a replica of this game, and I’m starting from player movement.

In Pixel Purge, you control a spaceship with arrows or WASD keys in an level that’s as big as twice the visible area of the game. When you move the spaceship, it remains in the center of the stage, with the rest of the game scrolling consequently, until it travels too close to level borders. When it happens, the game itself stops scrolling and the spaceship leaves the center of the stage. Play the game to see what I mean.

This is the feature I am showing in this prototype. It’s quite simple, being a simple concept.

Classes are made for the sake of tutorial understanding, so experienced users may find them a little weird, but they will be improved as soon as I’ll add more elements to the game.

Anyway, this is how things will work: besides the routine to detect pressed keys, we have a background that’s twice as width and height than the movie itself. In this case, the movie is 640×480 and the background is 1280×960 pixels. The player starts in the middle of the background, with x and y properties respectively at 640 (1280/2) and 480 (960/2). The background is also centered in the stage, at x = -320 and y = -240.

This way at the very beginning of the game, both the background and the player are perfectly centered on the stage. Then, when the ship moves, in a way very similar to
Flash game creation tutorial – part 1 (with AS3 classes), the background scrolls in the opposite way, keeping the ship centered in the screen, until it gets too close to background borders. Then, the background stops scrolling. There is really nothing new, so I would start with the code:

This is the main class:

This is game_container_mc class, managing the background itself:

And finally player_mc class:

This is the result:

move the “ship” with arrow keys, get close to the edges to see what happens.

Download the source code. If you like this series, next time I’ll show you how to fire.

  • If it’s in space, where does the friction come from ?

    • Hawdon

      Games that are too realistic are often way too hard to play.
      If there was no friction the game would be close to impossible to play. :)

  • Poupi

    There’s a bug.
    Start from the center of the screen, then push down and left arrow (or two consecutive directions)
    At a certain point, the movement is quite strange, and looks something like :

    Great job anyway :)

    • Emanuele Feronato

      it’s the same glitch you can find in the original game… and it happens when you’re close to a level edge (i.e. the topmost one) and suddenly you get close to another edge (i.e. the leftmost one)

      • scaredBoy

        however, in the original game it’s not that puzzling.
        Probably because the background image has a parallax effect and move not as quickly as the player vehicle.

  • Me

    Very nice, thanks :)

  • gabriel

    how can you guys deal with AS3? This code is killing me. Why can’t we just stick to the good old AS2? WHY?!

    • BOB

      10 reasons:
      1.AS3 is OOP and is good for you
      2.AS3 is more efficient
      3.AS3 uses less stuff on stage
      4.AS3 makes less instance names so its easier to find
      5.AS3 is easier
      6.AS3 is more like C++/C/Java
      7.AS3 is MUCH faster to load
      8.AS3 makes a smaller filesize
      9.AS3 is simpler
      10.Its easier to find code (must be in class, or stage if its not important)

  • Pingback: 2.0 – flash és webfejlesztés » Heti linkajánló 2010/39()

  • Pingback: Create a Flash game like Pixel Purge step 2 – Firing bullets - Emanuele Feronato()

  • Thomas

    I would always recommend multiplying the momentum by -1 or -0.5 when the player hits a boundary. Having a sticky boundary (and in this case, the momentum stays even when you’re touching, so you can’t get off the wall within a second) is so much less appealing. Bouncy walls are fun.

    • Emanuele Feronato

      great idea, will be developed in next step

  • shawn

    Excuse me ! How to do “fire spread”?
    I added some code in this source ,fire spread is working,but it can’t fixed radian firing between bullet!

    I can send my source to you!

  • Lyker

    Hey cool, I searched a lot for a good tutorial and source code about a 2D Camera.

    Thanks :)

  • Kyle O’Donnell

    I was wondering how to incorporate walls into something like this? I love your survival game tutorial, but I can’t get those walls to work when I’m centered on a player.

  • Random Person

    Your contact page is broken.