读书人

AS3的Sprite传入属性的有关问题

发布时间: 2012-03-02 14:40:29 作者: rapoo

AS3的Sprite传入属性的问题
生成一些圆圈,鼠标经过的时候会把对应的文字传进去,鼠标离开的时候关闭mc,效果实现了,但是文字怎么传进去?,请高手指教,还有有没有更好的方法?
代码如下:
var Circle:Object = new Object();
var RectMc:Sprite = new Sprite();
var Mc1:Sprite = new Sprite();
private function display():void
{
var x2 = _config['stageHeight'] - _config['plotArea_margins_bottom'];
var y2 = _config['stageWidth'] - _config['plotArea_margins_right'];

var iY:int,iX:int,i:int=1;
var numY:int = 9,numX:int = 6;//Y个数
var leftY:int = _config['plotArea_margins_left'];
var bottomX:int = x2;
var foo:publicFunction = new publicFunction();
Mc1 = new Sprite();
addChild(Mc1);
for (iX=1; iX <= numX; iX++)
{
for (iY=1; iY <= numY; iY++)
{
var Circle = new drawCircle(20,"008800")
Circle.x=leftY+40,Circle.y=bottomX-40;
addChild(Circle);
leftY = leftY + 85;
Circle['name'] = "Rect"+i;
Circle.addEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
Circle.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
i++;
}
leftY = _config['plotArea_margins_left'];
bottomX = bottomX-80;
}
RectMc = new RectSprite(0xCDE855);
}

private function onMouseOver(e:MouseEvent):void {
var TG:Sprite=e.target as Sprite;
trace(e.currentTarget);
var dataName:String = TG.name;
RectMc.x = 0,RectMc.y=0;
RectMc.labelName = dataName;
Mc1.addChild(RectMc);
}
private function onMouseOut(e:MouseEvent):void {
Mc1.removeChild(RectMc);
removeEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
}
包:
package org.sys
{
import flash.display.Sprite;
import flash.text.TextField;

public class RectSprite extends Sprite
{
private var _label:TextField;
public static var labelName:String = "";
public function RectSprite(color:uint) {
//以下四行使用Sprite内的graphics对象画一个圆角矩形背景
this.graphics.lineStyle(2, 0x85DB18);
this.graphics.beginFill(color);
this.graphics.drawRoundRect(0,0,100,50,10,10);
this.graphics.endFill();
//生成文本框,并将labelName用html设成24号粗体
_label = new TextField();
_label.htmlText = "<font size='24'><b>"+RectSprite.labelName+"</b></font>";
_label.selectable = false;
_label.autoSize = "left";
//将文本框加入RectSprite对象的显示列表
addChild(_label);
}

}
}

[解决办法]
楼主写的代码有点乱,但是我还是觉得思路上也不是很清晰。
充分利用面向对象,你可以用更少的代码做更多的事。而且更清晰。
我看到楼主给的分不少。于是写了个类。不知道是否符合你的要求。
我也比较懒,没加注解。不过总共才90多行,看看吧。
----------调用方法---------------

JScript code
var tempname:int;for (var w:int=0; w<4; w++){    for (var i:int =0; i<5; i++)    {        tempname++;        var temp:mys = new mys();        temp.x = i * 80 + 100;        temp.y = w * 70 + 100;        addChild(temp);        temp.text = "<font size='18'>Hello" + tempname.toString() + "</font>";    }} 

读书人网 >Flash

热点推荐