目标:使用向量以编程方式选择随机颜色主题。
旧策略:多维数组。这工作正常。
所需策略:我想将其切换为多维向量。它的实践和掌握程度与其他任何东西一样多。
错误: 1120:访问未定义的属性
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.*;
import classes.calculations.GeoMath;
import classes.graphics.*;
import classes.ui.*;
public class Document extends MovieClip {
var colorThemes:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
colorThemes[0] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[1] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[2] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[0][0] = 0xb26002; // 1120: Access of undefined property colorThemes.
colorThemes[0][1] = 0xff9720; // 1120: Access of undefined property colorThemes.
colorThemes[0][2] = 0xff8b07; // 1120: Access of undefined property colorThemes.
colorThemes[0][3] = 0x007eb2; // 1120: Access of undefined property colorThemes.
colorThemes[0][4] = 0x07b6ff; // 1120: Access of undefined property colorThemes.
colorThemes[1][0] = 0xdc45ff; // 1120: Access of undefined property colorThemes.
...
如果我可以使用Vector文字来做这件事会很棒,但我也不会错误地尝试我的问题。
然后,我尝试从同一个Document类访问Vector,该类位于另一个函数newAsteroid
内的函数timerFunction
内,如下所示:
function timerFunction(e: TimerEvent): void {
// some more code
function newAsteroid(): void {
var idx2: int = Math.floor(Math.random() * colorThemes.length);
for (var i: int = 0; i <= 10000; i++) {
var idx: int = Math.floor(Math.random() * colorThemes[idx2].length);
asteroid = new Asteroid();
asteroid.x = (Math.random() * (stage.stageWidth - 200) + 100);
asteroid.y = (Math.random() * (stage.stageHeight - 100) + 50);
asteroid.graphics.lineStyle();
//var thiscolorVector: Array = [colorThemes[idx2][idx], colorThemes[idx2][idx]];
//var thisGradientArray: Array = [1, -20];
//var thisRatiosArray: Array = [0, 250];
//asteroid.graphics.beginGradientFill("radial", thiscolorVector, thisGradientArray, thisRatiosArray);
asteroid.graphics.beginFill(colorThemes[idx2][idx], dim);
asteroid.graphics.drawRect(-1, -1, 2, 2);
addChild(asteroid);
asteroids.push(asteroid);
}
}
...
任何人都可以看到我如何摆脱这些错误并制作多维色彩主题矢量?
我真的不认为问题在于我构建Vector的方式。我只是在另一个.fla文件中尝试过,它运行得很好。所以问题必须在于我如何尝试访问它或我试图从哪里访问它?
答案 0 :(得分:2)
您在类中编写代码,因此您需要在某个方法中执行此操作。一个选项如下:
public class Document extends MovieClip {
var colorThemes:Vector.<Vector.<uint>> = initColorThemes();
public function initColorThemes():Vector.<Vector.<uint>> {
var output:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
output[0] = new Vector.<uint>(5);
output[1] = new Vector.<uint>(5);
output[2] = new Vector.<uint>(5);
output[0][0] = 0xb26002;
return output;
}
答案 1 :(得分:1)
该属性在主类中声明(我的是public class Document extends MovieClip {
),然后必须在函数内部填充,就像标记为“已接受”的答案中所示。我以为我会发布这个,因为Petr Hrehorovsky的例子由于某些原因对我不起作用。最后,当我重新阅读this大约1120个错误时,所有这些都被点击了。
package {
public class Document extends MovieClip {
// the property has to go here! But not the values!
var colorThemes:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
public function Document() {
// declare the "internal" vectors
colorThemes[0] = new Vector.<uint>(5); // now I can make the "internal" vectors without an 1120 error
colorThemes[1] = new Vector.<uint>(5); // because colorThemes has been declared
colorThemes[2] = new Vector.<uint>(5); // as a property of Document class.
colorThemes[0][0] = 0xb26002; // These also work now because colorThemes has been declared
colorThemes[0][1] = 0xff9720;
colorThemes[0][2] = 0xff8b07;
colorThemes[0][3] = 0x007eb2;
colorThemes[0][4] = 0x07b6ff;
colorThemes[1][0] = 0xdc45ff;
colorThemes[1][1] = 0x733fe8;
colorThemes[1][2] = 0x23a8e8;
colorThemes[1][3] = 0x26ffd9;
colorThemes[1][4] = 0x3359ff;
colorThemes[2][0] = 0x2eccc9;
colorThemes[2][1] = 0x3d9998;
colorThemes[2][2] = 0x00ff6b;
colorThemes[2][3] = 0xff3b6c;
colorThemes[2][4] = 0xcc14a0;