作者bruce620 (o(‧"‧)o)
看板Flash
标题Re: [问题] 如何取得父类别里的Array索引值
时间Fri Apr 22 14:13:15 2011
(感谢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
1F:推 cjcat2266:何不在Vertex class里面宣告一个var i:uint 04/22 07:54
2F:→ cjcat2266:然後在Main的for loop里面写vertex[i].i = i? 04/22 07:55
3F:→ bruce620:哦哦!!感谢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)
4F:推 etrexetrex:我看完了 但是我不懂你的问题 我觉得你已经解了问题 04/22 16:22
5F:→ bruce620:就是让word.x y能显示在stage的x:60 y:80 04/22 16:24
6F:→ bruce620:以现在的写法run出来会成左上到右下显示word 04/22 16:24
7F:推 etrexetrex:你在外层不要设定 vertex 的座标 04/22 16:24
8F:→ etrexetrex:或是把 vertex 改成本身不是显示物件 04/22 16:25
9F:→ bruce620:可在main中vertex需要排列他的特定位置@@ 04/22 16:26
10F:→ etrexetrex:在main层写 stage.addChild(vertex.text); 之类的 04/22 16:26
11F:→ bruce620:阿..在vertex写 stage.addChild(word); 就可以了 囧 04/22 16:30
12F:→ bruce620:感谢 etrexetrex大 <_ _>"""" Q~Q 04/22 16:30