A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Help swapping tiles from board

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2

    Unhappy Help swapping tiles from board

    I'm new to AS3 and I've been searching info during some days but can't solve this problem. I create a board using the class Baldosa (which is a tile on my .fla file with 9frames, each is a tile of a different color). I first create and array of numbers which indicates which frame (color) the baldosa element will display. As grey baldosas are more than the other colors (this is intended), then I unsort the array to avoid grey baldosas accumulating on the right side of the board. Then I push each baldosa in an array, and add each of them as child of the contenedorBaldosas(container) to avoid adding events for each Baldosa. The problem comes when I want to check collisions. If there's a collision, I swap positions of each Baldosa. It only works if I swap the tile at depth number 0 and the last at depth 79, for the rest of Baldosas, it doesn't detect any collision (the baldosa clicked will return to its original position as nothing had happened. I made a trace and it seems the baldosa clicked doesnt collide with another baldosa in the array (each baldosa is given the name baldosaxx using the variable in the for loop), but rather an unnamed instancexxxx, so the problem might be this, but don't know how to fix the problem. Also I'm treating the baldosa elements as Sprites, but as the baldosa in my .fla is a MovieClip, and it contains a Timeline where each frame is a baldosa of a different color, the problem may be also here.

    package {

    import flash.display.*;
    import flash.geom.*;
    import flash.events.*;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.events.MouseEvent;




    public class MonsterSliders extends MovieClip {

    private static const anchoTablero:uint = 10;
    private static const altoTablero:uint = 8;
    private static const espaciadoHorizontalBaldosa:Number = 46;
    private static const espaciadoVerticalBaldosa:Number = 46;
    private static const desplazamientoTableroX:Number = 50;
    private static const desplazamientoTableroY:Number = 20;

    public function MonsterSliders():void {

    var listaDesordenadaBaldosasTablero:Array = new Array(); // esta lista sirve para escoger el color, no es el array final
    var listaOrdenadaBaldosasTablero:Array = new Array(); // array final, solo contiene el color de cada baldosa
    var contenedorBaldosas:Sprite = new Sprite(); // las baldosas se mostrarán en pantalla dentro
    //de este contenedor para asignar eventos de forma global
    var arrayBaldosas:Array = new Array(); //aquí se guardan las instancias ordenadas de las baldosas
    //var objetivo:Sprite = new Sprite; //variable para comprobar colisión de baldosas
    var nombreBaldosaRaton:String = new String; //nueva instancia de la baldosa que se arrastra con el ratón


    //en estas variables guardamos cuántas veces se repite el color en el tablero.
    var negro:uint = 0, azul: uint = 0, rojo:uint = 0, violeta:uint = 0,
    naranja:uint = 0, amarillo:uint = 0, verde: uint = 0, blanco: uint = 0;


    for(var x:uint=0;x<anchoTablero;x++) { // horizontal
    for(var y:uint=0;y<altoTablero;y++) { // vertical

    var colorBaldosa:uint = Math.floor(Math.random()*9+1); // se elige color aleatorio.



    switch(colorBaldosa){
    case 2:
    if(negro < 4) {
    negro++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 3:
    if(azul < 4) {
    azul++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 4:
    if(rojo < 4) {
    rojo++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 5:
    if(violeta < 4) {
    violeta++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 6:
    if(naranja < 4) {
    naranja++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 7:
    if(amarillo < 4) {
    amarillo++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 8:
    if(verde < 4) {
    verde++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    case 9:
    if(blanco < 4) {
    blanco++;
    listaDesordenadaBaldosasTablero.push(colorBaldosa) ;
    }
    else {
    listaDesordenadaBaldosasTablero.push(1);
    }
    break;
    default:
    listaDesordenadaBaldosasTablero.push(1);

    }



    }
    }


    this.addChild(contenedorBaldosas);

    contenedorBaldosas.x = 0; //el contenedor es invisible, pero debe situarse en pantalla
    contenedorBaldosas.y = 0; //para añadir las baldosas dentro.



    for(var k:uint=0;k<anchoTablero;k++) { // horizontal
    for(var g:uint=0;g<altoTablero;g++) { // vertical
    var baldosa:Baldosa = new Baldosa(); // copy the movie clip

    baldosa.stop(); // stop on first frame
    baldosa.x = k*espaciadoHorizontalBaldosa+desplazamientoTablero X; // set position
    baldosa.y = g*espaciadoVerticalBaldosa+desplazamientoTableroY;
    var num:uint = Math.floor(Math.random()*listaDesordenadaBaldosasT ablero.length);
    var numBaldosa:uint = listaDesordenadaBaldosasTablero[num];
    baldosa.gotoAndStop(numBaldosa);
    arrayBaldosas.push(baldosa);
    listaOrdenadaBaldosasTablero.push(numBaldosa); //guardamos color de la baldosa según su orden en pantalla
    listaDesordenadaBaldosasTablero.splice(num, 1);
    }
    }


    for(var j:uint =0;j<arrayBaldosas.length;j++) { //añadimos las baldosas en pantalla como hijo del contenedor principal
    contenedorBaldosas.addChild(arrayBaldosas[j]);


    arrayBaldosas[j].name = ('baldosa' + String(j));


    trace(arrayBaldosas[j].name);
    }



    contenedorBaldosas.addEventListener(MouseEvent.MOU SE_OVER, cursorEncimaBaldosa);
    contenedorBaldosas.addEventListener(MouseEvent.MOU SE_OUT, cursorAbandonaBaldosa);
    contenedorBaldosas.addEventListener(MouseEvent.MOU SE_DOWN, clicEnBaldosa);
    contenedorBaldosas.addEventListener(MouseEvent.MOU SE_UP, sueltaBaldosa);

    function cursorEncimaBaldosa(evt:MouseEvent):void{
    evt.target.alpha = 0.7;
    }

    function cursorAbandonaBaldosa(evt:MouseEvent):void{
    evt.target.alpha = 1;
    }


    var x_original:uint = 0;
    var y_original:uint = 0;
    var baldosaMovil:Sprite = new Sprite();
    var puntoInicio:Point = new Point();




    function clicEnBaldosa(evt:MouseEvent):void {


    baldosaMovil = Sprite(evt.target);
    var nombreBaldosa:String = new String;


    puntoInicio.x = baldosaMovil.x;
    puntoInicio.y = baldosaMovil.y;

    parent.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
    }

    function moveBlock(e:MouseEvent):void {



    baldosaMovil.x = goodX(mouseX);
    baldosaMovil.y = goodY(mouseY);
    }

    function goodX(inX:Number):Number {

    if (inX < 0) {
    return 0;
    }

    if (inX > (espaciadoHorizontalBaldosa*anchoTablero + desplazamientoTableroX)) {
    return (espaciadoHorizontalBaldosa*anchoTablero + desplazamientoTableroX);
    }

    return inX;

    }


    function goodY(inY:Number):Number {

    if (inY < 0) {
    return 0;
    }

    if (inY > (espaciadoVerticalBaldosa*altoTablero + desplazamientoTableroY)) {
    return (espaciadoVerticalBaldosa*altoTablero + desplazamientoTableroY);
    }

    return inY;
    }




    reply_txt.text = "cogida";



    var temp_x:uint = 0;
    var temp_y:uint = 0;



    function sueltaBaldosa(evt:MouseEvent):void{



    recolocaBaldosas(baldosaMovil, arrayBaldosas);


    baldosaMovil = new Sprite();
    puntoInicio = new Point();
    }



    function recolocaBaldosas(baldosaMovil:Sprite, arrayBaldosas:Array):void {


    var objetivo:Sprite = new Sprite;
    var tempX:Number = 0;
    var tempY:Number = 0
    var PuntoInicioObjetivo:Point = new Point;


    for(var s:uint=0; s < arrayBaldosas.length; s++){
    objetivo = arrayBaldosas[s];
    //trace(arrayBaldosas[s].name); // sprite1

    if (baldosaMovil.hitTestObject(objetivo))

    {
    trace("chocan");


    PuntoInicioObjetivo.x = arrayBaldosas[s].x;
    PuntoInicioObjetivo.y = arrayBaldosas[s].y;

    objetivo.x = puntoInicio.x;
    objetivo.y = puntoInicio.y;

    baldosaMovil.x = PuntoInicioObjetivo.x;
    baldosaMovil.y = PuntoInicioObjetivo.y;


    break;
    }
    else {
    baldosaMovil.x = puntoInicio.x;
    baldosaMovil.y = puntoInicio.y;


    break;
    }



    baldosaMovil = new Sprite();
    puntoInicio = new Point();

    trace(baldosaMovil.name);
    trace(objetivo.name);

    }

    reply_txt.text = "";


    }



    }

    }
    }
    Attached Files Attached Files
    Last edited by stalyan; 12-30-2009 at 11:12 AM.

  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    I've attached a .zip in the original post with the .as and .fla files, also the .swf so you can check what I intent to do. If you execute the .swf, if you try to swap tiles 0 and 79 (top-left baldosa with bottom-right baldosa) you'll see that the code works as intended, but not for the other tile-baldosas, and I don't know why.

    Please, I beg any experienced AS3 programmer to help me on this!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center