我是一个正在尝试将参数传递给函数的AS3菜鸟。有人可以帮助我理解为什么当以下代码都在一个AS3文件中时,如下所示它可以工作并绘制紫色方块......
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var square_commands:Vector.<int> = new Vector.<int>(5,true);
square_commands[0] = 1;//moveTo
square_commands[1] = 2;//lineTo
square_commands[2] = 2;
square_commands[3] = 2;
square_commands[4] = 2;
var square_coord:Vector.<Number> = new Vector.<Number>(10,true);
square_coord[0] = 20; //x
square_coord[1] = 10; //y
square_coord[2] = 50;
square_coord[3] = 10;
square_coord[4] = 50;
square_coord[5] = 40;
square_coord[6] = 20;
square_coord[7] = 40;
square_coord[8] = 20;
square_coord[9] = 10;
Fill(square_commands, square_coord);
}
public function Fill(a:Vector.<int>,b:Vector.<Number>){
import flash.display.*;
graphics.beginFill(0x442266);//set the color
graphics.drawPath(a, b);
}
}
}
然而,当我将代码分成两个AS3文件并尝试将参数传递给函数时,如下所示......
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var square_commands:Vector.<int> = new Vector.<int>(5,true);
square_commands[0] = 1;//moveTo
square_commands[1] = 2;//lineTo
square_commands[2] = 2;
square_commands[3] = 2;
square_commands[4] = 2;
var square_coord:Vector.<Number> = new Vector.<Number>(10,true);
square_coord[0] = 20; //x
square_coord[1] = 10; //y
square_coord[2] = 50;
square_coord[3] = 10;
square_coord[4] = 50;
square_coord[5] = 40;
square_coord[6] = 20;
square_coord[7] = 40;
square_coord[8] = 20;
square_coord[9] = 10;
Fill(square_commands, square_coord);
}
}
}
和...
package{
import flash.display.*;
public class Fill extends Sprite{
public function Fill(a:Vector.<int>,b:Vector.<Number>){
graphics.beginFill(0x442266);//set the color
graphics.drawPath(a, b);
}
}
}
Flash CS5给出了一条错误消息1137,表示它在代码行中只需要1个参数 - &gt;填充(square_commands,square_coord);
有人可以解释我如何将参数square_commands和square_coord传递给第二个AS3文件中的函数吗?
提前感谢所有帮助!!!
答案 0 :(得分:0)
您不需要创建单独的Fill类,您应该在单独的文件中创建一个Fill函数,它位于您的Main文件所在的同一个包中。 所以你的Fill.as文件可能如下所示:
// ActionScript file
package{
import flash.display.Sprite;
public function Fill(target:Sprite, a:Vector.<int>,b:Vector.<Number>):void{
target.graphics.beginFill(0x442266);//set the color
target.graphics.drawPath(a, b);
}
}
提及附加参数target,它将提供Sprite,其中应绘制矩形。
函数调用可能看起来:
Fill(this, square_commands, square_coord);
答案 1 :(得分:0)
// main.as
package{
// good practice is to declare all imports in one spot and at the top of the class.
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var square_commands:Vector.<int> = new Vector.<int>(5,true);
square_commands[0] = 1;//moveTo
square_commands[1] = 2;//lineTo
square_commands[2] = 2;
square_commands[3] = 2;
square_commands[4] = 2;
var square_coord:Vector.<Number> = new Vector.<Number>(10,true);
square_coord[0] = 20; //x
square_coord[1] = 10; //y
square_coord[2] = 50;
square_coord[3] = 10;
square_coord[4] = 50;
square_coord[5] = 40;
square_coord[6] = 20;
square_coord[7] = 40;
square_coord[8] = 20;
square_coord[9] = 10;
// notice we are passing the parameters to the constructor of the Fill class
var fill = new Fill(square_commands, square_coord);
}
}
}
// Fill.as
package{
import flash.display.*;
public class Fill extends Sprite{
// A function with the same name of the class file is the constructor.
// A constructor will be called every time the new operator is called like I did in main.
public function Fill(a:Vector.<int>,b:Vector.<Number>){
graphics.beginFill(0x442266);//set the color
graphics.drawPath(a, b);
}
}
}
I suggest you read up on OOP and learn it if you are going to do lots of coding.