A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Adding Sprites and scrolling them

Hybrid View

  1. #1
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57

    Adding Sprites and scrolling them

    Hi all,
    I just started learning AS3 I was just trying out something.To read values from xml and make a drop down menu. I reached only this far

    Actionscript Code:
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;

    var i:Number = 0;
    var sp:Sprite = new Sprite();
    var sptxt:TextField = new TextField();
    stage.addEventListener(MouseEvent.MOUSE_DOWN,addMore);

    function addMore(e:MouseEvent):void {
        sp.name = "sp"+i;
        sptxt.name = "sptxt"+i;
        this.y+=10;
        //sptxt.addEventListener(MouseEvent.CLICK,EditMe);
        sptxt.text="this is the "+i+"th text";
        addChild(sp);
        sp.addChild(sptxt);
        trace("sp="+sp.name);
        trace("sptxt="+sptxt.name);
        trace("i="+i);
        i++;
    }
    /*
    function EditMe():void {
        sp.sptxt.type=TextFieldType.input();
    }
    */

    What I did is that,When you click on the stage It will add a new spirte, sp with name sp0,sp1 ... and adds sptxt0,sptxt1... inside sp0,sp1... respectively.I put
    Actionscript Code:
    this.y+=10;
    so that each sp will come along down like a drop down.

    And
    Actionscript Code:
    EditMe()
    is a function to make the text field in sp editable.Like if i click sp0,its text,ie sp0.sptxt0 must become editable.But its not working,am getting error as Access to undefined property

    Another problem is that am getting output as "this is 0th text" then when i click again,it erases all things and adds "this is 1th text" at y+=0.Its not showing "this is 0th text" above that line. ...

    Please help.

    Btw One more thing.I think I know how to read XML values into those sptxt0,sptxt1 etc,but how can i make them scroll? Using the Scroll() on sp? or another container is needed?
    The CodeBreaker!

  2. #2
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    Nobody knows?THen I am happy to share my version of the solution

    Actionscript Code:
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;

    var i:Number = 0;
    var inc:Number = 10;
    var sp:Sprite = new Sprite();
    var sptxt:TextField = new TextField();
    stage.addEventListener(MouseEvent.MOUSE_DOWN,addMore);

    function addMore(e:MouseEvent):void {
        this['sp'+i] = new Sprite();
        this['sptxt'+i] = new TextField();
        this['sptxt'+i].text="this is the "+i+"th text";
        this['sptxt'+i].y+=inc;
        this['sp'+i].addEventListener(MouseEvent.MOUSE_DOWN,EditMe);
        addChild(this['sp'+i]);
        this['sp'+i].addChild(this['sptxt'+i]);
        trace("sp="+this['sp'+i].name);
        trace("sptxt="+this['sptxt'+i].text);
        trace("i="+i);
        trace("x="+this['sptxt'+i].x);
        i++;
        inc += 10;
    }

    function EditMe(e:MouseEvent):void {
        this["sptxt"+i].type=TextFieldType.input();
    }

    Although I was able to create sprites and texts inside it,I am not able to refer to the textField to the corresponding sprite in EditMe()
    Actionscript Code:
    function EditMe(e:MouseEvent):void {
        this["sptxt"+i].type=TextFieldType.input();
    }
    Anyone knows that?
    The CodeBreaker!

  3. #3
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    If I push all the sprites into an array, say
    Actionscript Code:
    var a:Array = new Array();
    a.push(this['sp'+i]);
    How can i refer to the text field inside 'sp'+i ?(ie 'sp'+i . 'sptxt'+i ?)
    The CodeBreaker!

  4. #4
    Member
    Join Date
    Sep 2007
    Posts
    48
    Instead of doing this:
    <code>
    this['sp'+i] = new Sprite();
    </code>
    why don't you do something like this:
    <code>
    var sprite_array = new Array();
    sprite_array[0]= new Sprite();
    </code>

  5. #5
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    ah glad that you saw my mail...
    If i do like that,how will i refer to its child text field?Like if user clicks sprite_array[0],
    how will i know that its sprite_array[0] and can access
    sprite_array[0].text_array[0]?

    Anyway to return array index?
    The CodeBreaker!

  6. #6
    Member
    Join Date
    Sep 2007
    Posts
    48
    in the listener function you can write something like
    Actionscript Code:
    e.target.text_array[0];
    assuming "e" refers to the event object which is passed to the listener as an argument.

  7. #7
    THE YELLOW FLASH!
    Join Date
    Feb 2008
    Posts
    57
    But if we are not sure if its 0 or anything else?To return the array index can we use
    Actionscript Code:
    getIndex()
    ?
    like
    Actionscript Code:
    location = sprite_array.getIndex(e.target);
    sprite_array[location].text_array[location];

    Will that select sprite_array[8].text_array[8]; if i click on the 8th sprite?
    The CodeBreaker!

  8. #8
    Member
    Join Date
    Sep 2007
    Posts
    48
    Yep, hopefully.

Tags for this Thread

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