看板 Flash 關於我們 聯絡資訊
前輩好 我做了小範例來想請教一下各位 今天有兩個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
cjcat2266:何不在Vertex class裡面宣告一個var i:uint 04/22 07:54
cjcat2266:然後在Main的for loop裡面寫vertex[i].i = i? 04/22 07:55
bruce620:哦哦!!感謝CJ大!!qq 讓我順利找到之前為何不行!!謝謝Q_Q 04/22 10:01