ThinkGeek - Cool Stuff for Geeks and Technophiles

December 10, 2003

A little game in Flashcom

It's true, that the most amazing feature of Flashcom is his poweful video and audio streaming, but , i think that the real power of flashcom is in sharedobjects. Here you are a really simple app to show the power of SO and syncro between conected users.

It is a simple "3 on line". Of course, it can be really improved, but it is just a test

//Initial values··········································
//I use 6 mc's, they are circles with two frames, one frame is a red circle 
//("rojo") and the other frame is a black circle ( "negro" ) 
circulo1={mc:circulo1_mc, x:40, y:235, color:"rojo" };//first circle
circulo2={mc:circulo2_mc, x:20, y:194, color:"rojo" };//second circle
circulo3={mc:circulo3_mc, x:40, y:152, color:"rojo" };
circulo4={mc:circulo4_mc, x:20, y:110, color:"negro"};
circulo5={mc:circulo5_mc, x:40, y:69, color: "negro" };
circulo6={mc:circulo6_mc, x:20, y:27, color: "negro" };

//array
var circulos=[circulo1,circulo2,circulo3,circulo4,circulo5,circulo6];

//Zone flashcom·········································
function initSO(nc){
 	//We build a non persistent SO
 	my_so=SharedObject.getRemote("tresraya_so",nc.uri,false);
 	my_so.onSync=function(lista){
  		for ( var prop in my_so.data){
   			debug("propiedad:valor -
   > "+prop+" : "+my_so.data[prop]);
   		}
  		var indexe=my_so.data.props[0];
  		var xPos=my_so.data.props[1];
  		var yPos=my_so.data.props[2];
  		circulos[indexe].mc._x=xPos;
  		circulos[indexe].mc._y=yPos;
  	}
 	my_so.connect(nc);
}

//Conexión al servidor Flash Communication Server
nc=new NetConnection();
nc.onStatus=function(info){
 	if(info=="NetConnection.Connect.Success");{
  		debug("estoy conectado");
  		initSO(this);
  	}
}
nc.connect("rtmp:/tresenraya");

//functins·····························3
//trace
function debug(texto){
 	trace(texto);	
}


/*
*Metodo:	estadoIncial
*Desc:    establish the initial values and colors 	
*	
*params:	No params.
*/
estadoInicial=function(){
 	for (var i=0;i<circulos.length;i++){
  		circulos[i].mc._x=circulos[i].x;
  		circulos[i].mc._y=circulos[i].y;
  		if(circulos[i].color=="negro"){
   			circulos[i].mc.gotoAndStop(2);	
   		}//if
  	}//for
}//estadoInicial

/*
*Metodo:	press
*Desc: 	event when press a circle
*	
*params:	no params
*/

MovieClip.prototype.press=function(){
 	var indexe=this.i;
 	debug(indexe);//tracear la posición del array sobre la que hemos 
 pinchado
 	this.onMouseMove=function(){
  		circulos[indexe].mc._x=_xmouse;
  		circulos[indexe].mc._y=_ymouse;
  		var propos=[indexe,circulos[indexe].mc._x,circulos
  [indexe].mc._y]
  		my_so.data.props=propos;
  	}
}

/*
*Metodo:	release
*Desc: 	event on release
*	
*params:	No params
*/
MovieClip.prototype.release=function(){
 	delete this.onMouseMove;
}

/*
*Metodo:	allFunctions
*Desc: 	onPress onRelease
*	
*params:	No params
*/
MovieClip.prototype.allFunctions=function(){
 	this.onPress=press;
 	this.onRelease=release;
}

//
for (var i=0;i<circulos.length;i++){
 	circulos[i].mc.i=i;
 	circulos[i].mc.allFunctions();
}

//start the app
estadoInicial();

A simple code, but it works and it can be used to show the power of So. You can improve it, i.e. makin the SO persistent on the Server, so you can "save match".

Posted by Javier Tardáguila Date: December 10, 2003 11:43 AM
Comments