1

i'm having some problems with as3

var eventChildren:XMLList = eventInput.channel.children();
var nr:Number;
nr=0;
for each (var eventInfo:XML in eventChildren) {

    nr++;
    trace(eventInfo.title);
    var ev="ev"+String(nr);
    var titl="title"+String(nr);
    trace(ev);
    trace(titl);

    var newEV:Object = Object(ev);
    var newTITL:Object = Object(titl);

    trace(newEV);
    trace(newTITL);

    newEV.newTITL.text=eventInfo.title;

}

}

this is my code, i'm trying to set the title value for every child instance of eventChild, as i am new to action script in general, and action script 3 in particular i don't really know what i'm doing wrong here. I'm trying to set the text for ev1.title1, ev2.title2, etc. from values in eventChildren like this : first child, sets ev1.title1, second ev2.title2 and so on. Any ideas on what i should change in the code or where to look for some information ?

edit : thank you for the help, both answers took me to the right solution :

for each (var eventInfo:XML in eventChildren) {

    nr++;

    trace(eventInfo.title);
    var ev="ev"+String(nr);
    var titl="title"+String(nr);
    //trace(ev);
    //trace(titl);

    var oTitle:Object = {}; // create object for the field titleXX
    oTitle[titl] = {text:eventInfo.title}; // create and assign the field text to a new object
    allFields[ev] = oTitle; // assign the title object to the field evXX

 }

ev1.title1.text=allFields.ev1.title1.text;
ev2.title2.text=allFields.ev2.title2.text;
ev3.title3.text = allFields.ev3.title3.text;
ev4.title4.text=allFields.ev4.title4.text;

2 Answers 2

2

ev and titl are Strings and not Object, there is no eval in as3 so you will not be able to create a new variable based on a string name .But you can create a new Object that will have a field based on your ev string:

var o:Object={};
o[ev]="....";

So if ev is equal to the string "ev1" you will have an object with a field named ev1 => o.ev1=...

For the title you can do the same create a new Object that will have a field based on titl string:

var o:Object={};
o[titl]="...";

So if titl is equal to the string "title1" you will have an object with a field named title1 => o.title1=...

Same thing for the text you have to create an Object to hold the text field.

Mixing all this infos you end up with:

var eventChildren:XMLList = eventInput.channel.children();
var nr:Number=0;
var AllFields:Object={};

for each (var eventInfo:XML in eventChildren) {
    nr++;
    trace(eventInfo.title);
    var ev="ev"+String(nr);
    var titl="title"+String(nr);
    trace(ev);
    trace(titl);

    var oTitle:Object = {}; // create object for the field titleXX
    oTitle[titl] = {text:eventInfo.title}; // create and assign the field text to a new object
    allFields[ev] = oTitle; // assign the title object to the field evXX
 }

// then you can have access to all your field within the object allFields
trace(allFields.ev1.title1.text)
trace(allFields.ev2.title2.text)

See also this question for object notation

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

Comments

2

You can create the variable name using 'this':

this['mystring'] = new Object();
this.mystring.title = 'mytitle';

If you're doing this inside a class, the class has to be dynamic to allow for new members:

dynamic public class MyClass extends MovieClip {
    public function MyClass() {
        this['mystring'] = new Object();
        this.mystring.title = 'mytitle';
    }
}

If your class is not dynamic, you can still do this but must continue to use array notation rather than dot notation:

public class MyClass extends MovieClip { // not dynamic
    public function MyClass() {
        this['mystring'] = new Object();
        this['mystring'].title = 'mytitle';
    }
}

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.