Copy link to clipboard
Copied
Hi.My name is Jeong-Hyeon-Seong.I'm planning to make a simple p2p game(Korean people call as JUMP-JUMP).I was success at making multi-p2p chat application by course.But one important thing has remained.
this is my source code that powered by course,which is made by Leoz(레 의 잡다한 밤하늘 : 네이버 블로그)
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.GroupSpecifier;
import flash.net.NetStream;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.display.MovieClip;
import flashx.textLayout.utils.CharacterUtil;
stop()
nam.text = irm;
var _netconnection:NetConnection;
var _groupname:String;
var _netgroup:NetGroup;
var _groupspecifier:GroupSpecifier;
var _sendstream:NetStream;
var _connected:Boolean = false;
var _recvstreamarray:Array = [];
var _recvstreamfaridarray:Array = [];
var _connectingurl:String;
var i:int;
var _userarray:Array = [];
var objo:Object = {}
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
const CONNECT_URL:String = 'rtmfp://p2p.rtmfp.net/xxx/';
dcon.addEventListener(MouseEvent.CLICK,disconnecting_f);
snd.addEventListener(MouseEvent.CLICK,sending_f);
con.addEventListener(MouseEvent.CLICK,startc_f);
function sending_f(a:MouseEvent)
{
_sendstream.send('recvprot_msg',nam.text+':'+msg.text);
//리시브 함수 1번;
msgsplus_f(nam.text+':'+msg.text);
}
function startset(ncurl:String, stname:String)
{
_groupname = stname;
_groupspecifier = new GroupSpecifier(_groupname);
_groupspecifier.serverChannelEnabled = true;
_netconnection = new NetConnection ;
_netconnection.addEventListener(NetStatusEvent.NET_STATUS,netstatus_f);
_connectingurl = ncurl;
_netconnection.connect(_connectingurl);
msgsplus_f('채팅방'+'Name- '+stname);
}
function startc_f(e:MouseEvent)
{
msgsplus_f('연결을 시도합니다.');
startset(CONNECT_URL,'ase');
con.enabled = false;
_userarray.push(nam.text);
ulists.appendText(_userarray[0]);
}
function msgsplus_f(string:String)
{
msgs.appendText(String(string)+'\n');
}
function netstatus_f(e:NetStatusEvent):void
{
if (e.info.level == 'error')
{
msgsplus_f(e.info.code+'\n 의 에러가 발생합니다.');
}
else if (e.info.code =='NetConnection.Connect.Success')
{
_netgroup = new NetGroup(_netconnection,_groupspecifier.groupspecWithAuthorizations());
_sendstream = new NetStream(_netconnection,NetStream.DIRECT_CONNECTIONS);
msgsplus_f(_netconnection.nearID);
var obj = new Object();
obj.onPeerConnect = function(e):Boolean
{
pushrecvstream_f(e.farID);
return true;
};
_sendstream.client = obj;
_sendstream.publish(_groupname);
}
else if (e.info.code == 'NetGroup.Connect.Success')
{
_netgroup.addEventListener(NetStatusEvent.NET_STATUS,netstatus_f);
}
else if (e.info.code == 'NetGroup.Neighbor.Connect')
{
pushrecvstream_f(e.info.peerID);
_sendstream.send('recvprot_nick',nam.text);
//채팅관련함수
}
else if (e.info.code == 'NetGroup.Neighbor.Disconnect')
{
}
else if (e.info.code == 'NetStream.Connect.Success')
{
msgsplus_f('Clear');
_sendstream.send('recvprot_nick',nam.text);
//리시브 함수 2번;
}
else if (e.info.code == 'NetStream.Play.Start')
{
_connected = true;
_sendstream.send('recvprot_nick',nam.text);
//리시브 함수 2번;
}
};
function pushrecvstream_f(newpeerid:String):void
{
//수신관련 함수
for (i=0; i<_recvstreamarray.length; i += 1)
{
if (_recvstreamarray.farID == newpeerid)
{
return;
}
}
var _newrecvstream:NetStream = new NetStream(_netconnection,newpeerid);
_newrecvstream.addEventListener(NetStatusEvent.NET_STATUS,netstatus_f);
var obj = new Object();
obj.recvprot_msg = function(e)
{
msgsplus_f(e)
};
//리시브 함수 1번
obj.recvprot_nick = function(e)
{
for(i=0;i<_userarray.length;i+=1){
if(_userarray == e)
{
return;
}
}
//리시브 함수 2번
_userarray.push(e as String);
msgsplus_f('추가');
ulists.appendText(_userarray[_userarray.length-1]+'\n');
_sendstream.send('recvprot_nick',nam.text)
};
obj.recvprot_delete = function(e)
{
for(i=0;i<_userarray.length;i+=1)
{
if(e as String == _userarray)
{
msgsplus_f(_userarray+'사용자님이 접속을 종료하셨습니다.');
_userarray.splice(i,1)
break;
}
}
ulists.text = _userarray[0];
for(i=1;i<_userarray.length;i+=1)
{
ulists.appendText(_userarray+'\n');
}
};
_newrecvstream.client = obj;
_newrecvstream.play(_groupname);
_recvstreamarray.push(_newrecvstream);
}
function disconnecting_f(e:MouseEvent)
{
_connected = false;
_sendstream.send('recvprot_delete',nam.text);
_netgroup.close();
_recvstreamarray = [];
_recvstreamfaridarray = [];
_userarray = [];
_sendstream.close();
_netconnection.close();
ulists.text = '';
con.enabled = true;
msgsplus_f('종료');
rot.gotoAndStop(1);
}
In this situation,I would like to add the character's meeting and moving.But I didn't have the idea about this.Because my applying power is about zero.
Please tell me the source code about character's meeting and moving.When that done,I really appreciated.And My Dream in Winter vacation was fully achieved.I'm going to plan a game with relatives.
so Lot of help will be appriciated.Thank you.
i'm not sure this will help you but it's an excerpt from a book i wrote (Flash Game Development: In a Social, Mobile and 3D World)
this is the class that establishes peer-to-peer connection and the communicates between two peers.
P2P.as
package com.kglad{
import flash.display.MovieClip
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.GroupSpecifier;
import flash.net.NetGroupReplicationStrategy;
public
...Copy link to clipboard
Copied
i'm not sure this will help you but it's an excerpt from a book i wrote (Flash Game Development: In a Social, Mobile and 3D World)
this is the class that establishes peer-to-peer connection and the communicates between two peers.
P2P.as
package com.kglad{
import flash.display.MovieClip
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.GroupSpecifier;
import flash.net.NetGroupReplicationStrategy;
public class P2P {
private static var nc:NetConnection;
private static var netGroup:NetGroup;
private static var main:MovieClip;
internal static function init(_main:MovieClip):void {
main = _main;
// Create a NetConnection instance
nc = new NetConnection();
// Add event listener to handle connections and data to and from
// this peer.
nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusF);
// Use your server address and key (from Adobe,
// http://www.adobe.com/cfusion/entitlement/index.cfm?e=cirrus)
nc.connect(Data.SERVER+Data.DEVKEY);
}
private static function netStatusF(e:NetStatusEvent):void {
CONFIG::DEBUG {
main.textF("p2p netStatusF begin:",main.message_tf);
main.stringifyF(e.info);
main.textF("p2p netStatusF end:",main.message_tf);
}
switch (e.info.code) {
case "NetConnection.Connect.Success":
// When this user connects, initialize group
initializeGroupF();
break;
case "NetGroup.Neighbor.Connect":
// only ~14 closest peers will be neighbors
Data.addToGroup(e.info.neighbor);
CONFIG::DEBUG{
main.textF("*** addToGroup: ***"+e.info.neighbor,main.message_tf);
}
// Notify user they need to enter a name.
if(main.message_tf.text.indexOf("To start, enter your name and click SEND")==-1 && Data.nameS.length<1){
main.textF("To start, enter your name and click SEND",main.message_tf);
main.sendName_btn.visible = true;
}
break;
case "NetGroup.Neighbor.Disconnect":
// A peer connection was lost. Remove from
// Data.groupAddressA and check if peer was this
// user's opponent.
Data.removeFromGroup(e.info.neighbor);
main.checkGroupLostF(e.info.neighbor);
break;
case "NetGroup.SendTo.Notify":
CONFIG::DEBUG{
main.textF("*** NOTIFY *** "+Data.groupAddressA.length,main.message_tf);
}
// Process received data in Main.
main.receiveDataF(e.info);
// Allowing netGroup.sendToNearest() to execute
// crashes the debug flash player. I could not find a
// problem commenting out this section but I only
// tested with up to 40 peers.
/*
if(e.info.fromLocal){
main.receiveDataF(e.info);
} else {
netGroup.sendToNearest(e.info.message, e.info.message.destination);
}
*/
break;
}
}
private static function initializeGroupF():void {
// Define a GroupSpecifier. Presumably using this should allow
// each developer (with their one key) to develop many different
// games and applications.
// This is the tic-tac-toe group.
var groupSpecifier:GroupSpecifier=new GroupSpecifier("tttGroup");
// Enable posting to entire group.
groupSpecifier.postingEnabled = true;
// Enable direct routing a peer.
groupSpecifier.routingEnabled = true;
// Allow peers to join the group automatically.
groupSpecifier.serverChannelEnabled = true;
// Create NetGroup instance.
netGroup=new NetGroup(nc,groupSpecifier.groupspecWithAuthorizations());
netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatusF);
}
internal static function sendDataF(obj:Object):void {
if(Data.connectionAddressF.length>0){
// communicate only with the peer at
// Data.connectionAddressF
obj.destination = Data.connectionAddressF;
netGroup.sendToNearest(obj, obj.destination);
} else {
netGroup.sendToAllNeighbors(obj);
}
}
internal static function sendDirectedDataF(obj:Object):void {
// Send message to only one peer.
netGroup.sendToNearest(obj, obj.destination);
}
// I don't need this function but it shows how to send a message to
// everyone in the group.
internal static function sendDataToAllF(obj:Object):void{
netGroup.sendToAllNeighbors(obj);
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now