2013-03-20 4 views
0

책의 Actionscript 교실에서 예제 4 단원의 예제를 수행하지만 스테이지에 지우기 (CLEAR) 버튼을 추가하여 수정합니다.취소 버튼이있는 동작 스크립트 그림

모든 기능을 테스트 할 때마다 작동하지만, 내 버튼 상단을 그릴 수 있습니다. 사용자가 그림을 그릴 때 이상적으로 색상이 단추 아래에 있어야합니다.

타임 라인에서 배경, 버튼 및 액션을위한 레이어가 있습니다. 문제를 빨리 해결할 수 있도록 아래 코드를 추가했습니다. 감사!

package { 

import flash.display.MovieClip; 

    public class Ellipse extends MovieClip { 

     // constructor 
     public function Ellipse(w:Number=40,h:Number=40,color:Number=0xff0000) { 
      graphics.beginFill(color); 
      graphics.drawEllipse(0, 0, w, h); 
      graphics.endFill(); 
     } 

    } // end class Ellipse 

} // end package 




import flash.events.MouseEvent; 

var color:Number; 
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing); 

function startDrawing(e:MouseEvent):void { 
stage.addEventListener(MouseEvent.MOUSE_MOVE, makeShapes); 
color = Math.random() * 0xFFFFFF; 
} 

function stopDrawing(e:MouseEvent):void { 
stage.removeEventListener(MouseEvent.MOUSE_MOVE, makeShapes); 
} 

function makeShapes(e:MouseEvent):void { 
var ellipse:Ellipse = new Ellipse(10, 10, color); 
stage.addChild(ellipse); 
ellipse.x = mouseX; 
ellipse.y = mouseY; 
} 


btnClear.addEventListener(MouseEvent.CLICK, clearBoard); 

function clearBoard(e:MouseEvent) 
{ 
    for (var i:int = stage.numChildren-1; i >= 1; i--) { 
    stage.removeChildAt (i); 
} 
} 

답변

0

addChild는 표시 목록의 상단에 항목을 추가, 그래서 당신이 무대에 타원을 추가 할 때, 당신은 당신의 버튼을 누르면 동영상의 앞에 추가된다. 즉, 버튼이있는 동영상은 인덱스 0에 있지만 모양 1은 인덱스 1 이상에 추가됩니다. 한 가지 해결책은 addChildAt을 사용하는 대신 영화 아래에 추가하는 것입니다 :

var shapeIndex:uint = 0; 
function makeShapes(e:MouseEvent):void { 
    var ellipse:Ellipse = new Ellipse(10, 10, color); 
    stage.addChildAt(ellipse, shapeIndex); // add the shape starting at 0, and count up from there 
    // this will keep the movie at the top of the stage's display list 
    shapeIndex++; 
    ellipse.x = mouseX; 
    ellipse.y = mouseY; 
} 

대체 솔루션 먼저 컨테이너 클립을 확인한 다음 대신이 컨테이너 클립 모양의를 추가하는 것입니다. 이렇게하면 도형이 나타나는 위치를 쉽게 제어 할 수 있습니다.

var container : Sprite = new Sprite(); 
stage.addChildAt(container, 0); // add the container to the bottom of the stage 
// now we can just easily add our shapes to the container, and they will all be behind the main movie. 
function makeShapes(e:MouseEvent):void { 
    var ellipse:Ellipse = new Ellipse(10, 10, color); 
    container.addChild(ellipse); 
    shapeIndex++; 
    ellipse.x = mouseX; 
    ellipse.y = mouseY; 
} 

그리고 이것은 실제로 화면을 지우는 것과 같은 다른 작업을 쉽게 만듭니다. 다음과 같이 컨테이너 클립을 제거하고 다시 만들면됩니다.

function clearBoard(e:MouseEvent) 
{ 
    stage.removeChild(container); 
    container = new Sprite(); 
    stage.addChildAt(container, 0); 
} 
+0

'스테이지'에 바로 추가하는 것은 좋지 않습니다. 대신 '루트'에 추가하는 것을 고려하십시오. –

+0

감사합니다. 마이크! 너는 도움이되었다. – PrgmRNoob