Copy link to clipboard
Copied
I have an array of capital letters A-Z.
I am adding the child to the stage with a discrete button representing the letter of the alphabet.
I can add different letters to the stage up to 4 letters (I have an 'if' condition limiting the number of children on the stage). However if I try to add 4 of the SAME letter, the first gets added, the second gets added and the first deleted, the third gets added and the second deleted (and the first has already been deleted) and so forth. So I can never have the same letters like if I want to spell out MMMM as an expression of sentiment.
How can this be? I think I have used a similar method before in a previous prototype where new children were loading onto the stage from an image file on disk. In this case all of the children are embedded MovieClips in the FLA file (the FLA is huge!... because I have about 10 types of lettering images for each letter of the alphabet). And I am calling those with Class constructors and pushing the Classes into an array, then calling that array in a for loop iterating from 0 to less than 27 with i being a uint.
Any help is greatly appreciated.
-markerline
:
// initialize
var wordDisplayA:Array = [];
var btnA:Array=[btn_A,btn_B,..];
for(var i:int=0;i<btnA.length;i++){
btnA.addEventListener(MouseEvent.CLICK,letterDisplayF);
}
// use wordDisplayA elements to remove objects from stage and null them.
// can tweak but don't change significantly
function letterDisplayF(e:MouseEvent):void{
var C:Class=Class(getDefinitionByName("Alpha_01_"+e.currentTarget.name.split("btn_")[1].toUpperCase()));
var instance:MovieClip=new C();
addChild(instance);
wordDisplayA.push(inst
...Copy link to clipboard
Copied
What does the code look like?
Copy link to clipboard
Copied
Here's my code:
Ned Murphy wrote:
What does the code look like?
var A_01:MovieClip=new Alpha_01_A();
var B_01:MovieClip=new Alpha_01_B();
var C_01:MovieClip=new Alpha_01_C();
var D_01:MovieClip=new Alpha_01_D();
var E_01:MovieClip=new Alpha_01_E();
var F_01:MovieClip=new Alpha_01_F();
var G_01:MovieClip=new Alpha_01_G();
var H_01:MovieClip=new Alpha_01_H();
var I_01:MovieClip=new Alpha_01_I();
var J_01:MovieClip=new Alpha_01_J();
var K_01:MovieClip=new Alpha_01_K();
var L_01:MovieClip=new Alpha_01_L();
var M_01:MovieClip=new Alpha_01_M();
var N_01:MovieClip=new Alpha_01_N();
var O_01:MovieClip=new Alpha_01_O();
var P_01:MovieClip=new Alpha_01_P();
var Q_01:MovieClip=new Alpha_01_Q();
var R_01:MovieClip=new Alpha_01_R();
var S_01:MovieClip=new Alpha_01_S();
var T_01:MovieClip=new Alpha_01_T();
var U_01:MovieClip=new Alpha_01_U();
var V_01:MovieClip=new Alpha_01_V();
var W_01:MovieClip=new Alpha_01_W();
var X_01:MovieClip=new Alpha_01_X();
var Y_01:MovieClip=new Alpha_01_Y();
var Z_01:MovieClip=new Alpha_01_Z();
var alphaArr:Array=new Array(A_01, B_01, C_01, D_01,
E_01, F_01, G_01, H_01,
I_01, J_01, K_01, L_01,
M_01, N_01, O_01, P_01,
Q_01, R_01, S_01, T_01,
U_01, V_01, W_01, X_01,
Y_01, Z_01);
var C1:Class;
var C2:Class;
var C3:Class;
var C4:Class;
var C5:Class;
var C6:Class;
var C7:Class;
var C8:Class;
var C9:Class;
var C10:Class;
var C11:Class;
var C12:Class;
var C13:Class;
var C14:Class;
var C15:Class;
var C16:Class;
var C17:Class;
var C18:Class;
var C19:Class;
var C20:Class;
var C21:Class;
var C22:Class;
var C23:Class;
var C24:Class;
var C25:Class;
var C26:Class;
C1=Class(getDefinitionByName("Alpha_01_A"));
C2=Class(getDefinitionByName("Alpha_01_B"));
C3=Class(getDefinitionByName("Alpha_01_C"));
C4=Class(getDefinitionByName("Alpha_01_D"));
C5=Class(getDefinitionByName("Alpha_01_E"));
C6=Class(getDefinitionByName("Alpha_01_F"));
C7=Class(getDefinitionByName("Alpha_01_G"));
C8=Class(getDefinitionByName("Alpha_01_H"));
C9=Class(getDefinitionByName("Alpha_01_I"));
C10=Class(getDefinitionByName("Alpha_01_J"));
C11=Class(getDefinitionByName("Alpha_01_K"));
C12=Class(getDefinitionByName("Alpha_01_L"));
C13=Class(getDefinitionByName("Alpha_01_M"));
C14=Class(getDefinitionByName("Alpha_01_N"));
C15=Class(getDefinitionByName("Alpha_01_O"));
C16=Class(getDefinitionByName("Alpha_01_P"));
C17=Class(getDefinitionByName("Alpha_01_Q"));
C18=Class(getDefinitionByName("Alpha_01_R"));
C19=Class(getDefinitionByName("Alpha_01_S"));
C20=Class(getDefinitionByName("Alpha_01_T"));
C21=Class(getDefinitionByName("Alpha_01_U"));
C22=Class(getDefinitionByName("Alpha_01_V"));
C23=Class(getDefinitionByName("Alpha_01_W"));
C24=Class(getDefinitionByName("Alpha_01_X"));
C25=Class(getDefinitionByName("Alpha_01_Y"));
C26=Class(getDefinitionByName("Alpha_01_Z"));
var alpha2Arr:Array=new Array();
alpha2Arr.push(new C1());
alpha2Arr.push(new C2());
alpha2Arr.push(new C3());
alpha2Arr.push(new C4());
alpha2Arr.push(new C5());
alpha2Arr.push(new C6());
alpha2Arr.push(new C7());
alpha2Arr.push(new C8());
alpha2Arr.push(new C9());
alpha2Arr.push(new C10());
alpha2Arr.push(new C11());
alpha2Arr.push(new C12());
alpha2Arr.push(new C13());
alpha2Arr.push(new C14());
alpha2Arr.push(new C15());
alpha2Arr.push(new C16());
alpha2Arr.push(new C17());
alpha2Arr.push(new C18());
alpha2Arr.push(new C19());
alpha2Arr.push(new C20());
alpha2Arr.push(new C21());
alpha2Arr.push(new C22());
alpha2Arr.push(new C23());
alpha2Arr.push(new C24());
alpha2Arr.push(new C25());
alpha2Arr.push(new C26());
//addChild(alphaArr[0]);
var alphaNewArr:Array=new Array(grid_contain.A_ltr, grid_contain.B_ltr, grid_contain.C_ltr, grid_contain.D_ltr,
grid_contain.E_ltr, grid_contain.F_ltr, grid_contain.G_ltr, grid_contain.H_ltr,
grid_contain.I_ltr, grid_contain.J_ltr, grid_contain.K_ltr, grid_contain.L_ltr,
grid_contain.M_ltr, grid_contain.N_ltr, grid_contain.O_ltr, grid_contain.P_ltr,
grid_contain.Q_ltr, grid_contain.R_ltr, grid_contain.S_ltr, grid_contain.T_ltr,
grid_contain.U_ltr, grid_contain.V_ltr, grid_contain.W_ltr, grid_contain.X_ltr,
grid_contain.Y_ltr, grid_contain.Z_ltr);
var numNewArr:Array=new Array(grid_contain.n0_ltr, grid_contain.n1_ltr, grid_contain.n2_ltr, grid_contain.n3_ltr, grid_contain.n4_ltr,
grid_contain.n5_ltr, grid_contain.n6_ltr, grid_contain.n7_ltr, grid_contain.n8_ltr, grid_contain.n9_ltr);
var sliderArr:Array=new Array(A_slider, B_slider, C_slider, D_slider,
E_slider, F_slider, G_slider, H_slider,
I_slider, J_slider, K_slider, L_slider,
M_slider, N_slider, O_slider, P_slider,
Q_slider, R_slider, S_slider, T_slider,
U_slider, V_slider, W_slider, X_slider,
Y_slider, Z_slider);
function init():void{
for each (var disp:MovieClip in alphaArr){
disp.stop();
disp.gotoAndStop(alpha_slider.value);
//disp.visible=false;
}
for each (var dispB:MovieClip in alpha2Arr){
dispB.stop();
dispB.gotoAndStop(alpha_slider.value);
//disp.visible=false;
}
for each (var dispA:MovieClip in alphaNewArr){
dispA.addEventListener(MouseEvent.CLICK, addEvent);
}
for each (var slid:Slider in sliderArr){
slid.addEventListener(Event.ENTER_FRAME, onEnterFrameEventSlider);
}
for (var k:uint=0;k<27;k++){
//alphaNewArr
.addEventListener(MouseEvent.CLICK, addEvent); //alphaNewArr
.name=k; }
}
init();
function onEnterFrameEventSlider(e:Event):void{
for each (var disp3:MovieClip in alphaArr){
disp3.gotoAndStop(alpha_slider.value);
}
for (var l:uint=0; l<27; l++){
if(alphaArr
){ alphaArr
.gotoAndStop(sliderArr .value); }
}
}
var offset:Number=450/2;
var locX:Number=200*2;
var numMCs:int=0;
var tempSprArr:Array=new Array();
var scale=.5;
function addEvent(e:MouseEvent):void{
trace(e);
trace(e.target.name);
for (var i:uint=0; i<27; i++){
if(e.target.parent==alphaNewArr){
trace(e.target);
var tempSpr:Sprite = new Sprite();
//var tempSpr:Sprite = new Sprite();
tempSpr.addChild(new C());
tempSprArr.push(tempSpr);
tempSpr.x=locX+offset*numMCs;
//tempSpr.x=0;
tempSpr.y=stage.stageHeight/2;
alphaArr.visible=true;
//addChild(alphaArr);
addChild(tempSpr);
tempSpr.scaleX=tempSpr.scaleY=scale;
}
}
numMCs++;
if(numMCs>4){
for each(var tDisp:DisplayObject in tempSprArr){
removeChild(tDisp);
}
tempSprArr=[];
numMCs=0;
}
}
The problem with trying to implement kglad's solution is that it would require a text field input to input the String that he is using, rather than pressing a button on the stage. This application is meant to be used on a tablet, and using the keyboard is not as fast as pressing an existing button on the stage.
KGlad, how do I modify your code to implement my strategy with buttons?
-markerline
Copy link to clipboard
Copied
if you only have one M you wouldn't expect to display two or more at any one time. if you need more than one M, create more than one M.
and you might want to rethink your setup. instead of adding class instances to your array, assuming your class names are "A","B" etc, you should probably use something like:
// initialize
var wordDisplayA:Array = [];
wordDisplayF("MMMM");
// use wordDisplayA elements to remove objects from stage and null them.
// can tweak but don't change significantly
function wordDisplayF(s:String,x:int,y:int):void{
var nextX:int = x;
for(var i:int=0;i<s.length;i++){
var C:Class=Class(getDefinitionByName(s.charAt(i).toUpperCase()));
var instance:MovieClip=new C();
addChild(instance);
instance.x=nextX;
instance.y=nextY;
nextX+=Math.ceil(instance.width);
wordDisplayA.push(instance);
}
Copy link to clipboard
Copied
(from the reply to Ned Murphy)
KGlad, how do I modify your code to implement my strategy with buttons?
-markerline
Copy link to clipboard
Copied
:
// initialize
var wordDisplayA:Array = [];
var btnA:Array=[btn_A,btn_B,..];
for(var i:int=0;i<btnA.length;i++){
btnA.addEventListener(MouseEvent.CLICK,letterDisplayF);
}
// use wordDisplayA elements to remove objects from stage and null them.
// can tweak but don't change significantly
function letterDisplayF(e:MouseEvent):void{
var C:Class=Class(getDefinitionByName("Alpha_01_"+e.currentTarget.name.split("btn_")[1].toUpperCase()));
var instance:MovieClip=new C();
addChild(instance);
wordDisplayA.push(instance);
instance.x=instance.width*wordDisplayA.length;
}
// call when you want to remove the letters.
function removeWordF():void{
for(var i:int=wordDisplayA.length-1;i>=0;i--){
removeChild(wordDisplayA);
}
wordDisplayA.length=0;
}
Copy link to clipboard
Copied
That's a great solution for most of the app. However I am trying to use sliders to access frames within each letter's MovieClip. This will enable me to display different visual instances of that specific letter (such as the different images of the letter M). However with your code I can't seem to name the "instance" using instance.name=[someNumber] in a for loop.
How would I fix that? It is a critical part of my app to be able to display different visualizations of the letter.
Copy link to clipboard
Copied
i don't see why you need an instance name for that. you can just loop through all the wordDisplayA elements (cast as movieclips) and direct them to the correct frame.
but if needed, you can assign any string for the name property. eg:
// initialize
var wordDisplayA:Array = [];
var btnA:Array=[btn_A,btn_B,..];
for(var i:int=0;i<btnA.length;i++){
btnA.addEventListener(MouseEvent.CLICK,letterDisplayF);
}
// use wordDisplayA elements to remove objects from stage and null them.
// can tweak but don't change significantly
function letterDisplayF(e:MouseEvent):void{
var C:Class=Class(getDefinitionByName("Alpha_01_"+e.currentTarget.name.sp lit("btn_")[1].toUpperCase()));
var instance:MovieClip=new C();
instance.name=e.currentTarget.name.split("btn_")[1];
addChild(instance);
wordDisplayA.push(instance);
instance.x=instance.width*wordDisplayA.length;
}
// call when you want to remove the letters.
function removeWordF():void{
for(var i:int=wordDisplayA.length-1;i>=0;i--){
removeChild(wordDisplayA);
}
wordDisplayA.length=0;
}
Copy link to clipboard
Copied
The reason I thought to use instance names was to use a for loop and an if condition to check if the names match the sliders. There is one slider on the stage PER letter-button. This way if the condition is true, only then move the matching letter to the correct frame. Otherwise the swf won't know which slider it is supposed to reference for the letter on the stage. The case being that if you have more than one letter and the letters are a different value you will have the option to slide each letter into its own frame position.
But I just thought about something wrong.
If I have the same letter and there is only one slider then all instances of that same letter will always evaluate to the same frame.
Thus I need a control that manipulates each letter that is on the stage one at a time and if it is the same letter, provide the option to select that letter first and then adjust its frame accordingly. If you need to adjust a different letter but it's the same "letter" (such as the first and the third capital M) then you can select the desired M first to make it an active Event and then adjust its frame.
What would be a solution path for this?
I have currently a bunch of nested for each loops iterating through the letters and the sliders that are on the stage as well as the letter names and slider names and then an if statement to check if they are equivalent names. They are tracing properly but the letter is not changing to the correct frame even though I can see the correct frame in the trace.
Copy link to clipboard
Copied
// initialize
var wordDisplayA:Array = [];
var btnA:Array=[btn_A,btn_B,..];
for(var i:int=0;i<btnA.length;i++){
btnA.addEventListener(MouseEvent.CLICK,letterDisplayF);
}
// use wordDisplayA elements to remove objects from stage and null them.
// can tweak but don't change significantly
function letterDisplayF(e:MouseEvent):void{
var C:Class=Class(getDefinitionByName("Alpha_01_"+e.currentTarget.name.sp lit("btn_")[1].toUpperCase()));
var instance:MovieClip=new C();
nameF(instance,e.currentTarget.name.split("btn_")[1]);
addChild(instance);
wordDisplayA.push(instance);
instance.x=instance.width*wordDisplayA.length;
}
function nameF(mc:MovieClip,s:String):void{
n:int=0;
for(var i:int=0;i<wordDisplayA.length;i++){
if(wordDisplayA.name.indexOf(s)>-1){
n++;
}
}
mc.name=s+"_"+n;
}
Copy link to clipboard
Copied
Thanks for all of your input kGlad. I do appreciate the time.
I have come up with a solution integrating some of your code with some of mine and it seems to be acceptable from a UI standpoint so far. I have to bounce it off of some other folks to make sure that it's not a cumbersome experience when using the tablet. But everything hopefully will go smoothly.
Regards,
markerline
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now