AS3:设置文本字段相对于另一个动态文本字段的动态位置

发布于 2024-10-06 12:53:36 字数 3089 浏览 3 评论 0原文

我正在 AS3 中创建一个 XML 驱动的饼图,每个饼片中有 2 个文本框。这两个文本字段都是动态的,因为它们由 XML 文档填充,然后被告知将其放置在 AS3 中的位置。我让它们都使用相同的 x 和 y 位置来放置自己(当然,这将它们放在彼此的顶部),但我想制作其中一个字段(其作用类似于标签)或较大数字和 % 文本字段的标题)将其自身放置在另一个文本字段周围的特定位置。我正在寻找的结果是让较小的“标题”文本字段出现在上方大约 5 个像素处,并左对齐到较大的“百分比”字段。这有道理吗?我对这种类型的代码很糟糕,所以我向你们寻求帮助。并提前致谢。

无论如何,这是我目前得到的两部分代码。第一个块用于“标签”部分,使用良好的旧 x 和 y 方法设置“百分比”文本字段的位置。第二块用于标题,设置较小的“标题”文本的位置。

//evaluate tags
private function evaluateTags():void{

对于 (s=0;s

    //setting the position
    arTags[s].x= -objPie.radius * 
    objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
    arTags[s].y= -objPie.radius *objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2;

    //calculatind corner angles
    var angO1:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x)*180/Math.PI)%360
    var angO2:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x)*180/Math.PI)%360
    var angO3:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x+arTags[s].width)*180/Math.PI)%360
    var angO4:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x+arTags[s].width)*180/Math.PI)%360

    //min angle
    var minO:Number = Math.min(angO1,angO2);
    minO = Math.min(minO,angO3);
    minO = Math.min(minO,angO4);
    minO = minO * Math.PI / 180;

    //max angle
    var maxO:Number = Math.max(angO1,angO2);
    maxO = Math.max(maxO,angO3);
    maxO = Math.max(maxO,angO4);
    maxO = maxO * Math.PI / 180;

    //evaluating if the tag must be showed or not
    if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
    arTags[s].textShowed = false;
    }else{
    arTags[s].textShowed = true;
    }
    } 
    }

//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {

    //setting the position
    arTitles[s].x= -objPie.radius * objTitles.distance*Math.cos(objData.midAngles[s]+Math.PI/3) - arTitles[s].width/2;
    arTitles[s].y= -objPie.radius * objTitles.distance*Math.sin(objData.midAngles[s]+Math.PI/3) - arTitles[s].height/2;

    //calculatind corner angles
    var angO1:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x)*180/Math.PI)%360
    var angO2:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x)*180/Math.PI)%360
    var angO3:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360
    var angO4:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360

    //min angle
    var minO:Number = Math.min(angO1,angO2);
    minO = Math.min(minO,angO3);
    minO = Math.min(minO,angO4);
    minO = minO * Math.PI / 180;

    //max angle
    var maxO:Number = Math.max(angO1,angO2);
    maxO = Math.max(maxO,angO3);
    maxO = Math.max(maxO,angO4);
    maxO = maxO * Math.PI / 180;

    //evaluating if the titles must be shown or not
    if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
    arTitles[s].textShowed = false;
    }else{
    arTitles[s].textShowed = true;
    }
    } 
    }

I'm creating an XML-driven pie chart in AS3 with 2 text boxes in each pie slice. Both text fields are dynamic in the sense that they are populated by the XML doc and then told where to place themselves in the AS3. I've got them both using the same x and y position to place themselves at the moment (which of course puts them right one top of each other), but I'd like to make one of the fields (which acts like a label or a title to the larger number and % text field) place itself in a particular spot around the other text field. The result I'm looking for is to have the smaller "title" text field appear approximately 5 pixels above and left-justified to the larger "percentage" field. Does that make sense? I am awful at this type of code, so I'm looking to you guys for help. And thanks in advance for that.

Anyway, here are two sections of code that I've currently got. The first chunk, for the Tags portion, sets the position for the "percentage" text field using a good old x and y method. The second chunk, for Titles, is setting the position for the smaller "title" text.

//evaluate tags
private function evaluateTags():void{

for (s=0; s

    //setting the position
    arTags[s].x= -objPie.radius * 
    objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
    arTags[s].y= -objPie.radius *objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2;

    //calculatind corner angles
    var angO1:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x)*180/Math.PI)%360
    var angO2:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x)*180/Math.PI)%360
    var angO3:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x+arTags[s].width)*180/Math.PI)%360
    var angO4:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x+arTags[s].width)*180/Math.PI)%360

    //min angle
    var minO:Number = Math.min(angO1,angO2);
    minO = Math.min(minO,angO3);
    minO = Math.min(minO,angO4);
    minO = minO * Math.PI / 180;

    //max angle
    var maxO:Number = Math.max(angO1,angO2);
    maxO = Math.max(maxO,angO3);
    maxO = Math.max(maxO,angO4);
    maxO = maxO * Math.PI / 180;

    //evaluating if the tag must be showed or not
    if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
    arTags[s].textShowed = false;
    }else{
    arTags[s].textShowed = true;
    }
    } 
    }

//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {

    //setting the position
    arTitles[s].x= -objPie.radius * objTitles.distance*Math.cos(objData.midAngles[s]+Math.PI/3) - arTitles[s].width/2;
    arTitles[s].y= -objPie.radius * objTitles.distance*Math.sin(objData.midAngles[s]+Math.PI/3) - arTitles[s].height/2;

    //calculatind corner angles
    var angO1:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x)*180/Math.PI)%360
    var angO2:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x)*180/Math.PI)%360
    var angO3:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360
    var angO4:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360

    //min angle
    var minO:Number = Math.min(angO1,angO2);
    minO = Math.min(minO,angO3);
    minO = Math.min(minO,angO4);
    minO = minO * Math.PI / 180;

    //max angle
    var maxO:Number = Math.max(angO1,angO2);
    maxO = Math.max(maxO,angO3);
    maxO = Math.max(maxO,angO4);
    maxO = maxO * Math.PI / 180;

    //evaluating if the titles must be shown or not
    if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
    arTitles[s].textShowed = false;
    }else{
    arTitles[s].textShowed = true;
    }
    } 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2024-10-13 12:53:36

一切都好,我想通了。解决办法是:

//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {

    //setting the position
    arTitles[s].x= -objPie.radius * objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
    arTitles[s].y= -objPie.radius * objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2 -        arTitles[s].height + 4;

    //calculatind corner angles

All good, I figured it out. The solution is:

//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {

    //setting the position
    arTitles[s].x= -objPie.radius * objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
    arTitles[s].y= -objPie.radius * objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2 -        arTitles[s].height + 4;

    //calculatind corner angles
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文