How to push instantiated MC into Array dynamically?

你。 提交于 2020-01-05 05:09:07

问题


Im really stuck. I have 5 MC´s that are being spliced from one array at a certain time. In that same function I want to push another movieclips into another array. The two arrays holds mc's that represent right or wrong answers. So when one question is being given a correct answer that questions visualisation alters. This function holds a incrementing variable as I do want the mc's to be pushed by the user and one at the time. The thing is I cant seem to refer them properly. I´ve tried

pQuestSum = this[pQuest + pQuestNumber];

and

pQuestSum = this[pQuest] + pQuestNumber;

and pretty much everything I´ve imagined would work...but the problem is I havent tried

the right thing.

when I trace pQuestSum (which would be the reference) I get an error saying thats its not a number.

this is one of 5 mc's named from 1-5:

var passedquest1:PassedQuest = new PassedQuest();

this is the vars that i try to to build a reference of

var pQuest = "passedquest";
var pQuestNumber = 1;
var pQuestSum;
var questCorrArray:Array = [];

if(event.target.hitTestObject(questArray[ix])){
    removeChild(questArray[ix]);
    questArray.splice(ix,1);
    pQuestNumber ++;
    pQuestSum = this[pQuest] + pQuestNumber;
    trace("pQuestSum"); // NaN
    questCorrArray.push(pQuestSum);
    //trace(questArray.length);
    pointsIncreased = false;
        questPoints = 0;
    }

How do I refer an existing movieclip when the reference consists of both a string and a number? Hope I made myself somewhat clear:)


回答1:


If you had an instance of an object on your timeline called "passedquest1" (as an example), then you could access it this way:

var myObj = this["passedquest" + 1];

Or,

var pQuest = "passedquest";
var pQuestNumber = 1;
var myObj = this[pQuest+ pQuestNumber.toString()];

When you do this: pQuestSum = this[pQuest] + pQuestNumber;, you are trying add the number to an object (this[pQuest]), unless you have number/int var called "passedquest", this will result in NaN.



来源:https://stackoverflow.com/questions/16907127/how-to-push-instantiated-mc-into-array-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!