• Home
tudwaythecore.com
blog
  • Recent Posts

    • Detecting focus in flash player
    • BitmapData PerlinNoise
    • Creating bridges in APE part 2
    • Creating bridges in APE
    • SteerWheels 2
    • Creating a simple APE example
    • Design me some levels!
    • Finished Migrating
  • Recent Comments

    • tudway on Creating a simple APE example
    • tudway on creating a platform game in as3 part 4
    • venu on Making a Shooting Game in AS3
    • chris on creating a platform game in as3 part 4
    • Kronoshifter on Creating a simple APE example
  • Categories

    • APE
    • flash
    • game
    • platform
    • sound
    • tutorial
    • Uncategorized
  • Archives

    • May 2009
    • October 2008
    • September 2008
    • August 2008
    • May 2008
    • April 2008
    • March 2008
  •  

    March 2008
    M T W T F S S
        Apr »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • games

    • Game Showcase
Mar 28

creating a platform game in as3 part 3

flash, game, platform, tutorial Add comments
You should read this and this before continuing, if you haven’t already. 

This little platform engine works fine at the moment, but most platform games have more than one platform. i will add another platform and see how that goes.

if you move right on the screen you will see that we have a problem. the character is floating on the screen!! This is because we are using hitTestObject and not hitTestPoint. Lets change that.
So we can make some more complex terrain, i have made a new movieClip linkaged as ground, and another one linkaged as character. These replace the land and char movieclips that we drew in actionscript earlier.
code:-
import flash.display.*;
import flash.events.*;
stage.frameRate = 30;
stage.addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);    

var gravity:Number = 0.2;
var velocity:Number = 0;
var lspeed:int = -2;
var rspeed:int = 2;
var goleft:Boolean = false;
var goright:Boolean=false;
var attemptJump:Boolean=false;
var char:MovieClip = new character();
char.x=200;
char.y=0;
stage.addChild(char);

var land:MovieClip = new ground();
land.x=200;
land.y=300;
stage.addChild(land);

function keyPressed(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP :
attemptJump = true;
break;
case Keyboard.LEFT :
goleft = true;
break;
case Keyboard.RIGHT :
goright = true;
break;
}
}
function keyReleased(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP :
attemptJump = false;
break;
case Keyboard.LEFT :
goleft = false;
break;
case Keyboard.RIGHT :
goright = false;
break;
}
}

function run(event:Event) {
velocity+=gravity;
if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
velocity = 0;
char.y–;
if (attemptJump==true) {
velocity =-5;
}

} else {
char.y += velocity;
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}

 

 

 

The most important thing that i have changed is the hitTest function – if (land.hitTestPoint(char.x, char.y+char.height/2, true))  This is a much better way of doing it than hitTestObject because you can have angled platforms. Lets explain the if statement. if the “char” movieClip’s foot hits the land than do the function that follows. Lets focus on the char.y+char.height/2 this declares the “foot” of the moviclip as the registration point of “char” is in its centre. so centre + half the height gives the bottom.
The next thing that you will notice i have changed is to put a char.y– in the hitTest function. this just keeps the “char” movieclip on the top of the floor, so it doesn’t sink through it.
The only other thing that i have changed is to add an “else” statement to the if statement that checks the hitTest between the “char” movieclip and the “ground” movieclip, and move the line of code that makes the “char” fall (char.y += velocity;) into it. This just means that it only makes the “char” fall if its not touching the ground movieclip.
Demo
As you can see from the demo, you can successfully walk up and down slopes without problem. But the character seems to vibrate, which is kind of weird in a platform game! This is easily rectified and i will show you how in the next tutorial. The character also seems to be able to walk through steep hills.

3 Responses to “creating a platform game in as3 part 3”

  1. Cindy Says:
    August 11th, 2008 at 4:44 pm

    Thanks for the tutorial – this is great!

    I’m having a problem that I guess is related to something I’m doing wrong since no one else has commented on this. When I get to this part of the tutorial, my game no longer works (I see nothing), and I get two errors:

    Frame 1, Line 15: 1180: Call to a possibly undefined method character.
    Code: var char:MovieClip = new character();

    Frame 1, Line 20: 1180: Call to a possibly undefined method ground.
    Code: var land:MovieClip = new ground();

    I’m very new to ActionScript, so maybe there’s something very basic that I’m not doing. I know the code is pasted in my document just as you have it on your site – I’ve tried it several times.

    Any help would be greatly appreciated. Thanks!

  2. tudway Says:
    August 16th, 2008 at 1:22 pm

    Sorry i haven’t got back to you sooner (i was on holiday!) your getting these errors because you need to create a movieclip with a linkage of “character”and one with a linkage of “ground”.

  3. Ole Andreas Says:
    September 26th, 2008 at 3:00 pm

    Would really love the next tutorial:)

Leave a Reply

Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml