TLDR:
如何创建一类函数并在另一个.as文件中访问它们?我可能需要导入包吗?如果是这样,我会在package
后放置什么?我试着输入它所在的文件夹的名称而且没有用。
有人可以帮我解决在AS3中创建一类函数的语法吗?
我正在为这个项目划分我不断增长的脚本。许多功能都与处理push,pop,shift,unshift数组内容有关。所以我想我有一个名为ArrManagement
的班级。我现在做的就是这样:
package {
public class ArrManagement{
public function ArrManagement() {
}
public function doStuff(){
trace("good to go");
}
}
}
由于它只是保留功能,我认为它不需要extend
任何事情......如果我错了,请纠正我。
构造函数是空的,因为我真的不希望它特别做任何事情。
doStuff
功能显然只是填充物。
因此,这个.as文件保存在与该项目的所有其他.as文件相同的文件夹中,并且它们都可以正常工作。
所以我尝试在我的主文档类构造函数上面实例化这个类(我们使用var
声明属性)
arrMan:ArrManagement = new ArrManagement();
我的IDE为这个类自动完成了,所以它至少被识别了。但是,在代码的其他地方我尝试这个:
arrMan.doStuff();
我收到以下错误:
错误:
1061: Call to a possibly undefined method doStuff through a reference with static type Class.
和
1172: Definition AnimationFiles:ArrManagement could not be found.
是的,在现实生活中,这个函数比简单的trace
调用更复杂,但假设我想开始简单,所以假设这个是我想要使用的函数,我怎么能使用像这样的独特的.as文件来实现这个目标呢?
对于null,谁要求一个具体的例子,你去:
private function addAndRemoveTiles(pan:String):void
{
var lenRows:int = onScreenRows;
var lenCols:int = onScreenCols;
if (pan == "right"){
onScreenLeftCol++;
// for each row
for (var i:int = 0; i < lenRows; i++){
//////////////////
// remove tile from Left
//////////////////
// t = Tile removed from left of row
var t:Tile = onScreenArray[i].shift();
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// add Tile from left of row
t.x = tileNum * offScreenDeckSpacingX * SCALE_UP + offScreenDeckX;
t.y = arrGround[tileNum].length * offScreenDeckSpacingY * SCALE_UP + offScreenDeckY;
addChild(t);
/////////////
// add tile to Right
/////////////
// c = right tile col (same)
var c:int = onScreenCols + onScreenLeftCol-1;
// cType = right tile Type (same)
var cType:int = complexArray[i+onScreenTopRow][c];
// t = Tile to take from arrGround
var t:Tile = arrGround[cType][arrGround[cType].length - 1];
// farRightTile = Tile on far right of screen (same)
var farRightTile:Tile = onScreenArray[i][onScreenArray[i].length-1];
// add tile from arrGround to onScreeArray
onScreenArray[i].push(t);
// remove tile from arrGround to onScreenArray
arrGround[cType].pop();
// place tile on screen
t.x = farRightTile.x + tileWidth;
t.y = farRightTile.y;
addChild(t);
}
} else if (pan == "left"){
onScreenLeftCol--;
for (var i:int = 0; i < lenRows; i++){
// remove tile from Right
var t:Tile = onScreenArray[i].pop();
addChild(t);
var tileNum:int = tileType(t);
arrGround[tileNum].push(t);
t.x = tileNum * offScreenDeckSpacingX * SCALE_UP + offScreenDeckX;
t.y = arrGround[tileNum].length * offScreenDeckSpacingY * SCALE_UP + offScreenDeckY;
// add tile to Left
var c:int = onScreenLeftCol;
var cType:int = complexArray[i+onScreenTopRow][c];
var t:Tile = arrGround[cType][arrGround[cType].length - 1];
var farLeftTile:Tile = onScreenArray[i][0];
onScreenArray[i].unshift(t);
arrGround[cType].pop();
// place tile on Screen
t.x = farLeftTile.x - tileWidth;
t.y = farLeftTile.y;
addChild(t);
}
} else if (pan == "down"){
onScreenTopRow++;
//////////////////
// remove tile from Top
//////////////////
var removedArray:Array = onScreenArray.shift();
for (var i:int = removedArray.length; i > 0; i--){
// t = Tile removed from top of column
var t:Tile = removedArray[removedArray.length-1];
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// add Tile from top of column
addChild(t);
t.x = tileNum * offScreenDeckSpacingX * SCALE_UP + offScreenDeckX;
t.y = arrGround[tileNum].length * offScreenDeckSpacingY * SCALE_UP + offScreenDeckY;
removedArray.pop();
}
//add new Bottom Row
onScreenArray.push([]);
// for each col
for (var i:int = 0; i < lenCols; i++){
/////////////
// add tile to Bottom
/////////////
// botRow = bottom tile row
var botRow:int = onScreenRows-1 + onScreenTopRow;
// tType = bottom tile Type
var tType:int = complexArray[botRow][i+onScreenLeftCol];
// t = Tile to take from arrGround
var t:Tile = arrGround[tType][arrGround[tType].length - 1];
// tile on bottom of pertinent column
var bottomTile:Tile = onScreenArray[onScreenArray.length-2][i];
// add tile from arrGround to onScreeArray
onScreenArray[onScreenArray.length-1].push(t);
// remove tile from arrGround to onScreenArray
arrGround[tType].pop();
// place tile on screen
t.y = bottomTile.y + tileWidth;
t.x = bottomTile.x;
addChild(t);
}
} else if (pan == "up"){
onScreenTopRow--;
//////////////////
// remove tile from Bottom
//////////////////
var removedArray:Array = onScreenArray.pop();
for (var i:int = removedArray.length; i > 0; i--){
// t = Tile removed from bottom of column
var t:Tile = removedArray[removedArray.length-1];
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// add Tile from top of column
addChild(t);
t.x = tileNum * offScreenDeckSpacingX * SCALE_UP + offScreenDeckX;
t.y = arrGround[tileNum].length * offScreenDeckSpacingY * SCALE_UP + offScreenDeckY;
removedArray.pop();
}
//add new Top Row
onScreenArray.unshift([]);
// for each col
for (var i:int = 0; i < lenCols; i++){
/////////////
// add tile to Top
/////////////
// topRow = Top tile row
var topRow:int = 0;
// tType = bottom tile Type
var tType:int = complexArray[onScreenTopRow][i+onScreenLeftCol];
// t = Tile to take from arrGround
var t:Tile = arrGround[tType][arrGround[tType].length - 1];
// tile on bottom of pertinent column
var topTile:Tile = onScreenArray[topRow+1][i];
// add tile from arrGround to onScreeArray
onScreenArray[topRow].push(t);
// remove tile from arrGround to onScreenArray
arrGround[tType].pop();
// place tile on screen
t.y = topTile.y - tileWidth;
t.x = topTile.x;
addChild(t);
}
}
// put border back on top
addChild(blockingBorder);
}