-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.as
124 lines (118 loc) · 3.04 KB
/
Block.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
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
122
123
124
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Block extends MovieClip {
protected var _timer:Timer;
public var _onFinish:Function;
public var matrik:Matriks;
public function Block(tipe:int, onFinish:Function) {
// constructor code
matrik = new Matriks();
matrik.no = tipe;
matrik.rot = 0;
_onFinish = onFinish;
fill(matrik.arBlockRandom());
this.addEventListener(Event.ADDED_TO_STAGE, _init);
}
function _init(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, _init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
}
protected function kdown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.UP) {
rotasiKanan();
}
if (e.keyCode == Keyboard.DOWN) {
geserBawah();
}
if (e.keyCode == Keyboard.LEFT) {
geserKiri();
}
if (e.keyCode == Keyboard.RIGHT) {
geserKanan();
}
}
public function mulai() {
_timer = new Timer(1000);
_timer.addEventListener(TimerEvent.TIMER, updateTimer);
_timer.start();
}
protected function updateTimer(e:TimerEvent):void {
geserBawah();
}
public function posOffset():Point {
return new Point(Math.floor(this.x / Game._offset), Math.floor(this.y / Game._offset));
}
public function setPosisi(po:Point):void {
this.x = po.x;
this.y = po.y;
}
public function geserBawah():void {
var pos:Point = posOffset();
pos.y += 1;
if (Game.cekBisa(pos, matrik.arBlock())) {
this.y += Game._offset;
} else {
//selesai sampai bawah
stage.removeEventListener(KeyboardEvent.KEY_DOWN, kdown);
_timer.removeEventListener(TimerEvent.TIMER, updateTimer);
_timer.stop();
_onFinish.apply(this, new Array(posOffset(), matrik.arBlock()));
MovieClip(parent).removeChild(this);
}
trace(posOffset());
}
public function geserKiri():void {
var pos:Point = posOffset();
pos.x -= 1;
if (Game.cekBisa(pos, matrik.arBlock())) {
this.x -= Game._offset;
}
}
public function geserKanan():void {
var pos:Point = posOffset();
pos.x += 1;
if (Game.cekBisa(pos, matrik.arBlock())) {
this.x += Game._offset;
}
}
public function rotasiKanan():void {
matrik.rotKanan();
if (Game.cekBisa(posOffset(), matrik.arBlock())) {
wipe();
fill(matrik.arBlock());
} else {
matrik.rotKiri();
if (posOffset().x < 0) {
geserKanan();
rotasiKanan();
}
if (posOffset().x > (Game._lebar - matrik.arBlock().length)) {
geserKiri();
rotasiKanan();
}
}
}
protected function fill(mat:Array):void {
var ktk:MovieClip;
for (var i:int = 0; i < mat.length; i++) {
for (var j:int = 0; j < mat.length; j++) {
if (mat[i][j] == 1) {
ktk = new Kotak();
ktk.y = i * Game._offset;
ktk.x = j * Game._offset;
this.addChild(ktk);
}
}
}
}
protected function wipe():void {
this.removeChildren();
}
}
}