The issue I’m having is rather for the sake of optimizing my code. While I’m still new to AS3, I guess I have troubles understanding some basic concepts. Anyway, here’s the scenario:
It is from a game where the user picks a crop, either apples, corn or dairy. Based on that selection, the user will be able to play a matching game with a bunch of objects moving around. Those three main objects (apple, corn, dairy) are just containers, a movieclip that holds several different subobjects, like gala apple, golden apple, etc. With for-loops, I’m looping through the main object to place the subobjects on stage. This would look like this;
/**
* Depending on crop picked, bring the appropriate objects on stage.
*/
switch (_mainTimeline.cropPick) {
case "Apple" :
var matchObjectSilo:ApplesSilo = new ApplesSilo;
matchObjectSilo.gotoAndStop(_objectToPick);
matchObjectSilo.x = _CROP_SILO_X_POS + (h * _CROP_SILO_X_SPACING);
matchObjectSilo.y = _CROP_SILO_Y_POS;
var colorTransform:ColorTransform = matchObjectSilo.transform.colorTransform;
colorTransform.color = 0xC7C7C7;// color them grey
matchObjectSilo.transform.colorTransform = colorTransform;
addChild(matchObjectSilo);
break;
case "Corn" :
var matchObjectSilo:CornSilo = new CornSilo;
matchObjectSilo.gotoAndStop(_objectToPick);
matchObjectSilo.x = _CROP_SILO_X_POS + (h * _CROP_SILO_X_SPACING);
matchObjectSilo.y = _CROP_SILO_Y_POS;
var colorTransform:ColorTransform = matchObjectSilo.transform.colorTransform;
colorTransform.color = 0xC7C7C7;// color them grey
matchObjectSilo.transform.colorTransform = colorTransform;
addChild(matchObjectSilo);
break;
case "Dairy" :
var matchObjectSilo:DairySilo = new DairySilo;
matchObjectSilo.gotoAndStop(_objectToPick);
matchObjectSilo.x = _CROP_SILO_X_POS + (h * _CROP_SILO_X_SPACING);
matchObjectSilo.y = _CROP_SILO_Y_POS;
var colorTransform:ColorTransform = matchObjectSilo.transform.colorTransform;
colorTransform.color = 0xC7C7C7;// color them grey
matchObjectSilo.transform.colorTransform = colorTransform;
addChild(matchObjectSilo);
break;
}
Although this works perfectly fine, for example purposes I only posted a couple lines out of the actual things that would need to happen in each case-statement. So rather than having some 50 lines of code per statement, where only one line is really different (first one), I would like to have only that one line in the case and the for-loop separate.
The thing that I don’t understand now, how would I create a new (dynamic) object inside that for-loop, that would represent the one instantiated in the case-statement?
Here’s what I’ve tried so far, which sorta worked but not quite; Copying.
switch (_mainTimeline.cropPick) {
case "Apple" :
var initObjectSilo:ApplesSilo = new ApplesSilo;
break;
case "Corn" :
var initObjectSilo:CornSilo = new CornSilo;
break;
case "Dairy" :
var initObjectSilo:DairySilo = new DairySilo;
break;
}
for (var h:uint = 0; h < _MATCHES; h++) {
var matchObjectSilo:Object = new Object();
matchObjectSilo = copy(initObjectSilo);
matchObjectSilo.gotoAndStop(_objectToPick);
matchObjectSilo.x = _CROP_SILO_X_POS + (h * _CROP_SILO_X_SPACING);
matchObjectSilo.y = _CROP_SILO_Y_POS;
var colorTransform:ColorTransform = matchObjectSilo.transform.colorTransform;
colorTransform.color = 0xC7C7C7;// color them grey
matchObjectSilo.transform.colorTransform = colorTransform;
addChild(matchObjectSilo);
}
public function copy(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
This throws a compile time error, at the line where it tries to access the first property;
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
probably due to the fact that through the copying process, it “forgets” what kind of class/object it was before. “registerClassAlias” is supposed to take care of this but I can’t use it in my case, as I can’t hardcode anything in the for-loop that references the original class. Since there are three original classes (apple, corn, dairy), this has to be dynamic.
Other option I tried, did work but only half way: Array.
switch (_mainTimeline.cropPick) {
case "Apple" :
var initObjectSilo:ApplesSilo = new ApplesSilo;
break;
case "Corn" :
var initObjectSilo:CornSilo = new CornSilo;
break;
case "Dairy" :
var initObjectSilo:DairySilo = new DairySilo;
break;
}
for (var h:uint = 0; h < _MATCHES; h++) {
var matchObjectSilo:Array = new Array();
matchObjectSilo.push(initObjectSilo);
matchObjectSilo[0].gotoAndStop(_objectToPick);
matchObjectSilo[0].x = _CROP_SILO_X_POS + (h * _CROP_SILO_X_SPACING);
matchObjectSilo[0].y = _CROP_SILO_Y_POS;
var colorTransform:ColorTransform = matchObjectSilo[0].transform.colorTransform;
colorTransform.color = 0xC7C7C7;// color them grey
matchObjectSilo[0].transform.colorTransform = colorTransform;
addChild(matchObjectSilo[0]);
}
This compiles and no runtime error but it only adds one object to the stage, the one from the last loop. Despite the “new” operator, it just seems to only reference once to the original class and does not create multiple instances…
Again, maybe I’m missing some basic concepts here and there is a much easier solution. I would appreciate any suggestions, on how to create a new dynamic instance of an object inside the for-loop. TIA.