Membuat Game Ping Pong Sederhana P2P Dengan Adobe Flash Builder
Game kali ini yaitu game Ping Pong multiplayer berbasis jaringan LAN Peer to peer(P2P). Untuk membuatnya saya menggunakan aplikasi Adobe Flash Builder 4.6, untuk download Flash Builder bisa masuk ke website Adobe.
1. Buat project baru(File>New>Flex Project).
2. Paste code dibawah ini.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="294" height="424" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flashx.textLayout.factory.TruncationOptions;
import org.osmf.events.TimeEvent;
private var nc:NetConnection;
private var group:NetGroup;
[Bindable]
private var speedx:int = 3;
private var speedy:int = 3;
private var nyawa1:int = 10;
private var nyawa2:int = 10;
private var timer:Timer = new Timer(25);
private var obj:Object = new Object();
public var host:Boolean = false;
private function connect():void{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
}
private function netStatus(event:NetStatusEvent):void{
trace("[EVENT]\n"+event.info.code+"\n");
switch(event.info.code){
case "NetConnection.Connect.Success":
setupGroup();
break;
case "NetGroup.Connect.Success":
//connected = true;
break;
case "NetGroup.Posting.Notify":
//mc.x = event.info.message.x;
//mc.y = event.info.message.y;
break;
case "NetGroup.Neighbor.Connect":
ballpos();
break;
case "NetGroup.Neighbor.Disconnect":
break;
// routed message received
case "NetGroup.SendTo.Notify":
if(!host){
ball.x = event.info.message.bx;
ball.y = event.info.message.by;
}
mc2.x = event.info.message.x;
//mc2.y = event.info.message.y;
live1.text = event.info.message.nyawa2.toString();
live2.text = event.info.message.nyawa1.toString();
winLabel.text = event.info.message.wintxt;
goPanel.visible = event.info.message.panel;
break;
}
}
private function setupGroup():void{
var groupspec:GroupSpecifier = new GroupSpecifier("myGroup");
groupspec.postingEnabled = true;
groupspec.routingEnabled = true;
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");
group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
group.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}
// enable dragging
private function drag(e:MouseEvent):void {
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMe);
} // end drag function
// Drop the object.
private function drop(e:MouseEvent):void {
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveMe);
} // end drop function
// Move the object in accordind of the mouse position
private function moveMe(e:MouseEvent):void {
// move movieclip to mouse position
mc.x = this.mouseX - mc.width/2;
//mc.y = this.mouseY - mc.height/2;
// Save the current movieclip position
obj.x = ((mc.x+mc.width)-(294))*-1;
//obj.y = ((mc.y+mc.height)-(424))*-1;
// Set sender id
obj.sender = group.convertPeerIDToGroupAddress(nc.nearID);
// Sends a message to all members of a group.
//group.post(obj);
group.sendToAllNeighbors(obj);
} // end moveMe function
//start game
private function ballpos():void{
ball.x = 63 + Math.random() * 150;
ball.y = 212;
timer.stop();
timer.addEventListener(TimerEvent.TIMER,moveBall);
timer.start();
}
private function moveBall(e:TimerEvent):void{
if(host){
obj.main=false;
//===ball speed
ball.x +=speedx;
ball.y +=speedy;
//=========collision stage=============
if(ball.y < 28){
speedy = -3;
//speedx = 3;
ball.y = 212;
ball.x = 63 + Math.random() * 150;
nyawa2 -= 1;
live2.text = nyawa2.toString();
}else if(ball.y > 388){
speedy = 3;
//speedx = 3;
ball.y = 212;
ball.x = 63 + Math.random() * 150;
nyawa1 -= 1;
live1.text = nyawa1.toString();
}
if (ball.x < 0){
ball.x = 0;
speedx *= -1;
}else if (ball.x > 293){
ball.x = 293 - ball.width;
speedx *= -1;
}
//==========collision paddle=========
if(mc.hitTestObject(ball)){
if(ball.x>((mc.width/2)+4)){
speedx -=2;
}else if(ball.x<((mc.width/2)-4)){
speedx +=2;
}
if(speedy<10){
speedy +=1;
speedx +=1;
}
speedy *= -1;
}else if(mc2.hitTestObject(ball)){
if(ball.x>((mc2.width/2)+4)){
speedx -=2;
}else if(ball.x<((mc2.width/2)-4)){
speedx +=2;
}
if(speedy>-10){
speedy -=1;
}
speedy *= -1;
}
//===score ball======
if(nyawa1 == 0){
timer.stop();
goPanel.visible=true;
winLabel.text="You Lose";
obj.wintxt="You Win";
obj.panel = true;
}else if(nyawa2 == 0){
timer.stop();
goPanel.visible=true;
winLabel.text="You Win";
obj.wintxt="You Lose";
obj.panel = true;
}
//====save the value====
obj.nyawa1= nyawa1;
obj.nyawa2= nyawa2;
obj.bx = ((ball.x+ball.width)-(294))*-1;
obj.by = ((ball.y+ball.height)-(424))*-1;
// Set sender id
obj.sender = group.convertPeerIDToGroupAddress(nc.nearID);
group.sendToAllNeighbors(obj);
}
}
protected function join_clickHandler():void{
connect();
host = false;
mc.visible = true;
mc2.visible = true;
mulai.visible = false;
join.visible = false;
live2.rotation = 180;
live1.visible = true;
live2.visible = true;
}
protected function mulai_clickHandler():void{
connect();
host=true;
mc.visible = true;
mc2.visible = true;
mulai.visible = false;
join.visible = false;
live2.rotation = 180;
live1.visible = true;
live2.visible = true;
obj.x=112;
group.post(obj);
}
]]>
</fx:Script>
<mx:HRule x="-23" y="212" width="341" height="3"/>
<s:Ellipse x="95" y="161" width="104" height="104" id="tes">
<s:stroke>
<s:SolidColorStroke weight="2" color="#DDDDDD"/>
</s:stroke>
</s:Ellipse>
<s:Rect width="293" height="423">
<s:stroke>
<s:SolidColorStroke color="#DDDDDD" weight="4"/>
</s:stroke>
</s:Rect>
<s:Button id="ball" x="140" y="205" width="15" height="15" label="Ball" chromeColor="#FCFCFC"/>
<s:Label id="live1" x="269" y="223" fontWeight="bold" text="10" visible="false"/>
<s:Label id="live2" x="25" y="203" fontWeight="bold" text="10" visible="false"/>
<s:Button id="mc" x="112" label="▬▬▬▬▬▬▬" fontFamily="Courier New" mouseDown="drag(event)"
mouseUp="drop(event)" verticalCenter="190"/>
<s:Button id="mc2" x="112" label="▬▬▬▬▬▬▬" fontFamily="Courier New" verticalCenter="-192"/>
<s:Button id="mulai" x="112" y="187" label="Host" click="mulai_clickHandler()"
enabled="true" fontWeight="bold"/>
<s:Button id="join" x="112" y="218" label="Join" click="join_clickHandler()"
fontWeight="bold"/>
<s:Panel id="goPanel" x="30" y="140" width="235" height="144" title="Game Over" visible="false">
<s:Label id="winLabel" x="10" y="35" width="213" height="34" fontSize="32"
text="Player X Win" textAlign="center" verticalAlign="middle"/>
</s:Panel>
</s:Application>
Selamat mencoba. Semoga dapat membantu, dan menjadi inspirasi.
Subscribe to:
Post Comments (Atom)
widihhh mantap bgt dachhh bisa di coba nichhh
ReplyDeletethank's ya
This comment has been removed by the author.
ReplyDelete