dynamically add children to component, then dynamically manipulate them? - actionscript-3

i dont know why this doesnt work. i have a tabnavigator which i dynamically add a navigatecontainer to which has a textarea dynamically added to the nav container. the id's of both have the same stringname as the chatguys[c][0].
it gives errors saying 'TypeError: Error #1034: Type Coercion failed: cannot convert spark.skins.spark::SkinnableContainerSkin#9737851 to spark.components.TextArea." at runtime. wut do?
any help would be greatly appreciated, thank you :(
var idx:uint;
const len:uint = navigate.numChildren;
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
//var check:spark.components.TextArea = navigate.getElementAt(idx) as spark.components.TextArea;
//var check:Tab = navigate.getTabAt(idx) as Tab;
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
// var check = navigate.getElementAt(idx);
//elmt.selected = tgBtn.selected;
if (check.label == evt.username)
{// trace(navigate.getChildAt(idx).label);
var c:uint;
const p:uint = chatguys.length;
for (c = 0; c < p; c++){
if(chatguys[c][0] == evt.username){
spark.components.TextArea(DisplayObjectContainer(navigate.getChildAt(idx)).getChildAt(chatguys[c][0])).textFlow=TextConverter.importToFlow(chatguys[c][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
b here ill show you the second half which works fine, but maybe i need to do it differently to get waht i need done ?
var chats:Array = [];
var chatguys:Array = [];
public function userlist_click() :void{
var windowname:Object =users_lst.selectedItem;
var idx:uint;
const len:uint = navigate.numChildren;
trace(navigate.numChildren);
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
if (check.label == windowname.name)
{
alreadyexists = true;
}
}
if (alreadyexists == false)
{
chats[windowname.name] = new spark.components.TextArea();
chats[windowname.name].x = 10;
chats[windowname.name].y= 32;
chats[windowname.name].width= 517.19696 ;
chats[windowname.name].height= 343.18182;
chats[windowname.name].scroller
chats[windowname.name].x="9";
chats[windowname.name].y="2";
chats[windowname.name].width="517.19696";
chats[windowname.name].height="343.18182";
chats[windowname.name].setStyle("skinClass", spark.skins.spark.TextAreaSkin);
//textArea.skinClass= "spark.skins.spark.TextAreaSkin";
chats[windowname.name].text="ffg";
chats[windowname.name].setStyle("verticalScrollPolicy", ScrollPolicy.ON);
var match = new Array();
match.push(windowname.name);
match.push('bob');
chatguys.push(match);
for (var i in chatguys){
if (chatguys[i][0] == windowname.name){
chats[windowname.name].textFlow=TextConverter.importToFlow(chatguys[i][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
chats[windowname.name].id = windowname.name;
trace(chats[windowname.name]);
var messagebox:NavigatorContent = new NavigatorContent;
messagebox.percentWidth= 100;
messagebox.percentHeight= 100;
messagebox.label = chats[windowname.name];
messagebox.id = chats[windowname.name];
trace(messagebox.label);
messagebox.addElement(chats[windowname.name]);
navigate.addChild(messagebox); }}

Related

use .removeFromThreads in more than 100 threads inside a google app script

i have a script that removes every thread within a label and all sublabels.This was workng fine, but recently i got an error that says that the operation childrens[i].removeFromThreads(threads); can not be applied to more that 100 threads... How can i fix this?
function removingThreadsfromLabel() {
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
childrens[i].removeFromThreads(threads);
}
}
function children(parent) {
var name = parent.getName() + '/';
return GmailApp.getUserLabels().filter(function(label) {
return label.getName().slice(0, name.length) == name;
});
}
For now i have done the following, but it is not optimal...
function removingThreadsfromLabel() {
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
Logger.log(threads.length);
while (threads.length>100){
childrens[i].removeFromThread(threads[0]);
var threads = childrens[i].getThreads();
}
childrens[i].removeFromThreads(threads);
}
}
Regards,
Ok, i figure out how to do it in batch, so it is better that my first solution:
function removingThreadsfromLabel() {
var batchSize = 100;
var parentlabelstring = 'THELabel';
var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
var childrens = children(parentlabel);
for (var i = 0; i < childrens.length; i++){
var threads = childrens[i].getThreads();
Logger.log(threads.length);
for (var j = 0; j < threads.length; j+=batchSize) {
childrens[i].removeFromThreads(threads.slice(j, j+batchSize));
}
var threads = childrens[i].getThreads();
childrens[i].removeFromThreads(threads);
}

How to Save positions for 3 objects in Array to make random position between each other by AS3?

How to Save positions for 3 objects in Array to make random position between each other by AS3?
import flash.geom.Point;
var arry:Point = new Point();
arry[0] = arry[78,200];
arry[1] = arry[217,200];
arry[2] = arry[356,200];
//object called b1
b1.x = arry[0][0];
b1.y = arry[0][1];
//object called b2
b2.x = arry[1][0];
b2.y = arry[1][1];
//object called b3
b3.x = arry[2][0];
b3.y = arry[2][1];
//make objects swap positions between each other
var rand:Number = (Math.random()*arry.length);
//output to see random position [[78,200],[217,200],[356,200]]
trace(arry);
to get random with tween like this... https://www.youtube.com/watch?v=8m_m64plQ6E
At compile time you should get this Error I suppose : "ReferenceError: Error #1069"
Here is a way to store the positions (like in the link you provided from youtube) :
import flash.geom.Point;
var squareWidth:uint = 40;
var squareHeight:uint = 40;
var marginX:uint = 100;
var marginY:uint = 75;
var spacer:uint = 10;
var positions:Vector.<Point > = new Vector.<Point > (9);
function setPositions(v:Vector.<Point>):void {
var count:uint = 0;
var posx:uint;
var posy:uint;
for (var i = 0; i < 3; i ++)
{
for (var j = 0; j < 3; j ++)
{
posx = (j * squareWidth) + (spacer * j) + marginX;
posy = (i * squareHeight) + (spacer * i) + marginY;
v[count] = new Point(posx,posy);
count++;
}
}
}
setPositions(positions);
trace(positions);
// output :
// (x=100, y=75),(x=150, y=75),(x=200, y=75),(x=100, y=125),(x=150, y=125),(x=200, y=125),(x=100, y=175),(x=150, y=175),(x=200, y=175)
So here you have nine Points to place the clips like in the video.
You just have to add a function to swap the nine boxes stored in another Vector.
In your case.
For 3 positions do the following if I understand your question.
import flash.geom.Point;
var positions:Vector.<Point> = new Vector.<Point>(3);
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
// output : (x=78, y=200),(x=217, y=200),(x=356, y=200)
So, you're still unclear!
Your issue is to find a random position?
This may help you if this is the problem you're facing :
import flash.geom.Point;
var positions:Vector.<Point > = new Vector.<Point > (3);
var numbers:Vector.<uint> = new Vector.<uint>();
var numbersAllowed:uint = 3;
var rndNmbrs:Vector.<uint> = new Vector.<uint>(3);;
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
function populateRndNmbrs(n:uint):void {
for (var i:uint = 0; i < n; i++)
{
numbers[i] = i;
}
}
populateRndNmbrs(numbersAllowed);
function populateRandomNumbers(n:uint):void
{
var rnd:uint;
for (var i:uint = 0; i < n; i++)
{
rnd = numbers[Math.floor(Math.random() * numbers.length)];
for (var j:uint = 0; j < numbers.length; j++)
{
if (rnd == numbers[j])
{
numbers.splice(j,1);
}
}
rndNmbrs[i] = rnd;
}
}
populateRandomNumbers(numbersAllowed);
trace("rndNmbrs = " + rndNmbrs);
for (var i:uint = 0; i < numbersAllowed; i++)
{
trace("b"+ (i+1) + ".x = " + positions[rndNmbrs[i]].x);
trace("b"+ (i+1) + ".y = " + positions[rndNmbrs[i]].y);
// In place of trace, you should place the boxes at those random positions.;
}
//output:
//(x=78, y=200),(x=217, y=200),(x=356, y=200)
//rndNmbrs = 2,0,1
//b1.x = 356
//b1.y = 200
//b2.x = 78
//b2.y = 200
//b3.x = 217
//b3.y = 200
Is that what you want? Or do you want to know how to create a motion effect?
I'm not sure about what you really need...
This will help you to place all the boxes in a random position.
You may do this like here bellow, and add a function to check if the random positions are not the same.
With only 3 MovieClips, you will often have the same random positions as long they're stored in the "positions Vector"
var squares:Vector.<MovieClip> = new Vector.<MovieClip>(3);
function populateMCs(target:DisplayObjectContainer,n:uint):void{
for (var i:uint = 0; i < n; i++){
squares[i] = target["b"+(i+1)];
}
}
function swapMCs():void{
for (var i:uint=0; i<squares.length; i++){
squares[i].x = positions[rndNmbrs[i]].x;
squares[i].y = positions[rndNmbrs[i]].y;
}
}
populateMCs(this,numbersAllowed);
swapMCs();
I give you a last example to get a motion effect in AS3.
I'm not a translator AS2 -> AS3 and a video is not the best way to show your code :(
This will make your boxes move smoothly, but not the way you want.
Now, you have to learn AS3 and try to make the job by yourself.
Then, if you have another issue, just ask clearly what you want.
var friction:Number = 0.15;
setDestination(squares[0],squares[0].x,350,friction);
setDestination(squares[1],squares[1].x,350,friction);
setDestination(squares[2],squares[2].x,350,friction);
squares[0].addEventListener(Event.ENTER_FRAME,moveClip);
squares[1].addEventListener(Event.ENTER_FRAME,moveClip);
squares[2].addEventListener(Event.ENTER_FRAME,moveClip);
function setDestination(mc:MovieClip,x:uint,y:uint,friction:Number):void{
mc.destinx = x;
mc.destiny = y;
mc.f = friction;
}
function moveClip(e:Event):void{
var mc:MovieClip = e.target as MovieClip;
trace(mc.name)
mc.speedx = (mc.destinx - mc.x);
mc.speedy = (mc.destiny - mc.y);
mc.x += mc.speedx*mc.f;
mc.y += mc.speedy*mc.f;
if((Math.floor(mc.speedx)<1) && (Math.floor(mc.speedy)<1)){
mc.removeEventListener(Event.ENTER_FRAME,moveClip);
trace("STOP MOVE FOR " + mc.name);
}
}

adding movieclips from random generated no in array

hey i am trying to add movie clips to the stage randomly from a list of cards, no 1- 10,
this is what i have tried so far but i get an error saying its my randomly selected card is not a function, just wondering if anybody can help or know the proper way of accomplishing it
thank you
var printArray:Array =new Array();
var randPrint:String;
var rand
for(var n:int = 1; n <= 28; n++)
{
randNo=Math.round(Math.random() * 10+.5);
randPrint = "cardPrint"+randNo;
printArray.push(randPrint);
}
var cardPrint1:MovieClip = new card_1();
var cardPrint2:MovieClip = new card_2();
var cardPrint3:MovieClip = new card_3();
var cardPrint4:MovieClip = new card_4();
var cardPrint5:MovieClip = new card_5();
var cardPrint6:MovieClip = new card_6();
var cardPrint7:MovieClip = new card_7();
var cardPrint8:MovieClip = new card_8();
var cardPrint9:MovieClip = new card_9();
var cardPrint10:MovieClip = new card_10();
for(var p:int = 1; p <= 1; p++)
{
trace(printArray[p]);
addChild(printArray[p]);
}
some help would be great, thank you so much
I think something like the following will do what you want. I populated an array with all the available assets and then filled an array with random numbers between 0-9. The last for loop just creates the movieclips and adds them to the stage.
var printArray:Array = [];
var mcs:Array = [card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9, card_10];
for(var n:int = 1; n <= 28; n++)
{
var randNo:int = int(Math.random() * 10);
printArray.push(randNo);
}
for(var p:int = 0; p < printArray.length; p++)
{
trace(printArray[p]);
var mc:MovieClip = new mcs[printArray[p]];
addChild(mc);
}

AS3 Loading Image URLrequest - getBounds returns no values

I need to get the width and height of a flag I am loading into another movie so I can place it in the right location. Why is my getBounds not picking up the dimensions of the flag?
function displayFlags(evt:Event = null)
{
if(!Lang) { return; }
for (var i:uint = 0; i < Lang.length; i++)
{
//Language = new MovieClip();
//Language.name = Lang[i];
LangButton = new button01();
LangButton.name = Lang[i];
LangButton.btext.text = Lang[i];
LangButton.y = LangButton.height * i;
addChild(LangButton);
var flag:Loader = new Loader();
flag.load(new URLRequest(LangPath[i]+"/flag.png"));
/// Loads Flag into Button
LangButton.addChild(flag);
var fh = flag.getBounds(flag);
trace("FLAG HEIGHT = " + fh.height); // ZERO ZERO ZERO ZERO
// I really need this info to place the flag in the right location.
flag.y = (LangButton.height/2) - (flag.height/2);
}
evt.target.visible = false;
}
UPDATE: MAY 19TH, 2013
I was able to figure out that I need to wait for the flag to be loaded. Now I can get the correct Bounds.. however, now I can not get the movieClip "flag" in the load complete to respond. I don' think it sees the value of flag.
Below is my UPDATED code:
function displayFlags(evt:Event = null)
{
if(!Lang) { return; }
for (var i:uint = 0; i < Lang.length; i++)
{
//Language = new MovieClip();
//Language.name = Lang[i];
LangButton = new button01();
LangButton.name = Lang[i];
LangButton.btext.text = Lang[i];
LangButton.y = LangButton.height * i;
addChild(LangButton);
flag = new Loader();
flag.load(new URLRequest(LangPath[i]+"/flag.png"));
flag.name = Lang[i];
flag.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedFlag(flag));
function loadedFlag()
{
return function()
{
var fh = flag.getBounds(flag);
trace("FLAG HEIGHT = " + fh);
trace("flag Name: "+ flag.name);
flag.alpha = .3;
}
}
LangButton.addChild(flag);
}
evt.target.visible = false;
}
try this :
flag.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
then add function :
function completeHandler(e:Event):void
{
var myFlagInfo:LoaderInfo = e.currentTarget as LoaderInfo;
var myFlag:Loader = myFlagInfo.loader;
var fh = myFlag.getBounds(myFlag);
}

Making comparisson to items in an Object

I have the following object:
var users:Object= new Object();
users[0]["user_id"] = "1124";
users[0]["name"] = "ikke";
users[0]["age"] = "24";
users[0]["gender"] = "male";
users[1]["user_id"] = "1318";
users[1]["name"] = "test";
users[1]["age"] = "20";
users[1]["gender"] = "male";
var selectors:Object = new Object();
selectors["user_id"] = 1318;
selectors["gender"] = "male";
what i want is to use the selectors object in an if statement. In humans lanuguage it should be something like:
for (var index:String in users) {
If users[index]["gender"] == selectors[gender] && users[index]["user_id"] == "male" -> then trace "success".
}
The tricky part is that the selectors object is dynamic. Sometimes it can contain only 1 item , sometimes 3 items. Or it can also be null. In that case it should allways trace success. Anyone that can help me?
for(var i:int = 0; i < users.length; i++) {
var success:Boolean = true;
for(var key:String in selectors) {
if(users[i][key] != selectors[key]) {
success = false;
break;
}
}
if(success) {
trace('success for user ' + i);
}
}