我對flash和actionscript 3很新穎。我一直在閱讀很多,這也是我第一次面向對象編程。我正在寫AS3嗎?
到目前爲止,我使用登錄按鈕創建了一個應用程序,就這些。但是,我想知道我做錯了什麼樣的事情,或者應該做什麼不同(或更好)。我使用Adobe的Flex Builder 3
主要ActionScript文件是Client2.as:
package
{
//import required libraries
import flash.display.Sprite;
//set project properties
[SWF(width="800", height="600", frameRate="31", backgroundColor="#C0C0C0")]
//launch main class
public class Client2 extends Sprite
{
public function Client2() { //the constructor
trace("Client launched.");
var loginGui:LoginInterface = new LoginInterface(); //load the login interface object
loginGui.init(); //initialize the login interface (load it)
addChild(loginGui); //add login gui to the display tree
}
}
}
它加載登錄界面對象。這是一件好事,我正確地做這件事嗎?
然後有LoginInterface.as類文件:
package
{
//import required libraries
import flash.display.Sprite;
//the LoginInterface class
public class LoginInterface extends Sprite
{
public function LoginInterface() //the constructor
{
trace("LoginInterface object loaded.");
}
public function init():void //initialize the login interface (load it)
{
trace("LoginInterface init method was called.");
var loginButton:CustomButton = new CustomButton(300, 300, 100, 30, 3, 18, "Login!"); //create a new custom button
addChild(loginButton); //add the custom button to the display tree
}
}
}
怎麼樣?任何意見?爲了使創建簡單的按鈕更容易一點,我則創造了另一個名爲CustomButton.as類文件 - >
package
{
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomButton extends Sprite
{
public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String)
{
//create new simple button instance
var myButton:SimpleButton = new SimpleButton();
//create the look of the states
var normal:Sprite = new Sprite();
normal.graphics.lineStyle(1, 0x000000);
normal.graphics.beginFill(0x6D7B8D);
normal.graphics.drawRect(xLoc, yLoc, width, height);
//the mouseover sprite
var over:Sprite = new Sprite();
over.graphics.lineStyle(1, 0x000000);
over.graphics.beginFill(0x616D7E);
over.graphics.drawRect(xLoc, yLoc, width, height);
// assign the sprites
myButton.upState = normal;
myButton.downState = normal;
myButton.hitTestState = normal;
myButton.overState = over;
//add the button to the display tree
addChild(myButton);
//create button label
var tText:TextField = new TextField();
tText.mouseEnabled = false,
tText.x = xLoc;
tText.y = yLoc + iLabelOffset;
tText.width = width;
tText.selectable = false
var Format:TextFormat = new TextFormat();
Format.font = "Arial";
Format.color = 0x000000;
Format.size = fontsize;
Format.bold = false;
Format.align = TextFormatAlign.CENTER;
tText.defaultTextFormat = Format;
tText.text = label;
addChild(tText)
}
}
}
有什麼對此發表評論?我確信我做了很多錯誤的事情,也許我沒有真正瞭解整個面向對象的事情?另外,對於我在類聲明後使用「extends ...」的方式我有一種不好的感覺,主要是因爲我一直在使用Sprite,並不真正瞭解它爲什麼或者它做了什麼(遇到問題也在互聯網上找到)。我不確定的另一件事是AS3中變量的命名。我真的應該使用xLoc還是iLabelOffset?我認爲我的變量atleast命名不太一致?
我希望有人能給我一個比現在更好的軌道,因爲我相信我應該在繼續研究這個野獸之前改進我的AS3編碼。
非常感謝。
很高興看到幀率31神話活着和踢! – spender 2009-06-23 23:15:21