Actionscript - 需要循环生成颜色值

发布于 2024-12-10 20:56:04 字数 188 浏览 0 评论 0原文

有一个 for 循环说像

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

Having a for loop say like

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-12-17 20:56:04

以下是 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 );
    }
}

Following on Pranav Hosangadi's answer:

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 );
    }
}
相守太难 2024-12-17 20:56:04

ActionScript 中的颜色使用 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();

colours in ActionScript are specified using uints

var myColor:uint=0xRRGGBB;

To draw a rectangle (in a 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文