Step by step AS3 translation of Flash game creation tutorial – part 2

This is the second part of Tim Edelaar‘s step by step AS3 translation of Flash game creation tutorial.

Now it’s time to translate to AS3 the content of Flash game creation tutorial – part 2 which I suggest you to read before this one.

The style of the coding is the same of the previous part, with actionscript written directly in the timeline with the critical parts commented.

The bounds

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
 
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
 
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
 
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
 
//create the function main.
function Main(event:Event){
	CheckKeys();
	MoveHero();
	CheckCollision();
}
 
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is pressed.
		key_left = true;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is pressed.
		key_right = true;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is pressed.
		key_up = true;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is pressed.
		key_down = true;
	}
}
 
function KeyUp(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is released.
		key_left = false;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is released.
		key_right = false;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is released.
		key_up = false;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is released.
		key_down = false;
	}
}
 
function CheckKeys(){
	if(key_left){
		xspeed -= speed;
	}
	if(key_right){
		xspeed += speed;
	}
	if(key_up){
		yspeed -= speed*thrust;
	}
	if(key_down){
		yspeed += speed*thrust;
	}
}
 
function MoveHero(){
	hero.x += xspeed;
	hero.y += yspeed;
	xspeed *= friction;
	yspeed += gravity;
	yspeed *= friction;
	hero.rotation += xspeed;
}
 
function CheckCollision(){
	//Checks if left border hits the wall.
	if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if right border hits the wall.
	if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if upper border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if lower border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
}

The coin – 1st attempt

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
 
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
 
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
 
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
 
//create the function main.
function Main(event:Event){
	CheckKeys();
	MoveHero();
	CheckCollision();
}
 
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is pressed.
		key_left = true;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is pressed.
		key_right = true;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is pressed.
		key_up = true;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is pressed.
		key_down = true;
	}
}
 
function KeyUp(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is released.
		key_left = false;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is released.
		key_right = false;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is released.
		key_up = false;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is released.
		key_down = false;
	}
}
 
function CheckKeys(){
	if(key_left){
		xspeed -= speed;
	}
	if(key_right){
		xspeed += speed;
	}
	if(key_up){
		yspeed -= speed*thrust;
	}
	if(key_down){
		yspeed += speed*thrust;
	}
}
 
function MoveHero(){
	hero.x += xspeed;
	hero.y += yspeed;
	xspeed *= friction;
	yspeed += gravity;
	yspeed *= friction;
	hero.rotation += xspeed;
}
 
function CheckCollision(){
	//Checks if left border hits the wall.
	if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if right border hits the wall.
	if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if upper border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if lower border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Now do the same for our coin.
	if(coin.hitTestPoint(hero.x,hero.y,true)){
		coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
		coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
	}
}

The coin – 2nd attempt

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
 
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
 
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
 
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
 
//create the function main.
function Main(event:Event){
	CheckKeys();
	MoveHero();
	CheckCollision();
}
 
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is pressed.
		key_left = true;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is pressed.
		key_right = true;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is pressed.
		key_up = true;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is pressed.
		key_down = true;
	}
}
 
function KeyUp(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is released.
		key_left = false;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is released.
		key_right = false;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is released.
		key_up = false;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is released.
		key_down = false;
	}
}
 
function CheckKeys(){
	if(key_left){
		xspeed -= speed;
	}
	if(key_right){
		xspeed += speed;
	}
	if(key_up){
		yspeed -= speed*thrust;
	}
	if(key_down){
		yspeed += speed*thrust;
	}
}
 
function MoveHero(){
	hero.x += xspeed;
	hero.y += yspeed;
	xspeed *= friction;
	yspeed += gravity;
	yspeed *= friction;
	hero.rotation += xspeed;
}
 
function CheckCollision(){
	//Checks if left border hits the wall.
	if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if right border hits the wall.
	if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if upper border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if lower border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Now do the same for our coin.
	if(hero.hitTestObject(coin)){
		coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
		coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
	}
}

The coin – 3rd attempt

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
 
//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = 0.98;
var gravity:Number = 0.04;
var thrust:Number = 0.8;
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
 
//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
 
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);
 
//create the function main.
function Main(event:Event){
	CheckKeys();
	MoveHero();
	CheckCollision();
}
 
//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is pressed.
		key_left = true;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is pressed.
		key_right = true;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is pressed.
		key_up = true;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is pressed.
		key_down = true;
	}
}
 
function KeyUp(event:KeyboardEvent){
	if(event.keyCode == 37){		//checks if left arrowkey is released.
		key_left = false;
	}
	if(event.keyCode == 39){		//checks if right arrowkey is released.
		key_right = false;
	}
	if(event.keyCode == 38){		//checks if up arrowkey is released.
		key_up = false;
	}
	if(event.keyCode == 40){		//checks if down arrowkey is released.
		key_down = false;
	}
}
 
function CheckKeys(){
	if(key_left){
		xspeed -= speed;
	}
	if(key_right){
		xspeed += speed;
	}
	if(key_up){
		yspeed -= speed*thrust;
	}
	if(key_down){
		yspeed += speed*thrust;
	}
}
 
function MoveHero(){
	hero.x += xspeed;
	hero.y += yspeed;
	xspeed *= friction;
	yspeed += gravity;
	yspeed *= friction;
	hero.rotation += xspeed;
}
 
function CheckCollision(){
	//Checks if left border hits the wall.
	if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if right border hits the wall.
	if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if upper border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Checks if lower border hits the wall.
	if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
		hero.x = 275;
		hero.y = 200;
		xspeed = 0;
		yspeed = 0;
	}
	//Now do the same for our coin.
	if(hero.hitTestPoint(coin.x,coin.y,true)){
		coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
		coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.
	}
}

And that’s all for today.

Download source codes and thank Tim Edelaar for the contribution

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

7 Responses

  1. Graham says:

    I think, when using circles, you might want to do a radius+radius distance test fro a very accurate hittest.
    For example:
    var dx = ball.x-coin.x
    var dy = ball.y-coin.y
    var dis = Math.sqrt(dx*dx+dy*dy)
    if (dis < (rad1 + rad2)) {
    // it hit
    }

  2. Alex L. says:

    One thing you may want to do is make it so the coin cannot appear in the black border, or else it is impossible to get.

  3. Robin W. says:

    @Alex L.
    That has already been taken care of with this code:
    coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
    coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.

  4. sorby says:

    @ Robin W
    That has already been taken care of with this code:
    coin.x = Math.random()*510+20; //Changes the x of the coin to a random number between 20 and 530.
    coin.y = Math.random()*360+20; //Changes the y of the coin to a random number between 20 and 380.

    The only problem is that the width of the stage is 500, so if your getting a random number between 510 and 20 there’s a chance the coin may appear off the screen.

  5. ZealousZephyr says:

    I wrote a function for circle hit tests for a game before.

    function circleHitTest(
    circ1:MovieClip,
    circ2:MovieClip,
    extra:Number = 0):Boolean{
    //get distances between two circles
    var dx:Number = circ1.x – circ2.x;
    var dy:Number = circ1.y – circ2.y;
    //get “direct” distance
    var td:Number = Math.sqrt(dx*dx + dy*dy);
    //if the radius of both circles is
    //larger than the distance, then hit
    if(td < circ1.width/2 + circ2.width/2 + extra){
    return true;
    }
    return false;
    }

    (I hope the code displays correctly.)
    The “extra” parameter is if you want it to register as a hit if they are further away from eachother(I used it in my game to make sure that circles don’t spawn to close to each other).

  6. kyle_mantesso says:

    hey just wanted to thank you for all the tutorials! im writting a game in AS3 using the nintendo wiimote for uni and I have found these tutorials very useful, thank you!

  7. escorpior says:

    Realy nice tutorial, thank you very much.

Leave a Reply