A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: can not refure to graphic in HUD

  1. #1
    Senior Member
    Join Date
    Mar 2011
    Location
    Riverside ish...
    Posts
    173

    can not refure to graphic in HUD

    I added the function

    Actionscript Code:
    function createHUD() {
    function createHUD() {
        var Hud_Main:Sprite = new Sprite();
        Hud_Main.x=300;
        Hud_Main.y=225;
        Hud_Main.buttonMode=false;
           
        var yaw_baseline:Shape = new Shape();
        yaw_baseline.graphics.lineStyle(0, 0);
        yaw_baseline.graphics.beginFill(0xFFCC00, 1);
        yaw_baseline.graphics.drawRect(0, 50, 250, 1);
        yaw_baseline.graphics.endFill();
       
            var yaw_triangle:Shape = new Shape();
        yaw_triangle.graphics.lineStyle(1, 0x000000);
        yaw_triangle.graphics.beginFill(0xFF0000, 1);
        yaw_triangle.graphics.moveTo(125, 52);
        yaw_triangle.graphics.lineTo(125, 52);
        yaw_triangle.graphics.lineTo(130, 59);
        yaw_triangle.graphics.lineTo(120, 59);
        yaw_triangle.graphics.endFill();

        var yaw_text:TextField = new TextField();
        yaw_text.text="0";
        yaw_text.x=120;
        yaw_text.y=60;
        yaw_text.textColor = 0xFF0000;

        addChild(Hud_Main);
        Hud_Main.addChild(yaw_baseline);
        Hud_Main.addChild(yaw_triangle);
        Hud_Main.addChild(yaw_text);
       
    }

    to start building a basic HUD, bit for some reason can not reference to any of the shapes or the sprite its self without pulling an error...

    It keeps happening even when i try something simple like...

    Actionscript Code:
    function physicsRun() {
        playerYaw*=friction;
        Hud_Main.yaw_text.text=String(playerYaw);
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Did you mean "refer"? I do not see you referring to the graphics in Hud_Main outside of the createHUD function. You are attempting to refer to a TextField, though.

    I'll assume that the double function declaration is a copy/paste error and you don't really have it like that.

    All the variables that you declare inside a function are local to that function. Those variables are not in scope outside that function, even if the object they refer to still exists.

    You need to move the variable declaration of Hud_Main (and probably the rest too) outside the function.

    Adding a child does NOT create a property. Hud_Main does not have a yaw_text property, even though yaw_text is a child of Hud_Main. And since you did not give yaw_text a name (GOOD!), you can't get it with getChildByName either. You need a reference to yaw_text. So, like Hud_Main, move the declaration of yaw_text outside the function so that the yaw_text variable will still be in scope outside the function. Then when referring to it, you do not need to refer to Hud_Main since you already have a reference directly to yaw_text.
    Code:
    var hud_main:Sprite; //variables begin with lowercase by convention
    var yaw_text:TextField;
    
    function createHUD():void{
      //stuff here, do not redeclare the variables above, but do instantiate them.
      //...
    }
    
    function physicsRun() {
        playerYaw*=friction;
        yaw_text.text=String(playerYaw);
    }

  3. #3
    Senior Member
    Join Date
    Mar 2011
    Location
    Riverside ish...
    Posts
    173
    thank you flax im trying to learn conventions and you have been helping me a lot!

    var Hud_Main:Sprite = new Sprite();
    var yaw_baseline:Shape = new Shape();
    var yaw_triangle:Shape = new Shape();
    var yaw_text:TextField = new TextField();

    function createHUD() {
    Hud_Main.x=300;
    Hud_Main.y=225;
    Hud_Main.buttonMode=false;
    yaw_baseline.graphics.lineStyle(0, 0);
    yaw_baseline.graphics.beginFill(0xFFCC00, 1);
    yaw_baseline.graphics.drawRect(0, 50, 250, 1);
    yaw_baseline.graphics.endFill();
    yaw_triangle.graphics.lineStyle(1, 0x000000);
    yaw_triangle.graphics.beginFill(0xFF0000, 1);
    yaw_triangle.graphics.moveTo(125, 52);
    yaw_triangle.graphics.lineTo(125, 52);
    yaw_triangle.graphics.lineTo(130, 59);
    yaw_triangle.graphics.lineTo(120, 59);
    yaw_triangle.graphics.endFill();
    yaw_text.text="0";
    yaw_text.x=120;
    yaw_text.y=60;
    yaw_text.textColor=0xFF0000;

    addChild(Hud_Main);
    this.Hud_Main=Hud_Main;
    Hud_Main.addChild(yaw_baseline);
    Hud_Main.addChild(yaw_triangle);
    Hud_Main.addChild(yaw_text);

    }


    function physicsRun() {
    playerYaw*=friction;
    yaw_text.text=String(Math.floor(playerYaw*100));
    }
    Last edited by YBAB; 01-16-2012 at 01:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center