在as3中创建网格

时间:2018-06-29 12:23:10

标签: performance actionscript-3 flash optimization lag

我正在尝试使用as3网格化为正方形。 我正在使用嵌套的for循环来创建网格。

import flash.display.Shape;
import flash.display.MovieClip;    

var n=10;

var myClip = new MovieClip;
stage.addChild(myClip);

for (var i = 0; i < n * 10; i += 10) {
    for (var j = 0; j < n * 10; j += 10) {
        var _shape = new Shape;
        myClip.addChild(_shape);
        _shape.graphics.lineStyle(1);
        _shape.graphics.beginFill(0xcccccc);
        _shape.graphics.drawRect(i, j, 10, 10);
        _shape.graphics.endFill();
    }
}

我正在myClip上应用“缩放和平移”手势。对于此网格大小,一切正常。一旦我增加n的值,它就会开始滞后

我的游戏需要更大的网格。请帮助

1 个答案:

答案 0 :(得分:0)

我将以代码形式展示一些想法,我的AS3有点生锈,但也许有帮助。

您可以减少Shape实例的数量或将基于矢量的网格图形绘制到位图。由于正在缩放和平移,因此需要对其进行平滑处理。根据性能和缩放程度,您可能还想以比最初使用的分辨率更高的分辨率绘制位图。请参阅下面的评论。

import flash.display.Shape;
import flash.display.MovieClip;    

var n=10;

// do you actually need dynamic binding or a timeline in this?
// if not (also if you don't know what it means) do use the Sprite class instead
var myClip = new MovieClip;
stage.addChild(myClip);

// if you don't need individual shapes move this out of the for loop
// your logic with all of the shapes having the same origin suggests this might be so
var _shape = new Shape;
myClip.addChild(_shape);

for (var i = 0; i < n * 10; i += 10) {
    for (var j = 0; j < n * 10; j += 10) {
        _shape.graphics.lineStyle(1);
        _shape.graphics.beginFill(0xcccccc);
        _shape.graphics.drawRect(i, j, 10, 10);
        _shape.graphics.endFill();
    }
}

// then we can draw the grid to a bitmap
// since you are scaling it I'm drawing it a 300% resolution
var _bmd:BitmapData = new BitmapData(_shape.width*3, _shape.height*3, true, 0);
var _bit:Bitmap = new Bitmap(_bmd);
myClip.addChild(_bit);
_shape.width*=3;
_shape.height*=3;
_bmd.draw(stage); 
_bit.smoothing = true;
_bit.width/=3;
_bit.height/=3;

// and remove the vector shape so it won't be rendered
myClip.removeChild(_shape);
// if need be it can be redrawn later