AS3 Flash game creation tutorial – part 3: walls
Welcome to the 3rd step of this tutorial.
It’s time to cover walls, or whatever you may call some fixed, deadly obstacles.
Continuing on the OOP lane, the new object called wall has a class coded in a file called wall.as saved in the same path as the other ones.
This is how we will create a wall object in the main file, as3circle.as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package {
import flash.display.Sprite;
import flash.events.Event;
public class as3circle extends Sprite {
public var keyboard_input:keys;
public var circle_hero = new circle;
public var level_wall = new wall;
public var number_of_coins = 4;
public function as3circle() {
for (var i=1; i<=number_of_coins; i++) {
var ingame_coin = new coin;
addChild(ingame_coin);
}
addChild(circle_hero);
var keyboard_sprite = new Sprite();
keyboard_input = new keys(this);
addChild(level_wall);
stage.addEventListener(Event.ENTER_FRAME,on_enter_frame);
}
public function on_enter_frame(event:Event) {
if (keyboard_input.is_left()) {
circle_hero.apply_force(-1,0);
}
if (keyboard_input.is_right()) {
circle_hero.apply_force(1,0);
}
if (keyboard_input.is_up()) {
circle_hero.apply_force(0,-1);
}
if (keyboard_input.is_down()) {
circle_hero.apply_force(0,1);
}
}
}
} |
A variable called level_wall with wall type is created at line 7, then I place the wall object into the game at line 17.
And that’s all for the main file. Look how clean it’s remaining, despite of all new features I am introducing in the game.
This is the power of classes. Remember.
As for the wall.as file, here it is, fully commented:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package {
import flash.display.Sprite;
import flash.events.Event;
public class wall extends Sprite {
// variables used in this class
private var point_x:int;
private var point_y:int;
// precision is the number of points I am going to calculate
// on hero's circumference
private var precision:int = 36;
// calculating once for all 2*PI
private var twopi: Number = Math.PI*2;
// main function
public function wall() {
place_wall();
// checking for collisions at every frame
addEventListener(Event.ENTER_FRAME, check_collisions);
}
private function check_collisions(e:Event) {
for (var i=0; i<precision; i++) {
// checking for collisions between the hero and the wall
point_x = as3circle(root).circle_hero.x + 25 * Math.cos(twopi*i/precision);
point_y = as3circle(root).circle_hero.y - 25 * Math.sin(twopi*i/precision);
if (this.hitTestPoint(point_x, point_y, true)) {
// in case of collision, reset the hero
as3circle(root).circle_hero.init();
}
}
}
private function place_wall() {
x=0;
y=175;
}
}
} |
To check for collisions between the hero and the wall, I am using trigonometry because in this case is a lot more accurate than hitTestObject().
You can find the theory behind this method of collision in Create a flash draw game like Line Rider or others – part 2.
And this is the result:
We are closer and closer to a complete game… next time I will cover scoring.
They can be easily customized to meet the unique requirements of your project.















(5 votes, average: 4.00 out of 5)









This post has 8 comments
somedude123
Hey why does it try to install some pdf file on my computer did you do that on purpose?
It does it everytime I come on you’re site.
Nathan
I ran out of balls, they all teleported into the walls. What could you do to prevent this?
Frederik
You could check with hitTestPoint, if the coin is over the wall whenever you place it. If it returns true, the replace it, and check again.
Monkios
>> Hey why does it try to install some pdf file on my computer did you do that on purpose?
It is the PDF page from the “ActionScript 2.0 to 3.0 Migration Cheatsheet” article that loads on the main page.
For this tut, gret job !
I’ve found a bug : When you play with it long enough, the balls begin to disappear. I don’t know if they are outside of the screen or just behind the obstacles (wich I prefer to walls for a name ..)
kisi
Mate,
your rss-feed doesnt work for me ;(
great website though!
Ryan
This is so bad.
I’m a complete noob.
So to understand this tutorial, I have to read the AS2 version. Which is completely useless!
Why can’t you comment it as well as you did the AS2 one. Explaining each line.
AS3 Flash game creation tutorial - part 4: score : Emanuele Feronato
[...] AS3 Flash game creation tutorial – part 3: walls I introduce some obstacles to kill the circle, now it’s time to make it earn some points when it [...]
Nathan
@Ryan
he’s just trying to translate it, not doing a full fledged tut