0

My Questions are:

1- I want to link a text to an image, both text and images are in an array. So, when I get the photo randomly from the array, I need to display the text.

2- I want to display the text with the image in the stage, but how to put text in a specific place in stage.

Hope to get feedback soon guys.

Code: Photos = new Array();

        photo2 = new img2();
        photo3 = new img3();
        photo4 = new img4();
        //put those images into an array
        Photos.push(photo2);            
        Photos.push(photo3);
        Photos.push(photo4);


        //photo1.personDetails;
        //photo1.imageName;
        compass1.addEventListener(MouseEvent.CLICK, addPhoto); 



        //http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/arrays.html
        var elements:Array = [];
        elements[ 0 ] = [ "A", "B", "C" ];
        elements[ 1 ] = [ "D", "E", "F" ];
        elements[ 3 ] = [ "G", "H", "I" ];
        trace( elements[ 0 ][ 0 ] ); //A

    }

// Pick random Element of an Array Actionscript 3

        function getRandomElementOf(Photos:Array):Object {
            var idx:int=Math.floor(Math.random() * Photos.length);
            trace(idx);
            return Photos[idx];
        }


    public function addPhoto(e:MouseEvent) {
        trace("clicked compass");


        stage.addChild(DisplayObject(getRandomElementOf(Photos)));


    }
4
  • you could use a TextField object to create a text field then fill it with the text and add it to the stage like a display object. Any formatting can be mostly done with a TextFormat object and applied to the TextField object Commented Sep 17, 2015 at 7:13
  • Okay I got that, will try it. But am not sure how to link the text field to an image in the array list ..? which is randomly chosen @mitim Commented Sep 17, 2015 at 7:29
  • There are multiple ways to do this. You could store the text fields in another array where the indexes match (for example index 1 in 'Photos' array matches to index 1 in a 'TextFields' array) or just store the string only and create the text fields only when you add the image to the stage. Or store both the text and image together in an object and store those in an array...and so on. Commented Sep 17, 2015 at 7:37
  • You can use multidimensional arrays. At one dimension you keep images and other the text vice versa. Here is a help page about the arrays. link Commented Sep 17, 2015 at 8:50

1 Answer 1

1

make an array of what you want displayed, not just the photos.

for (var iPhoto:int = 0; iPhoto < Photos.length; ++iPhoto)
{

    var vector:Vector.<Sprite>() = new Vector.<Sprite>();
    var sprite:Sprite = new Sprite(); // create a "holder" for the images you want
    var textField:TextField = new TextField();
    var photo:Photo = Photos[iPhoto];
    sprite.addChild(photo);
    sprite.addChild(textField);
}

something like this. then you can choose randomly choose a sprite that has all of the things you want as children.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.