Comunidad de diseño web y desarrollo en internet online

Juego memorama problema al randomizar

Citar            
MensajeEscrito el 04 Jun 2009 06:59 pm
hola que tal tengo un problema ya he intentado muchas veces hacer un aleatorio para las cartas y nada mas no me sale tengo este codigo cortesia de un usuario de estos foros :

Código ActionScript :

var intervalo:Number;
var imagenes_arr:Array = new Array();
var arrayPosicion:Array = new Array();
imagenes_arr.push("uno_mc");
imagenes_arr.push("dos_mc");
imagenes_arr.push("tres_mc");
imagenes_arr.push("cuatro_mc");
imagenes_arr.push("cinco_mc");
//Creamos las imagenes del memorama 
crear(imagenes_arr,"tapa_mc");
//Creamos los duplicado de las imagenes del memorama 
crear(imagenes_arr,"tapa_mc");
//Ordenamos todas las imagenes de forma aleatoria 
ordenar();

function crear(img_arr:Array, tapa_str:String) {
   var aux:MovieClip;
   var nom:String;
   for (var i in img_arr) {
      nom = (this["mem"+i] == undefined) ? "mem"+i : "cop"+i;
      aux = this.createEmptyMovieClip(nom, this.getNextHighestDepth());
      aux.e = true;//Estado tapado 
      aux.t = tapa_str;//id de biblioteca de la tapa 
      aux.f = img_arr[i];//id de biblioteca de la imagen 
      aux.i = i;
      aux.attachMovie(aux.t,"art",0);
      aux.onRelease = function(Void):Void  {
         this.cambio();
         this._parent.verificar(this);
      };
      aux.cambio = function(Void):Void  {
         this.e = !this.e;
         this.attachMovie(this.e ? this.t : this.f,"art",0);
      };
   }
}

function verificar(aux:MovieClip):Void {
   for (var i in this) {
      if (this[i] != aux && this[i].onRelease != undefined) {
         if (aux.i == this[i].i && !this[i].e) {
            delete aux.onRelease;
            delete this[i].onRelease;
            completo();
            break;
         } else if (!this[i].e) {
            for (var j in this) {
               this[j].enabled = false;
            }
            intervalo = setInterval(this, "fallo", 500, this[i], aux);
            break;
         }
      }
   }
}

function ordenar(Void):Void {
   for (var i in this) {
      this[i]._x = (this[i]._width+10)*this[i].i;
      this[i]._y = (i.substr(0, 3) == "cop") ? this[i]._width+10 : 0;
   }
   
}

function fallo(aux1:MovieClip, aux2:MovieClip) {
   clearInterval(intervalo);
   for (var i in this) {
      this[i].enabled = true;
   }
   aux1.cambio();
   aux2.cambio();
}

function completo(Void):Void {
   for (var i in this) {
      if (this[i].onRelease) {
         return;
      }
      //Aqui incia el codigo de lo que pasa si ganaste   
   }
   trace("GANASTE");
}
//
function arreglos(arr) {
   trace(arr);
}
function randomizarArray(arreglo) {
   var m = arreglo.length-1;
   for (i=m; i>1; i--) {
      alea = Math.floor(i*Math.random());
      temp = arreglo[i];
      arreglo[i] = arreglo[alea];
      arreglo[alea] = temp;
   }
}
function ranRango(min:Number, max:Number):Number {
   var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
   return randomNum;
}

Mis dos problemas son: el random de las cartas y dos ahi tengo 5 mc atacchados pero estos deben ser 16 y estos se siguen derecho en dos filas como puedo ordenarlas de 4 en 4

Saludos

Por joshuavw

Claber

306 de clabLevel



Genero:Masculino  

Juego, aprendo y programo

firefox
Citar            
MensajeEscrito el 03 Mar 2010 08:16 am
hola, estuve modificado el codigo y asi quedo..no olvides crear tus identificadores en laspropiedades. :evil:

Código ActionScript :

stop();
//---Prototype de indexOf  
Array.prototype.indexOf = function(search):Number  { 
 
   var total:Number = this.length; 
   var index:Number = -1; 
   var item:Number = 0; 
   var finded:Boolean = false; 
 
   while (item<total && !finded) { 
 
      if (this[item] == search) { 
         index = item; 
         finded = true; 
      } else { 
         item++; 
      } 
   } 
   return index; 
 
}; 
 
//---Función de desordenar un arreglo 
function randomArray(arreglo:Array):Array { 
   //---Declaramos las variables locales 
   var returnArray:Array = new Array(); 
   var total:Number = arreglo.length; 
 
   var rand:Number; 
   var item:Number; 
   var sum:Number = 0; 
 
   while (sum<total) { 
 
      //---Creamos un número aleatorio entre el rango de la cantidad de imagenes 
      rand = Math.floor(Math.random() * total); 
      //---Buscamos el número correspondiente a este número en el arreglo 
      item = arreglo[rand]; 
      //---Comprobamos si el número no se encuentra en el arreglo y de ser así lo añadimos 
      if (returnArray.indexOf(item)<0) { 
         returnArray.push(item); 
         sum++; 
      } 
   } 
   return returnArray; 
} 


var arreglo1:Array = new Array(0, 1, 2, 3);
//trace(arreglo1);
var arreglo2:Array = new Array(0, 1, 2, 3);
//trace(arreglo2);
var desarreglo1:Array = randomArray(arreglo1);
//trace(desarreglo1);
var desarreglo2:Array = randomArray(arreglo2);
//trace(desarreglo2);
var completo:Array = desarreglo1.concat(desarreglo2);
trace(completo);

var num:Number = 8;
var dirX:Number = 0;
var dirY:Number = 0;
var col:Number = 4;
var total:Number = 0;
for (i=0; i<num; i++) {


   aux = this.createEmptyMovieClip("carta"+i, this.getNextHighestDepth());
   aux.e = true;//Estado tapado  
   aux.t = "tapa_mc";//id de biblioteca de la tapa  
   aux.f = "card"+completo[i];//id de biblioteca de la imagen  
   aux.i = i;
   _root["carta"+i]._x = 150*dirX;
   _root["carta"+i]._y = (170*dirY)+50;
   aux.attachMovie(aux.t,"mc",0);
   //trace("valori="+i);
   dirX++;

   if (dirX == col) {
      dirX = 0;
      dirY++;

   }

   aux.onRelease = function(Void):Void  {
      this.cambio();
      trace(this.f);
      this._parent.verificar(this);
   };
   aux.cambio = function(Void):Void  {
      this.e = !this.e;
      this.attachMovie(this.e ? this.t : this.f,"mc",0);
   };



}

function verificar(aux:MovieClip):Void { 
   for (var i in this) { 
   //busca otro que este abietro diferente del que acabo de selecionar
      if (this[i] != aux && this[i].onRelease != undefined) { 
         if (aux.f == this[i].f && !this[i].e) { 
            delete aux.onRelease; 
            delete this[i].onRelease;
         total++;
         trace(total);
            //_parent.completo(); 
            break; 
         } else if (!this[i].e) { 
            for (var j in this) { 
               this[j].enabled = false; 
            } 
            intervalo = setInterval(this, "fallo", 500, this[i], aux); 
            break; 
         } 
      } 
   } 
} 
 
 
function fallo(aux1:MovieClip, aux2:MovieClip) { 
   clearInterval(intervalo); 
   for (var i in this) { 
      this[i].enabled = true; 
   } 
   aux1.cambio(); 
   aux2.cambio(); 
} 

_root.onEnterFrame = function(){
   if(total == 4){
      trace("ganaste");
       delete this.onEnterFrame;  
      
      }


}

Por uriel2707

2 de clabLevel



 

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.