• 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

    • random on Making a Shooting Game Part 2
    • SammyG on Making a Shooting Game Part 2
    • Jim on Making a Shooting Game Part 2
    • rraven on Making a Shooting Game Part 2
    • Adrian on Making a Shooting Game Part 2
  • Categories

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

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

    February 2012
    M T W T F S S
    « May    
     12345
    6789101112
    13141516171819
    20212223242526
    272829  
  • games

    • Game Showcase
Mar 27

creating a platform game in as3 part 2

flash, game, platform, tutorial No Comments »

Read creating a platform game in as3 if you haven’t already.

In this tutorial i will show you how to make your character move and jump.
So… we need to make a function for key up events and for key down events to monitor key input so that we can have player controlled characters.
lets put this into 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 MovieClip();
char.graphics.lineStyle(2, 0×112233);
char.graphics.beginFill(0×112233);
char.graphics.drawRect(200, 0, 20, 50);
char.graphics.endFill();
addChild(char);

var land:MovieClip = new MovieClip();
land.graphics.lineStyle(2, 0×112233);
land.graphics.beginFill(0×112233);
land.graphics.drawRect(0, 350, 400, 25);
land.graphics.endFill();
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) {
char.y+=velocity;
velocity+=gravity;
if (char.hitTestObject(land)) {
velocity = 0;
}
if (attemptJump==true) {
velocity =-5;
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}



Click inside the demo to focus than use the arrow keys to move and jump. You will notice a problem where you can float, we will fix this later, but for now the new code…explained!
Lines 5-6: New event listeners for the key up and key down events.
lines 9-10: These are new speed variables, left speed and right speed.
lines 11-13: These are boolean statements, true or false statements. They are declared now rather than when i use them.
Key Pressed Function: This function is called whenever a key is pressed. When the UP key is pressed the attemptjump boolean = true. When the LEFT and RIGHT keys are pressed the booleans goleft and goleft are called true respectively.
Key Released Function: This function is called whenever a key is released. In the event of the UP key being released attemptjump is called false, the other booleans are also called false.
Run function: I have made some changes to the run function, where i make use of the attemptJump, goleft and goright booleans. Another if statement for each boolean. eg if (attemptJump==true) . This is called whenever the attemptJump boolean is true, so when the UP key is pressed and it makes the velocity = -5. I use 2 more if statements for the other booleans, where i increase the “char”‘s x position by lspeed, if goleft = true, or rspeed, if goright = true.
Now to sort out that unlimited jumping issue.
I think we can sort this by only allowing the player to jump if its touching the floor.
some code, just the new run function.
function run(event:Event) {
char.y+=velocity;
velocity+=gravity;
if (char.hitTestObject(land)) {
velocity = 0;
if (attemptJump==true) {
velocity =-5;
}
}
if (goleft==true) {
char.x+=lspeed;
}
if (goright==true) {
char.x+=rspeed;
}
}
All i have changed is put the if (attemptJump==true) statement inside the hittestobject statement. This means that it only checks to see if the player is attempting to jump if the “char” movieClip is touching the “land” movieClip.
A demo is in order!
Thats it for the 2nd part of my platform tutorials. Please wait for the next in the series.
Mar 27

Creating a platform game in as3

flash, game, platform, tutorial 2 Comments »

I’ve always wanted to create a platform game, so here goes. 

In a platform game we have a moving character, several platforms, and some type of vector, such as gravity or wind.
I’m going to create all of the movieclips in actionscript for now, so you won’t need to set anything up.
import flash.display.*;
import flash.events.*;
stage.frameRate = 30;
stage.addEventListener(Event.ENTER_FRAME, run);  

var gravity:Number = 0.2;
var velocity:Number = 0;
var char:MovieClip = new MovieClip();
char.graphics.lineStyle(2, 0×112233);
char.graphics.beginFill(0×112233);
char.graphics.drawRect(275, 0, 20, 50);
char.graphics.endFill();
addChild(char);

var land:MovieClip = new MovieClip();
land.graphics.lineStyle(2, 0×112233);
land.graphics.beginFill(0×112233);
land.graphics.drawRect(0, 350, 550, 25);
land.graphics.endFill();
addChild(land);

function run(event:Event) {
if (char.hitTestObject(land)) {
velocity = 0;
}
char.y+=velocity;
velocity+=gravity;
}

 


Ok you may need to refresh the page to view the swf.
as you can see, the character block falls down with increasing velocity and as it hits the floor it stops.
Lets explain the code so far:-
Lines 1-2: Importing the packages that i will use in my actionscript file
line 3: Setting the framerate to 30, this is how many frames per second go past.
line 4: This is an eventlistener that runs the function run on every frame, so 30 times a second.
lines 5-6: Setting up the variables, gravity will be a constant but velocity will change.
line 7: Setting up a new movieClip called char
line 8: The lineStyle of the char movieClip, 2 pixels thick and a dark blue colour.
line 9: The fill colour of char, same as the line colour
line 10: Creating a shape for the char movieclip, this is a rectangle. drawRect(x, y, width, height)
line 11:Telling flash to end the fill that we started on line 9
line 12: Adding the char movieclip to the stage.
lines 13-19: Setting up the land movieclip, the same way as we created the char movieclip.
line 20: Declaring the function “run” with an event type of event.
lines 21-23: An if statement that says if the char movieClip collides with the land MovieClip than set the velocity to 0.
line 24: Setting up the gravity vector, so that the “char” movieClip’s  y axis is affected by velocity.
line 25: Increasing the velocity variable by gravity once per frame. This will make the char fall to the land with increasing speed.
Thats all for this first post on creating a platform game in as3.
The next section will be out shortly.
Next Entries
Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml