lundi 26 septembre 2016

as3 dynamic TextField words from array placed into individual MovieClips into a sentence

I am trying to create a dynamic sentence from an array of words. The words need to be placed next to each other in a sentence. I need the words to be in a movie clip so I can drag them as well.

Below is the code I am using but I am having many issues. Any ideas?

package com.sentances {

    import flash.display.MovieClip;
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;


    public class Main_Sentances extends MovieClip {


        public var Sentance: Array;
        public var myText: TextField;
        public var myMovie: MovieClip;

        public var Words: Array = new Array("This", "is", "my", "sentance.");

        public function Main_Sentances() {
            // constructor code

            MakeWords();
        }


        public function MakeWords() {
            var xSpot = 0;
            for (var i = 0; i < Words.length; i++) {
                //trace(Words[i]);

                myMovie = new MovieClip();
                myMovie.name = Words[i];
                addChild(myMovie);

                myText = new TextField();
                myText.selectable = false;
                myText.border = true;
                myText.defaultTextFormat = new TextFormat('Verdana', 50, 0xffffff);
                myText.text = Words[i];

                //trace(myText.name);

                myText.addEventListener(Event.ADDED_TO_STAGE, handleChng);
                myMovie.addChild(myText);               







                function handleChng(e: Event): void {
                    trace("childMC ADDED_TO_STAGE");
                        trace(myText.textWidth);

                        if(i==0)
                        {
                        myMovie.x = 0;
                        myMovie.width = myText.textWidth;
                        myMovie.height = myText.textHeight;
                        xSpot = myText.textWidth;
                        }
                        else
                        {
                        myMovie.width = myText.textWidth;
                        myMovie.height = myText.textHeight;
                        xSpot = xSpot + myText.textWidth;
                        myMovie.x = xSpot;
                        }
                }

            }




        }


        // End Package  
    }
} 

Aucun commentaire:

Enregistrer un commentaire