2010-05-11 6 views
0

나는 일반적으로 플래시가 처음이며 MovieClip (Stems and Star)을 확장하는 두 클래스로 프로그램을 작성했습니다.클래스 내에서 장면에 자식 추가

사용자가 Star 개체 드래그를 중지했지만 Star 클래스의 코드에서 장면을 참조하는 방법을 알지 못하는 경우 새 Stems 개체를 장면의 자식으로 만들어야합니다.

나는 스타의 생성자에 장면을 통과하고 같은 sometihng 일을 시도했다 :

this.scene.addChild (new Stems()); 

하지만 분명히 그건하지 그것을 할 수있는 방법은 ... 아래 줄기와 별의 코드는이며, 조언은 크게 감사 할 것입니다.

package { 
import flash.display.MovieClip; 
import flash.events.*; 
import flash.utils.Timer; 


public class Stems extends MovieClip { 
    public const centreX=1026/2; 
    public const centreY=600/2; 
    public var isFlowing:Boolean; 
    public var flowerType:Number; 
    public const outerLimit=210; 
    public const innerLimit=100; 

    public function Stems(fType:Number) { 
     this.isFlowing=false; 
     this.scaleX=this.scaleY= .0007* distanceFromCentre(this.x, this.y); 
     this.setXY(); 
     trace(distanceFromCentre(this.x, this.y)); 
     if (fType==2) { 
      gotoAndStop("Aplant"); 
     } 

    } 

    public function distanceFromCentre(X:Number, Y:Number):int { 
     return (Math.sqrt((X-centreX)*(X-centreX)+(Y-centreY)*(Y-centreY))); 
    } 

    public function rotateAwayFromCentre():void { 
     var theX:int=centreX-this.x; 
     var theY:int = (centreY - this.y) * -1; 
     var angle = Math.atan(theY/theX)/(Math.PI/180); 
     if (theX<0) { 
      angle+=180; 
     } 
     if (theX>=0&&theY<0) { 
      angle+=360; 
     } 
     this.rotation = ((angle*-1) + 90)+180; 

    } 

    public function setXY() { 
     do { 
      var tempX=Math.random()*centreX*2; 
      var tempY=Math.random()*centreY*2; 
     } while (distanceFromCentre (tempX, tempY)>this.outerLimit || 
       distanceFromCentre (tempX, tempY)<this.innerLimit); 
     this.x=tempX; 
     this.y=tempY; 
     rotateAwayFromCentre(); 
    } 

    public function getFlowerType():Number { 
     return this.flowerType; 
    } 
} 

}

패키지 { 수입 flash.display.MovieClip의; import flash.events. *; import flash.utils.Timer;

public class Star extends MovieClip { 
    public const sWide=1026; 
    public const sTall=600; 
    public var startingX:Number; 
    public var startingY:Number; 
    public var starColor:Number; 
    public var flicker:Timer; 
    public var canUpdatePos:Boolean=true; 
    public const innerLimit=280; 

    public function Star(color:Number, basefl:Number, factorial:Number) { 
     this.setXY(); 
     this.starColor=color; 
     this.flicker = new Timer (basefl + factorial * (Math.ceil(100* Math.random()))); 
     this.flicker.addEventListener(TimerEvent.TIMER, this.tick); 

     this.addEventListener(MouseEvent.MOUSE_OVER, this.hover); 
     this.addEventListener(MouseEvent.MOUSE_UP, this.drop); 
     this.addEventListener(MouseEvent.MOUSE_DOWN, this.drag); 

     this.addChild (new Stems (2)); 

     this.flicker.start(); 
     this.updateAnimation(0, false); 
    } 

    public function distanceOK(X:Number, Y:Number):Boolean { 
     if (Math.sqrt((X-(sWide/2))*(X-(sWide/2))+(Y-(sTall/2))*(Y-(sTall/2)))>innerLimit) { 
      return true; 
     } else { 
      return false; 

     } 
    } 

    public function setXY() { 
     do { 
      var tempX=this.x=Math.random()*sWide; 
      var tempY=this.y=Math.random()*sTall; 
     } while (distanceOK (tempX, tempY)==false); 
     this.startingX=tempX; 
     this.startingY=tempY; 
    } 

    public function tick(event:TimerEvent) { 
     if (this.canUpdatePos) { 
      this.setXY(); 
     } 
     this.updateAnimation(0, false); 
     this.updateAnimation(this.starColor, false); 
    } 

    public function updateAnimation(color:Number, bright:Boolean) { 

     var brightStr:String; 

     if (bright) { 
      brightStr="bright"; 
     } else { 
      brightStr="low"; 

     } 
     switch (color) { 
      case 0 : 
       this.gotoAndStop("none"); 
       break; 
      case 1 : 
       this.gotoAndStop("N" + brightStr); 
       break; 
      case 2 : 
       this.gotoAndStop("A" + brightStr); 
       break; 
      case 3 : 
       this.gotoAndStop("F" + brightStr); 
       break; 
      case 4 : 
       this.gotoAndStop("E" + brightStr); 
       break; 
      case 5 : 
       this.gotoAndStop("S" + brightStr); 
       break; 
     } 
    } 

    public function hover(event:MouseEvent):void { 
     this.updateAnimation(this.starColor, true); 
     this.canUpdatePos=false; 
    } 

    public function drop(event:MouseEvent):void { 
     this.stopDrag(); 
     this.x=this.startingX; 
     this.y=this.startingY; 
     this.updateAnimation(0, false); 
     this.canUpdatePos=true; 



    } 

    public function drag(event:MouseEvent):void { 
     this.startDrag(false); 
     this.canUpdatePos=false; 

    } 

} 

}

답변

1

가장 빠른 방법으로 DisplayObject 부모를 참조하는 부모 변수를 사용하는 것입니다.

var stem:Stems = new Stems(2); 
stem.x = x; //optional: set stem coordinates to that of Star 
stem.y = y; 
parent.addChild(stem); 

당신이 무대에 스타 드래그 동작은 당신이 당신의 드롭 함수 내에서 위의 코드를 삽입 할 필요가 멈출 때마다 객체 줄기 추가합니다.

관련 문제