2010년 4월 2일 금요일

Duplicate - 다양한 객체 만들기(복제-패턴2)

 

- 한 무비클립을 복제하여 패턴을 만드는 예제를 하였었는데, 스터디 선생님의 권유로 약간 변형을 해보았다

 

 

  랜덤으로 색이 바뀌게 하는 네번째 버튼을 없애고 애초에 페이지에 진입하면 Timer 이벤트로 저절로 색이

  바뀌도록 바꿔보았다.

 

 

[code as3]
package
{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.events.IEventDispathcer;
import flash.geom.ColorTransform;

public class Duplicate_pattern extends Sprite
{
    public function Duplicate_pattern()
    {
        super();

        seedBox = new Sprite();

        seedBox.x = 80;
        seedBox.y = 20;

        addChild( seedBox );

        createButton();

        colorTimerConfig(); //타이머 이벤트 함수 호출
    }

    private var seedBox:Sprite;

    private var normalBtn:Sprite;

    private var alphaBtn:Sprite;

    private var scaleBtn:Sprite;

    private var colorBtn:Sprite;

    private var row:int = 8;

    private var col:int = 10;

    private var tot:int = row * col;

    private var gap:Numer = 30;

    /**
     *   @private
     *   랜덤컬러로 나오는 함수에 타이머 이벤트 등록
     */
    private function colorTimerConfig() : void
    {
        var timer:Timer = new Timer( 400, 10 ); //400/1000초, 10번 Timer객체 생성

        timer.start(); //타이머 시작

        timer.addEventListener( TimerEvent.TIMER, createColor ); //timer객체에 이벤트 등록
    }

    private function createButton() : void
    {
        var g:Graphics;

        if( !normalBtn )
        {
            normalBtn = new Sprite();

            g = normalBtn.graphics;

            g.clear();
            g.beginFill( 0xFFFFFF, 1 );
            g.drawRect( 0, 0, 50, 20 );
            g.endFill();

            normalBtn.x = 20;
            normalBtn.y = 20;

            normal.buttonMode = true;

           addChild( normalBtn );

           configListener( normalBtn );
        }

        if( !alphaBtn )
        {
            alphaBtn = new Sprite();

            g = alphaBtn.graphics;

            g.clear();
            g.beginFill( 0xFFFFFF, 1 );
            g.drawRect( 0, 0, 50, 20 );
            g.endFill();

            alphaBtn.x = 20;
            alphaBtn.y = normalBtn.y + gap;

            alphaBtn.buttonMode = true;

           addChild( alphaBtn );

           configListener( alphaBtn );
        }

        if( !scaleBtn )
        {
            scaleBtn = new Sprite();

            g = scaleBtn.graphics;

            g.clear();
            g.beginFill( 0xFFFFFF, 1 );
            g.drawRect( 0, 0, 50, 20 );
            g.endFill();

            scaleBtn.x = 20;
            scaleBtn.y = alphaBtn.y + gap;

            scaleBtn.buttonMode = true;

           addChild( scaleBtn );

           configListener( scaleBtn );
        }

        if( !colorBtn )
        {
            colorBtn = new Sprite();

            g = colorBtn.graphics;

            g.clear();
            g.beginFill( 0xFFFFFF, 1 );
            g.drawRect( 0, 0, 50, 20 );
            g.endFill();

            colorBtn.x = 20;
            colorBtn.y = scaleBtn.y + gap;

            colorBtn.buttonMode = true;

           addChild( colorBtn );

           configListener( colorBtn );
        }
    }

    private function configListener( dispatcher:IEventDispatcher ) : void
    {
        dispatcher.addEventListener( MouseEvent.CLICK, btnClickHandler );
    }

    private function btnClickHandler( e:MouseEvent ) : void
    {
        switch( e.target )
        {
            case normalBtn :
                createNormal();
                break;

            case alphaBtn :
                createAlpha();
                break;

            case scaleBtn :
                createScale();
                break;

            case colorBtn :
                createColor(e); //TimerEvent매개변수를 createColor함수에 넘김
                break;
        }
    }

    private function removeAllChild() : void
    {
        while( seedBox.numChildren )
        {
            seedBox.removeChildAt( 0 );
        }
        removeEventListener( TimerEvent.TIMER, createColor ); //버튼을 클릭할때마다 TimerEvent 제거
    }

    private function createNormal() : void
    {
        removeAllChild();

        var i:int = 0;

        for( i = 0; i < tot; i++ )
        {
            var temp:Sprite = new Seed();

            temp.x = ( i % col ) * 40 + gap;
            temp.y = Math.floor( i / col ) * 40 + gap;

            seedBox.addChild( temp );
        }
    }

    private function createAlpha() : void
    {
        removeAllChild();

        var i:int = 0;

        for( i = 0; i < tot; i++ )
        {
            var temp:Sprite = new Seed();

            temp.x = ( i % col ) * 40 + gap;
            temp.y = Math.floor( i / col ) * 40 + gap;

            temp.alpha = i / tot;

            seedBox.addChild( temp );
        }
    }

    private function createScale() : void
    {
        removeAllChild();

        var i:int = 0;

        for( i = 0; i < tot; i++ )
        {
            var temp:Sprite = new Seed();

            temp.x = ( i % col ) * 40 + gap;
            temp.y = Math.floor( i / col ) * 40 + gap;

            temp.scaleX = temp.scaleY = i / tot;
         
            seedBox.addChild( temp );
        }
    }

    private function createColor() : void
    {
        removeAllChild();

        var i:int = 0;

        for( i = 0; i < tot; i++ )
        {
            var temp:Sprite = new Seed();

            temp.x = ( i % col ) * 40 + gap;
            temp.y = Math.floor( i / col ) * 40 + gap;

            var ct:ColorTransform = new ColorTransform();
           
            ct.color = Math.random() * 0xFFFFFFFF;

            temp.transform.colorTransform = ct;

            seedBox.addChild( temp );
        }
    }
}
}
[/code]

- TimerEvent를 걸어준 부분은 앞 예제와 동일하고, TimerEvent를 추가하여 생성자에서 타이머 객체가 있는

  함수를 호출한다. 그래서 바로 0.4초마다 createColor함수를 10번 호출한다.

 

댓글 없음:

댓글 쓰기