FreshNova - Home

Wednesday 10th of March 2010

Home Flash Tutorials Exploring the Dark Room Using the Masking Property in Action Script 3
Exploring the Dark Room Using the Masking Property in Action Script 3 PDF Print E-mail
Written by Waleed Al-Bahrawy   
Tuesday, 10 February 2009 12:09

 

 

 

Using the masking property in Action Script 3 an object can mask the content of another object.

The masking object will show only the content of the masked object that is in it's range.

Many game developers are using this property in their games and I used it in my game Sky MazezZ (starting from level 15), where the level will be dark and the player can only see a limited area of the maze and doesnt know what is coming.

The above example has the same idea and this is what i did to make it :

1-I drew a map and converted it to a symbol movie clip and called it map.

7

 

2- I drew two circles, one for masking and called it maskBall and the other is the visible one and called it you. So you will move in the dark and maskBall will show the area around it!!

you_maskball

 

3- Then I did the masking part using Action Script.

 

Here is the code I used to make it.

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
map.mask=maskBall;//using the mask property to make the ball mask the stage
var left,right,down,up:Boolean;
 
addEventListener(Event.ENTER_FRAME,moveIt);//calling the followMouse function
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyPressedUp);
 
function moveIt(e:Event) {
if(up){
if(you.y>20)
you.y--;
}
if(down){
if(you.y<(stage.stageHeight-50))
you.y++;
}
if(left){
if(you.x>20)
you.x--;
}
if(right){
if(you.x<(stage.stageWidth-20))
you.x++;
}
 
maskBall.x=you.x;
maskBall.y=you.y;
}
 
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode==37) {
left=true;
}
if (event.keyCode==39) {
right=true;
}
if (event.keyCode==38) {
up=true;
}
if (event.keyCode==40) {
down=true;
}
}//keyPressedDown
 
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode==37) {
left=false;
}
if (event.keyCode==39) {
right=false;
}
if (event.keyCode==38) {
up=false;
}
if (event.keyCode==40) {
down=false;
}
}//keyPressedDown

 

Description of the code:

 

Line 1: This is the most important line in the code because it will do the masking for us.

map.mask=maskBall;

 

This code means that the mask of map is maskBall. So all the parts of the map will not be visible except the parts the mask covers.

 

Line 2: This line has the variables for the movement of the ball. Their values will be changed by two functions keyPressedUp and keyPressedDown.

Lines 8-28: These lines have the moveIt function that is resposible for the movement of you and maskBall.
Lines 26-27: Are assigning the position of you to the position of maskBall. So they will move together as one part.
Lines 30-43: These lines have the keyPressedDown which will detect the user's keyboard presses using the KeyboardEvent event.
Lines 45-58: These lines have the keyPressedUp which will detect if the user released the key using the KeyboardEvent event.
Thats It!!!
You will find the source file in the attachments below.
Attachments:
FileFile sizeLast Modified
Download this file (MaskMap.fla)MaskMap64 Kb11/02/09 20:51

Comments

Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Last Updated on Thursday, 12 February 2009 01:07
 

Archive

 

Categories


Copyright © 2009 FreshNova, by Talha Kalim and Waleed Al-Bahrawy