Re: [問題] 如何取得父類別裡的Array索引值
(感謝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,
04/22 07:54
→
04/22 07:55,
04/22 07:55
→
04/22 10:01,
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
04/22 16:24, 2F
→
04/22 16:24, , 3F
04/22 16:24, 3F
推
04/22 16:24, , 4F
04/22 16:24, 4F
→
04/22 16:25, , 5F
04/22 16:25, 5F
→
04/22 16:26, , 6F
04/22 16:26, 6F
→
04/22 16:26, , 7F
04/22 16:26, 7F
→
04/22 16:30, , 8F
04/22 16:30, 8F
→
04/22 16:30, , 9F
04/22 16:30, 9F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 3 篇):