Re: [問題] 如何在物件內控制舞台的元件?
※ 引述《woodymonkey (小罐的吃太快)》之銘言:
: 我在舞台上放了一些圖、文字 (沒有轉換成元件),
: 例如放了一個textfield,在屬性那裡命名為 TextA。
: 然後建立一個 A class。
: public class A {
: public function A() {
: // constructor
: }
: public function hideText():void {
: TextA.visible = false;
: }
: }
: 我把程式碼寫在時間軸上:
: var objA:A = new A();
: objA.hideText();
: 但是執行後卻顯示:存取未定義的屬性…
: 我想問說如何在物件裡控制主舞台上的"東西",
: 將 TextA 轉換為元件也是一樣的結果…
: parent 或 root 也試不出正確的用法…
: 請問該如何解決呢?
: 謝謝。
首先要先懂什麼是 Document Class
看圖說故事 http://goo.gl/8t7kM
取得場景元件的方式為:
public class A
{
/*
這邊一定要用 public 來修飾,private, protected 都會出錯
物件名稱就跟你在場景的物件名稱一樣
這樣就可以直接使用了
*/
public var TextA:TextField;
public function A()
{
// 假設要在第10格讓TextA隱藏
addFrameScript(9, hideText);
}
public function hideText():void
{
TextA.visible = false;
}
}
而且你要在影格寫程式碼存取 TextA
就直接寫 TextA.visible = false;
就好拉~ 不用大費周章還要寫 Document Class
且也不推薦程式碼在影格和 Document Class 都有寫
會造成日後維護困難
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.33.232.7
推
11/30 00:02, , 1F
11/30 00:02, 1F
→
11/30 00:05, , 2F
11/30 00:05, 2F
→
11/30 00:07, , 3F
11/30 00:07, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):