2011-10-19 25 views
-4

產生顏色值有一個for循環說像在ActionScript的Actionscript - 需要在循環中

for (i=0;i<10;i++) { 
    // need to generate color value for each i. 
    //need to set the color value for drawing rectangle dynamically 
} 
+1

你需要更具體一點關於你想要生成什麼樣的顏色。另外,你是使用繪圖API還是位圖繪製? – grapefrukt

+0

嗨,我正在繪製使用graphics.beginfill()方法內的精靈。無論如何,下一次我會更具體。 Pranav和Ptrick幫助解決了這個問題。 – Arulmurugan

回答

1

關於Pranav Hosangadi的回答:

private function createColors(n:int):Array 
{ 
    var colors:Array = []; 

    for(var i= 0; i < n ; i++) 
    { 
    // for instance, but you could come up with 
    // a variety of formulas , depending on what kind of spectrum 
    // you're looking for! 
    var color:uint = 0xffffff * Math.random(); 

    // add your newly created color to the Array 
    // you could also do : colors[i] = 0xffffff * Math.random(); 
    colors[i] = color; 
    } 

    return colors; 
} 


//after this you can generate your rectangles 

private function createRectangles():void 
{ 
    var colors:Array = createColors(10); 

    for(var i= 0; i < colors.length ; i++) 
    { 
     var rect:Sprite = new Sprite(); 

     var g:Graphics=rect.graphics; 
     g.beginFill(colors[i]); 
     g.drawRect(10 * i , 10 * i, 100, 150); 
     g.endFill(); 

     this.addChild(rect); 
    } 
} 
1

顏色是使用指定uint小號

var myColor:uint=0xRRGGBB; 

要繪製矩形(在DisplayObject

var g:Graphics=this.graphics; 
g.beginFill(0xcc0000); //Red fill 
g.drawRect(10, 10, 100, 150); //Rectangle at (10, 10) with size (100, 150) 
g.endFill();