Game tembak tembakan sederhana
Code yang digunakan untuk membuat sasaran tembak
package
{
import
flash.display.MovieClip;
public
class
Musuh
extends
MovieClip{
//default radius = 30
private
var
radius:
int
=
30
;
public
var
id:
int
;
public
function
Musuh(radius:
int
){
this
.radius = radius;
//gambar kepala
graphics.lineStyle(
5
,
0xFFFFFF
);
graphics.beginFill(
0xFF0000
);
graphics.drawCircle(
0
,
0
,radius);
graphics.endFill();
//gambar mata
graphics.beginFill(
0x00FF00
);
graphics.drawCircle(radius/
3
,-(radius/
3
),radius/
5
);
graphics.drawCircle(-(radius/
3
),-(radius/
3
),radius/
5
);
graphics.endFill();
//gambar mulut
graphics.moveTo(-(radius/
3
),radius/
3
);
graphics.lineTo((radius/
3
),radius/
3
);
}
}
}
Code yang digunakan untuk membuat bidikan pada tembakan
package
{
import
flash.display.Sprite;
public
class
BidikanPoint
extends
Sprite{
//default radius = 30
private
var
radius:
int
=
30
;
public
function
BidikanPoint(radius:
int
){
this
.radius = radius;
//gambar bulatan besar dan bulatan kecil
graphics.lineStyle(
5
,
0x000000
);
graphics.drawCircle(
0
,
0
,radius/
3
);
graphics.drawCircle(
0
,
0
, radius);
//gambar garis vertikal
graphics.moveTo(
0
, (radius/
3
));
graphics.lineTo(
0
, radius+
10
);
graphics.moveTo(
0
, -(radius/
3
));
graphics.lineTo(
0
, -(radius+
10
));
//gambar garis horizontal
graphics.moveTo((radius/
3
),
0
);
graphics.lineTo(radius+
10
,
0
);
graphics.moveTo(-(radius/
3
),
0
);
graphics.lineTo(-(radius+
10
),
0
);
}
}
}
Code yang digunakan untuk membuat button pada game
package
{
import
flash.display.Sprite;
import
flash.display.SimpleButton;
import
flash.display.Shape;
import
flash.text.TextField;
import
flash.text.TextFieldAutoSize;
import
flash.text.TextFormat;
public
class
Button
extends
Sprite{
private
var
simpleButton:SimpleButton =
new
SimpleButton();
private
var
textField:TextField =
new
TextField();
private
var
textFormat:TextFormat =
new
TextFormat();
private
var
downState:Shape =
new
Shape();
private
var
overState:Shape =
new
Shape();
private
var
upState:Shape =
new
Shape();
private
var
hitTestState:Shape =
new
Shape();
private
var
lebar:
int
;
private
var
tinggi:
int
;
private
var
color:
uint
;
public
function
Button(
label
:
String
,color:
uint
){
//format textField
textFormat.bold =
true
;
textFormat.size =
20
;
//label
textField.defaultTextFormat=textFormat;
textField.text =
label
;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.selectable =
false
;
textField.mouseEnabled =
false
;
//init panjang dan lebar
lebar = textField.width +
20
;
//10 pixel kanan & 10 pixel kiri
tinggi = textField.height +
20
;
//initColor
this
.color = color;
//init graphics
drawDownState();
drawOverState();
drawUpState();
drawHitTestState();
//button
simpleButton.downState = downState;
simpleButton.overState = overState;
simpleButton.upState = upState;
simpleButton.hitTestState = hitTestState;
addChild(simpleButton);
//sesuaikan posisi x,y textField agar berada di tengah
textField.x = -lebar/
2
+(lebar-textField.width)/
2
;
textField.y = -tinggi/
2
+(tinggi-textField.height)/
2
;
addChild(textField);
}
private
function
drawDownState(){
downState.graphics.lineStyle(
5
,
0x000000
);
downState.graphics.beginFill(color,
1
);
downState.graphics.drawRoundRect(-lebar/
2
,-tinggi/
2
,lebar,tinggi,
5
,
5
);
downState.graphics.endFill();
}
private
function
drawOverState(){
overState.graphics.lineStyle(
5
,
0x000000
);
overState.graphics.beginFill(
0xFFFFFF
,
0.3
);
overState.graphics.drawRoundRect(-lebar/
2
,-tinggi/
2
,lebar,tinggi,
5
,
5
);
overState.graphics.endFill();
}
private
function
drawUpState(){
upState.graphics.lineStyle(
5
,
0x000000
);
upState.graphics.beginFill(color,
1
);
upState.graphics.drawRoundRect(-lebar/
2
,-tinggi/
2
,lebar,tinggi,
5
,
5
);
upState.graphics.endFill();
}
private
function
drawHitTestState(){
hitTestState.graphics.lineStyle(
5
,
0x000000
);
hitTestState.graphics.beginFill(color,
1
);
hitTestState.graphics.drawRoundRect(-lebar/
2
,-tinggi/
2
,lebar,tinggi,
5
,
5
);
hitTestState.graphics.endFill();
}
}
}
Code untuk program utama pada game
package
{
import
flash.display.Sprite;
import
flash.display.MovieClip;
import
flash.events.MouseEvent;
import
flash.events.Event;
public
class
Main
extends
Sprite{
private
var
tambahMusuhButton:Button =
new
Button("Tambah Objek",
0x00FF00
);
private
var
arrayMusuh:
Array
=
new
Array
();
private
var
indexArray:
int
=
0
;
private
var
bidikanPoint:BidikanPoint =
new
BidikanPoint(
40
);
public
function
Main(){
//set posisi button diposisi 10 pixel dari sisi kiri dan 10 pixel dari sisi bawah
tambahMusuhButton.x =
10
+(tambahMusuhButton.width/
2
);
tambahMusuhButton.y = (stage.stageHeight-
10
)-(tambahMusuhButton.height/
2
);
tambahMusuhButton.addEventListener(MouseEvent.CLICK, tambahMusuhHandler);
addChild(tambahMusuhButton);
}
private
function
tambahMusuhHandler(event:MouseEvent){
var
radiusMusuh:
int
=
50
;
arrayMusuh.push(
new
Musuh(radiusMusuh));
//mengatur agar posisi objeck random di dalam stage
arrayMusuh[indexArray].x = radiusMusuh+Math.random()*(stage.stageWidth-(
2
*radiusMusuh));
arrayMusuh[indexArray].y = radiusMusuh+Math.random()*(stage.stageHeight-(
2
*radiusMusuh));
arrayMusuh[indexArray].addEventListener(MouseEvent.MOUSE_OVER, tampilBidikanPoint);
arrayMusuh[indexArray].addEventListener(MouseEvent.MOUSE_OUT, hapusBidikanPoint);
arrayMusuh[indexArray].addEventListener(MouseEvent.CLICK, hapusMusuh);
arrayMusuh[indexArray].id = indexArray;
addChild(arrayMusuh[indexArray]);
indexArray++;
}
private
function
hapusMusuh(event:MouseEvent){
removeChild(arrayMusuh[event.target.id]);
}
private
function
tampilBidikanPoint(event:MouseEvent){
bidikanPoint.x = mouseX;
bidikanPoint.y = mouseY;
addChild(bidikanPoint);
addEventListener(Event.ENTER_FRAME, gantiPosisiBidikanPoint);
}
private
function
gantiPosisiBidikanPoint(event:Event){
//berfungsi menghaluskan perpindahan Bidikan Point
bidikanPoint.x = mouseX;
bidikanPoint.y = mouseY;
}
private
function
hapusBidikanPoint(event:MouseEvent){
removeChild(bidikanPoint);
removeEventListener(Event.ENTER_FRAME, gantiPosisiBidikanPoint);
}
}
}
Source:
http://blog.hobbycoding.web.id/2011/03/18/game-tembak-tembakan-sederhana/