我正在尝试创建一个正方形数组,然后将它们添加到6x12网格中的舞台上,然后才能更改它们的颜色。我已经设法创建了一个数组数组并存储了正方形,但我现在不知道如何将它们添加到网格中的舞台上。我是一名新程序员,所以如果我没有正确格式化我的代码,我会提前道歉。感谢你的时间。
public class Main扩展Sprite {
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.DEACTIVATE, deactivate);
// touch or gesture?
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//array of arrays
var pixelArray:Array = [row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11, row12]
// Row arrays
var row1:Array = [];
var row2:Array = [];
var row3:Array = [];
var row4:Array = [];
var row5:Array = [];
var row6:Array = [];
var row7:Array = [];
var row8:Array = [];
var row9:Array = [];
var row10:Array = [];
var row11:Array = [];
var row12:Array = [];
for (var i:int = 0; i < 6; i++)
{
// "Pixel"
var pixel:Shape = new Shape; // initializing the variable
pixel.graphics.lineStyle(1, 0x000000, 1);
pixel.graphics.beginFill(0xFF0000); // fill colour
pixel.graphics.drawRect(0, 0, 30, 30); // (x spacing, y spacing, width, height)
pixel.graphics.endFill(); // end of fill
//addChild(pixel); // adds to stage, all shapes will be in same place?
row1.push(pixel);
row2.push(pixel);
row3.push(pixel);
row4.push(pixel);
row5.push(pixel);
row6.push(pixel);
row7.push(pixel);
row8.push(pixel);
row9.push(pixel);
row10.push(pixel);
row11.push(pixel);
row12.push(pixel);
}
addChild(pixelArray [1][1]); //tried to add just one square to stage, I think syntax is wrong here
}
private function deactivate(e:Event):void
{
// make sure the app behaves well (or exits) when in background
//NativeApplication.nativeApplication.exit();
}
}
}
答案 0 :(得分:0)
您将同一个pixel
放入每一行。要制作更多像素,您需要为每列每行创建一个像素。为了避免使用copypaste,您可以创建一个返回现成Shape
对象的函数,然后在所有rowX.push()
语句中调用它。像这样:
function makePixel(fillColor:int=0xff0000):Shape {
var pixel:Shape=new Shape();
pixel.graphics.lineStyle(1, 0x000000, 1);
pixel.graphics.beginFill(fillColor); // fill colour
pixel.graphics.drawRect(0, 0, 30, 30); // (x spacing, y spacing, width, height)
pixel.graphics.endFill(); // end of fill
return pixel;
}
row1.push(makePixel());
row2.push(makePixel());
// etc