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

    May 2012
    M T W T F S S
    « May    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • games

    • Game Showcase
Mar 28

creating a platform game in as3 part 4

flash, game, platform, tutorial 4 Comments »

In the end of the last post we had a problem where the character vibrated. This was because when ever the character touched the floor i made it move up by one pixel. to rectify this i will make another hittest that detects when the character is one pixel off the floor and only calculate the velocity of the player if it returns false.

Some code:-
PLAIN TEXT
Actionscript:
  1. <span style="color: #666666;">function run(event:Event) {</span></div>
  2. <div><span style="color: #666666;"> velocity+=gravity;
  3. if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
  4. velocity = 0;
  5. char.y--
  6. }
  7. if (!land.hitTestPoint(char.x, char.y+char.height/2+1, true)) {
  8. char.y += velocity;
  9. } else {
  10. velocity=0;
  11. if (attemptJump==true) {
  12. char.y--;
  13. velocity =-5;
  14. }
  15. }
  16. if (goleft==true) {
  17. char.x+=lspeed;
  18. }
  19. if (goright==true) {
  20. char.x+=rspeed;
  21. }
  22. }

This is only the new run function because that's all i have changed.
I have removed the else statement that i created last time to calculate the velocity and moved it to the new hitTest where i calculate if the character hits one pixel above the land.
The other thing that i have changed is to move the attemptjump function to the new hitTest and add to it another char.y-- which means that the character will be able to jump with the new hitTest in place.
Heres a demo.
Ok thats that problem fixed. Now i have noticed that when you walk into a steep wall, you seem to go into it for a bit and then float to the top if you stop. To fix this we can make it so that you can't walk up steep hills. We can do this 2 ways, either by creating 2 more  movieClips, 1 either side of the character and calculating the gradient in between them. Or we can use the hitTestPoint function again and use it to calculate a point that stops the player moving if the hill is too steep. i have chosen the latter because it is easier and will run quicker.
First we need to change the land movieClip so that it has some steep hills at either side of the screen.
Next we have to create the new hitTest functions.
some code, just the new run function again.

PLAIN TEXT
Actionscript:
  1. function run(event:Event) {
  2. velocity+=gravity;
  3. if (land.hitTestPoint(char.x, char.y+char.height/2, true)) {
  4. velocity = 0;
  5. char.y--;
  6. }
  7. if (!land.hitTestPoint(char.x, char.y+char.height/2+1, true)) {
  8. char.y += velocity;
  9. } else {
  10. velocity=0;
  11. if (attemptJump==true) {
  12. char.y--;
  13. velocity =-5;
  14. }
  15. }
  16. if (!land.hitTestPoint(char.x-char.width/2, char.y+char.height/4, true)) {
  17. if (goleft==true) {
  18. char.x+=lspeed;
  19. }
  20. }
  21. if (!land.hitTestPoint(char.x+char.width/2, char.y+char.height/4, true)) {
  22. if (goright==true) {
  23. char.x+=rspeed;
  24. }
  25. }
  26. }

As you can see from the code i have put the new hitTest functions in front of the move left and right functions. Lets look at the first one, the "goleft" function. i have put a conditional statement in front of the old goleft function. Note the exclamation mark in front. This means that you can go left only if the character's left hand side, a quarter up from the foot, is not touching the land.
It is nearly the same code for the goright function, but if look carefully, you will see that instead of a "char.x-char.width/2" we have a "char.x+char.width/2" this is just declaring a point on the right side instead of the left.
Heres a demo
As you can see, the demo runs fine, you can't walk up steep hills but you can still jump up them. Thats fine for now.
In the next tutorial i am going to see what happens if i add a floating platform that you can go underneath and jump on to.
Mar 28

creating a platform game in as3 part 3

flash, game, platform, tutorial 3 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.
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, 0x112233);
char.graphics.beginFill(0x112233);
char.graphics.drawRect(200, 0, 20, 50);
char.graphics.endFill();
addChild(char);

var land:MovieClip = new MovieClip();
land.graphics.lineStyle(2, 0x112233);
land.graphics.beginFill(0x112233);
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.
Previous Entries Next Entries
Powered by WordPress .::. Designed by SiteGround Web Hosting

cssandhtml