Re: [問題] 如何取得父類別裡的Array索引值

看板Flash作者 (o(‧"‧)o)時間14年前 (2011/04/22 14:13), 編輯推噓2(207)
留言9則, 2人參與, 最新討論串2/3 (看更多)
(感謝cj大) 不好意思 想延伸請教一個疑惑 若照Cj做法取得了Array中的i值 今天若在Vertex寫上TextField讓ROLL OVER出現他的Array編號 有什麼方法能夠寫在Vertex裡且能固定出現文字欄位的位置 我寫在Vertex裡(例如 變數.x=60; 變數.y=80) 這樣顯示出來會以主程式的new 出來的vertex位置為基底的右方60下方80顯示 不會使出現的文字真正是以stage上的x=60;y=80; 來固定顯示 不知前輩們是否了解我的疑問 好像也是父子的關係 我希望是不要用算i的位置加減來固定 如果用亂數來顯示main中的vertex位置 那就會沒加減算 有沒有能夠像寫在MAIN一樣.x y就是那個位置 code就如下 (code和上一篇其實大部份相同); Vertex.as package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; public class Vertex extends Sprite { public var i:int; public var sp:Sprite = new Sprite(); public function Vertex(_color:uint,_size:int):void { sp.graphics.lineStyle(2, 0x000000); sp.graphics.beginFill(_color); sp.graphics.drawCircle(0,0,_size); sp.graphics.endFill(); addChild(sp); addEventListener(MouseEvent.ROLL_OVER, overTest); } private function overTest(e:MouseEvent):void { var word:TextField = new TextField(); word.text = e.currentTarget.parent.vertex[i].i; word.x = 60; word.y = 80; addChild(word); } } } Main.as package { import flash.display.*; import flash.events.*; import Vertex; public class Main extends Sprite { public var vertex:Vector.<Vertex>; private var n:int = 8; public function Main():void { vertex = new Vector.<Vertex>(n, true); for (var i:int = 0; i < n; i++){ vertex[i] = new Vertex(0xFF00FF, 10); if (i == 3){ vertex[i] = new Vertex(0xFF0000, 30); } vertex[i].x = 50 + 60 * i; vertex[i].y = 50 + 30 * i; vertex[i].i = i; addChild(vertex[i]); } } } } 感謝各位 一切還在學習中 謝謝指教 ※ 引述《bruce620 (o(‧"‧)o)》之銘言: 前輩好 我做了小範例來想請教一下各位 今天有兩個as檔 一個為Main 另個為Vertex (故意有用一個sprite包起來) 主程式裡若是使用一個Array來存取Vertex的class 要如何在Vertex的類別中 能夠使滑鼠移上trace出在Main裡丟入Array的索引值 (就是vertex[i]裡的i); (我試過在Main裡宣告一個public 變數 然後在Vertex裡寫.這個變數 但沒試出來) 希望有前輩能指出該如何寫才能trace出那個i值 程式檔範例如下 (內包含FlashDevelop和Flash 版本) http://ppt.cc/u4,8 程式碼我也貼上來請教前輩 感謝各位 !! Vertex.as package { import flash.display.Sprite; import flash.events.MouseEvent; public class Vertex extends Sprite { public var id:int; public var sp:Sprite = new Sprite(); public function Vertex(_color:uint,_size:int):void { sp.graphics.lineStyle(2, 0x000000); sp.graphics.beginFill(_color); sp.graphics.drawCircle(0,0,_size); sp.graphics.endFill(); addChild(sp); addEventListener(MouseEvent.ROLL_OVER, overTest); } private function overTest(e:MouseEvent):void { trace(e.currentTarget.parent.vertex); //[object Vertex] } } } Main.as package { import flash.display.*; import flash.events.*; import Vertex; public class Main extends Sprite { public var vertex:Vector.<Vertex>; private var n:int = 8; public function Main():void { vertex = new Vector.<Vertex>(n, true); for (var i:int = 0; i < n; i++){ vertex[i] = new Vertex(0xFF00FF, 10); if (i == 3){ vertex[i] = new Vertex(0xFF0000, 30); } vertex[i].x = 50 + 60 * i; vertex[i].y = 50 + 30 * i; addChild(vertex[i]); } } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.63.100.169

04/22 07:54,
何不在Vertex class裡面宣告一個var i:uint
04/22 07:54

04/22 07:55,
然後在Main的for loop裡面寫vertex[i].i = i?
04/22 07:55

04/22 10:01,
哦哦!!感謝CJ大!!qq 讓我順利找到之前為何不行!!謝謝Q_Q
04/22 10:01
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.63.100.169 ※ 編輯: bruce620 來自: 61.63.100.169 (04/22 14:13) ※ 編輯: bruce620 來自: 61.63.100.169 (04/22 14:14)

04/22 16:22, , 1F
我看完了 但是我不懂你的問題 我覺得你已經解了問題
04/22 16:22, 1F

04/22 16:24, , 2F
就是讓word.x y能顯示在stage的x:60 y:80
04/22 16:24, 2F

04/22 16:24, , 3F
以現在的寫法run出來會成左上到右下顯示word
04/22 16:24, 3F

04/22 16:24, , 4F
你在外層不要設定 vertex 的座標
04/22 16:24, 4F

04/22 16:25, , 5F
或是把 vertex 改成本身不是顯示物件
04/22 16:25, 5F

04/22 16:26, , 6F
可在main中vertex需要排列他的特定位置@@
04/22 16:26, 6F

04/22 16:26, , 7F
在main層寫 stage.addChild(vertex.text); 之類的
04/22 16:26, 7F

04/22 16:30, , 8F
阿..在vertex寫 stage.addChild(word); 就可以了 囧
04/22 16:30, 8F

04/22 16:30, , 9F
感謝 etrexetrex大 <_ _>"""" Q~Q
04/22 16:30, 9F
文章代碼(AID): #1DiHn-OI (Flash)
文章代碼(AID): #1DiHn-OI (Flash)