2009-03-16 6 views
9

आप कैसे एक स्प्राइट पर पंजीकरण बिंदु निर्धारित हैं या actionscript के माध्यम से आकार (मैं जानता हूँ कि यह फ्लैश का उपयोग कर तुच्छ है, लेकिन मैं actionscript में विशुद्ध रूप से यह सब करने की ज़रूरत)एएस 3: एक्शनस्क्रिप्ट के साथ प्रदर्शन ऑब्जेक्ट का पंजीकरण बिंदु सेट करना?

उत्तर

2

आप के लिए सूचकांक मतलब है?

टिप्पणियों के बाद, आप नीचे दिए गए त्वरित कार्यान्वयन कर सकते हैं। यह वही नहीं है जो आप चाहते हैं क्योंकि आप प्रत्येक बच्चे के लिए अलग-अलग संरेखण सेट नहीं कर सकते हैं। मैं बस, यह भी जटिल बनाने के लिए नहीं करना चाहता था यह आप एक विचार देने के लिए और अधिक की तरह 'काम कर-छद्म कोड' है ...

package 
{ 
    import flash.display.DisplayObject; 
    import flash.display.Sprite;  


    public class SpriteWithRegistration extends Sprite 
    { 

     private var _regV:String = "T"; 
     private var _regH:String = "L"; 

     private var _width:Number = 0; 
     private var _height:Number = 0; 


     public function SpriteWithRegistration(width:Number, height:Number, registrationPoint:String = "TL") 
     { 
      this.width = height; 
      this.height = width; 
      this.registrationPoint = registrationPoint; 
     } 

     public function set registrationPoint(p:String):void 
     { 
      if(p.length != 2) return; 

      var regV:String = p.toUpperCase().substr(0, 1); 
      var regH:String = p.toUpperCase().substr(1, 1); 

      _regV = (regV == "T" || regV == "C" || regV == "B") ? regV : _regV; 
      _regH = (regH == "L" || regH == "C" || regH == "R") ? regH : _regH; 

      alignChildren(); 
     } 

     override public function addChild(child:DisplayObject):DisplayObject 
     { 
      alignChild(child); 
      super.addChild(child); 
      return child; 
     } 

     override public function set width(value:Number):void 
     { 
      _width = value; 
      alignChildren(); 
     } 

     override public function get width():Number 
     { 
      return _width; 
     } 

     override public function set height(value:Number):void 
     { 
      _height = value; 
      alignChildren(); 
     } 

     override public function get height():Number 
     { 
      return _height; 
     } 

     private function alignChildren():void 
     { 
      for(var index:int = 0;index < numChildren; index++) 
       alignChild(getChildAt(index)); 
     } 

     private function alignChild(disp:*):void 
     { 
      switch(_regH) 
      { 
       case "L": disp.x = 0;        break; 
       case "C": disp.x = _width*.5 - disp.width * .5; break; 
       case "R": disp.x = _width - disp.width;   break; 
      } 

      switch(_regV) 
      { 
       case "T": disp.y = 0;        break; 
       case "C": disp.y = _height*.5 - disp.height * .5; break; 
       case "B": disp.y = _height - disp.height;   break; 
      } 
     } 
    } 
} 
+0

नहीं, पंजीकरण बिंदु। डिफ़ॉल्ट रूप से यह शीर्ष, बाएं है। – Ronn

+0

बीटीडब्ल्यू, इसके लिए sprites और आकार के लिए कोई डिफ़ॉल्ट कार्यान्वयन नहीं है। जो समझ में आता है क्योंकि यह पूरी तरह से निर्भर करेगा कि आपकी वस्तु का आकार कैसा है। यूआई ढांचे का उपयोग करना या अपना खुद का निर्माण करना सबसे अच्छा है। धारक के आकार और बच्चों के संरेखण को निर्दिष्ट करने का विचार। –

0

कारण है कि आप इसकी आवश्यकता के आधार पर यह द्वारा हल किया जा सकता है DisplayObject का उपयोग कर। transformmatrix, जो Matrix ऑब्जेक्ट देता है।

उदाहरण के लिए, यदि आप रोटेशन के बिंदु को बदलना चाहते हैं तो आप ट्रांसफॉर्म के मैट्रिक्स को ऐसा करने के लिए उपयोग कर सकते हैं।

11

फ़्लैश प्लेयर एपीआई इसका खुलासा नहीं करता है। मेरा मानना ​​है कि ऐसा इसलिए है क्योंकि फ्लैश वास्तव में पंजीकरण बिंदु में बेक करता है जब यह एसडब्ल्यूएफ बनाता है। इस प्रकार, स्थानांतरित करने के लिए कोई वास्तविक पंजीकरण बिंदु नहीं है (इसके बजाय, आप आकृति डेटा ले जायेंगे ... यदि फ़्लैश प्लेयर आपको आकार डेटा संपादित करने देता है!)।

मैं हमेशा अपने स्प्राइट/आकार को अन्य डिस्प्ले ऑब्जेक्ट पर parenting करके हल करता हूं। तो हर जगह पर spriteB को मैं पहले spriteA की चर्चा करते हुए किया गया था उल्लेख से


var spriteB:Sprite = new Sprite(); 
spriteB.addChild(spriteA); 
spriteA.x = 15; 
spriteA.y = 35; 

और फिर: तो, अगर मैं spriteA है और (15, 35) के लिए अपना पंजीकरण बिंदु सेट करना चाहते हैं, मैं यह कर चाहते हैं।

+1

यह निश्चित रूप से सबसे आसान और सरल तरीका है। यद्यपि सबसे सुरुचिपूर्ण नहीं है। Bu यदि कोई अतिरिक्त कंटेनर रखने का उपर कोई मुद्दा नहीं है ... –

5

ट्रांसफॉर्म मैट्रिक्स का उपयोग करना यह संभव है। this site पर पाए गए इसका एक अच्छा कार्यान्वयन यहां दिया गया है।

public function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean) 
{ 
    //translate movieclip 
    s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy); 

    //registration point. 
    if (showRegistration) 
    { 
     var mark:Sprite = new Sprite(); 
     mark.graphics.lineStyle(1, 0x000000); 
     mark.graphics.moveTo(-5, -5); 
     mark.graphics.lineTo(5, 5); 
     mark.graphics.moveTo(-5, 5); 
     mark.graphics.lineTo(5, -5); 
     s.parent.addChild(mark); 
    } 
} 
+1

क्या आप इसे और विस्तार से समझा सकते हैं, कृपया? – Vishnu

+0

लिंक मर चुका है। शायद यह बेहतर समझना एक अच्छी बात थी। –

1

मुझे उम्मीद है कि यह किसी की मदद करेगा। स्टार्लिंग ढांचे में एक शानदार विधि है, जिसे "alignPivot()" कहा जाता है। मैंने अपनी अवधारणा ली और इसे फ्लैश डिस्प्लेलिस्ट में अनुकूलित किया। आप अनिवार्य रूप से बाएं, दाएं, केंद्र, ऊपर और नीचे के पंजीकरण को बदलने के लिए एक स्प्राइट बताते हैं। यह कोड आपके स्प्राइट को एक कंटेनर स्प्राइट में रखता है और खुद को उचित स्थिति में रखता है। यह अंदर मौजूद मूल स्प्राइट के साथ कंटेनर स्प्राइट देता है।

कोड एक अलग नामक "स्थिति" में संग्रहीत कोड।

public static function alignPivot(s:DisplayObject, horizontalAlign: String = "center", verticalAlign: String = "center", showRegistration: Boolean = false, _color: uint = 0x000000): Sprite { 

     //create a container sprite to house the sprite you'd like to move 
     var _container: Sprite = new Sprite(); 
     //add your sprite to the continer sprite (the container sprite is what will be returned) 
     _container.addChild(s); 
     //using getBounds(), find the x,y,width,and height of your sprite within the continer. 
     var bounds:Rectangle = _container.getBounds(s); 
     //create variables for x and y cooridnates 
     var xVal: Number; 
     var yVal: Number; 

     //I have a separate class called Align which contains public static constants for positiong. 
     //Check the values passed above and get the correct x value; 
     if (horizontalAlign == Align.LEFT) xVal = -bounds.x; 
     else if (horizontalAlign == Align.CENTER) xVal = -bounds.x - bounds.width * .5; 
     else if (horizontalAlign == Align.RIGHT) xVal = -bounds.x - bounds.width; 
     else throw new ArgumentError("Invalid horizontal alignment: " + horizontalAlign); 

     //Check the values passed above and get the correct y value; 
     if (verticalAlign == Align.TOP) yVal = -bounds.y; 
     else if (verticalAlign == Align.CENTER) yVal = -bounds.y - bounds.height * .5; 
     else if (verticalAlign == Align.BOTTOM) yVal = -bounds.y - bounds.height; 
     else throw new ArgumentError("Invalid vertical alignment: " + verticalAlign); 

     //apply the new x and y cooridnates to your sprite (the one moving within the container we created above) 
     s.x = xVal; 
     s.y = yVal; 

     //optional - this will create a small X at the 0,0 position of the container. 
     //This is helpful if you want to see where your registration points are 
     if (showRegistration) { 
      var mark: Sprite = new Sprite(); 
      mark.graphics.lineStyle(1, _color); 
      mark.graphics.moveTo(-5, -5); 
      mark.graphics.lineTo(5, 5); 
      mark.graphics.moveTo(-5, 5); 
      mark.graphics.lineTo(5, -5); 
      _container.addChild(mark); 
     } 
     //return your contianer sprite 
     //This will replace the sprite that you passed in the first parameter. 
     //That sprite is inside the container, so you won't notice 
     return _container; 


    } 

कार्यान्वयन: ताकि आप उन्हें अपने आप को लिखने के लिए नहीं है स्थिरांक की

//Create your sprite 
     var _holder: Sprite = new Sprite(); 
    //Create a shape to put in your sprite 
     var my_shape: Shape = new Shape(); 
     my_shape.graphics.beginFill(0x000000); 
     my_shape.graphics.drawRect(0, 0, 200, 100); 
     my_shape.graphics.endFill(); 
    //Add the shape to your sprite 
     _holder.addChild(my_shape); 
    //Align the holder (must be done AFTER all children have been added) 
    //This will put the registration point at the top-center of the sprite. 
    //_holder is replaced by a sprite (container) that contains _holder. 
    //The _holder inside the container is positioned appropriately. 
    _holder = Position.alignPivot(_holder,Align.CENTER, Align.TOP); 
    //Add the new sprite to your stage. 
    this.addChild(_holder); 

सूची:

public static const CENTER:String = "center"; 
    public static const LEFT:String = "left"; 
    public static const RIGHT:String = "right"; 
    public static const TOP:String = "top"; 
    public static const BOTTOM:String = "bottom";