flash on 2013-9-12

by marcsali
Copyright marcsali ( http://wonderfl.net/user/marcsali )
MIT License ( http://www.opensource.org/licenses/mit-license.php )
Downloaded from: http://wonderfl.net/c/sdaz

import caurina.transitions.Tweener;
♥0 | Line 124 | Modified 2013-09-13 00:08:49 | MIT License
play

ActionScript3 source code

/**
 * Copyright marcsali ( http://wonderfl.net/user/marcsali )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lW4A
 */

/**
 * Copyright marcsali ( http://wonderfl.net/user/marcsali )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sdaz
 */
// forked from flashrod's 15 puzzle
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
//import caurina.transitions.Tweener;
import gs.TweenLite;
public class Main extends Sprite {    
        private static const W:int = 300; //465
        private static const H:int = 300; //465
        private static const U:int = int(W/4);
        private static const V:int = int(H/4);
        private var _count:TextField;
        private var board:Array = [];    
        public function Main() {
            for (var k:int = 1; k < 17; ++k) {
                var p:Piece = new Piece(k, U, V);
                addChild(p);
                board.push(p);
            }
            var i:int, j:int = 3;
            for (k = 0; k < 20; ++k) {
                i = int(Math.random()*3);
                shift(i, j);
                j = int(Math.random()*3);
                shift(i, j);
            }
            stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
                shift(e.stageX/U, e.stageY/V);
                repaint();
                // Info2();
            });
            repaint();
            Info();
        }
      }  
        private function shift(x:int, y:int):void {
            if (x>=0 && x<4 && y>=0 && y<4) {
                for (var i:int = 0; i < 4; ++i) {
                    var p:Piece = board[y*4+i];
                    if (p.value == 16) {
                        for (; i>x; --i)
                            board[y*4+i] = board[y*4+i-1];
                        for (; i<x; ++i)
                            board[y*4+i] = board[y*4+i+1];
                        board[y*4+x] = p;
                        return;
                    }
                }
                for (var j:int = 0; j<4; ++j) {
                    p = board[j*4+x];
                    if (p.value == 16) {
                        for (; j>y; --j)
                            board[j*4+x] = board[(j-1)*4+x];
                        for (; j<y; ++j)
                            board[j*4+x] = board[(j+1)*4+x];
                        board[y*4+x] = p;
                        return;
                    }
                }
            }
        }
        private function repaint():void {
            for (var j:int = 0; j < 4; ++j) {
                for (var i:int = 0; i < 4; ++i) {
                    var p:Piece = board[j*4+i];
                    var cntr:int = 5;
                   // p.x = i*U;
                   // p.y = j*V; 
                   // Tweener.addTween(p, {x:i*U, y:j*V, time:1.0, transition:"easeOutQuad"});
                   TweenLite.to(p, 1.2 ,{x:i*U, y:j*V});
                   TweenLite.to(p, 2, {x:i*U-cntr,y:j*V-cntr });
                }
            }
        }
        
        public function Info():void {
        var unFormat:TextFormat = new TextFormat();
        unFormat.font = "Arial";
        unFormat.size = 14;
        unFormat.align = "center";
        unFormat.color = 0x333333;
        var unTexte:TextField = new TextField();
        unTexte.x = 0;
        unTexte.y = 300;
        //unTexte.width = 50;
        //unTexte.height = 15;
        unTexte.defaultTextFormat = unFormat;
        unTexte.selectable = false;
        unTexte.multiline = true;
        unTexte.background = false;
        unTexte.border = false ;
        //unTexte.type = "dynamic";
        unTexte.text = "puntos:";
        addChild(unTexte); 
          }    
        public function Info2():void {
        var unFormat2:TextFormat = new TextFormat();
        unFormat2.font = "Arial";
        unFormat2.size = 14;
        unFormat2.align = "center";
        unFormat2.color = 0x333333;
        var unTexte2:TextField = new TextField();
        unTexte2.x = 50;
        unTexte2.y = 300;
        //unTexte2.width = 50;
        //unTexte2.height = 15;
        unTexte2.defaultTextFormat = unFormat2;
        unTexte2.selectable = false;
        unTexte2.multiline = true;
        unTexte2.background = false;
        unTexte2.border = false;
        unTexte2.type = "dynamic";
        unTexte2.text = "number";
        addChild(unTexte2); 
          }             
  //  }
}
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
class Piece extends Sprite {
    public var value:int;
    public function Piece(value:int, w:int, h:int) {
        this.value = value;
        var fmt:TextFormat = new TextFormat();
        fmt.font = "Verdana";
        fmt.size = Math.min(w, h) * .4;
        fmt.color=0xFFFFFF;
        var t:TextField = new TextField();
        t.defaultTextFormat = fmt;
        t.text = value < 16 ? String(value) : "[+]";
        t.selectable = true;  
        addChild(t);  
    }
}