Flash AS3 Remove Clip, reorder other items - actionscript-3

I have a scrollpane to which I add movieclips to. I am using it a an online users list. It works well so far but now I have run into a problem. I am able to remove the movieclips I want easily enough using removechild, but when I remove it there is only a blank space where the removed clip was. So I need to know how to have the scrollpane refresh somehow and move the clips below that one removed up. How can I do this?
I have tried invalidate, refreshPane, update, etc. Maybe I didnt have them in the correct order or something, but how can this be done?
If the answer is complex, could an example be provided please? Im really not good enough with as3 just yet to code an entire algorithm to get the number of children, remove them all, readding them, etc.
Any help please?

Just a variation, but saves the -1, simply rearrange such as:
lstOnline.removeChild(lstOnline.getChildByName("NAMEHERE"));
var numleft:Number = lstOnline.numChildren;
for(var i = 0; i < numleft - 1; i++) {
lstOnline.getChildAt(i).y = i*60;
}

Guess I was a little better than I thought. In case anyone else faces this problem, the solution I found was the following:
var numleft:Number = lstOnline.numChildren;
lstOnline.removeChild(lstOnline.getChildByName("NAMEHERE"));
for(var i = 0; i < numleft - 1; i++) {
lstOnline.getChildAt(i).y = i*60;
}
lstOnline is the name of the ScrollPane and in my case 60 is the height of the added movieclips.

Related

Make multiple objects within a movieclip disappear at once

I have multiple buttons within a movieclip "mc_circlebtn". I want to make all of them disappear at once with actionscript 3. How can I do it? Would really appreciate it if I could get some fast responses. Thank you.
I am not sure what you are trying, but maybe this is what you need, run this code when you want to remove buttons:
for (var i:int = mc_circlebtn.numChildren-1; i >= 0; i--)
{
mc_circlebtn.removeChildAt(i);
}

How to let an object appear in actionscript

I have an animation that may stop at 2 points depending on what the user fills in, if the animation stops at one of the two frames an objects has to apear and have to disappear if the user continues with playing the animation again. Can someone tell me how I can let object appear if the animation stops at a certain frame?
Do I need something like this? I have very little experience so please help!
star_mc._alpha = 0; star_mc.onEnterFrame = function(){ if(this._alpha < 100){ this._alpha = this._alpha + 5; }}
The code looks ok.... try it... just put it in the right place!
It's hard to tell where because I don't know how your animation built!!

Make all children do something...?

I'm wondering if there is an easy way to make all children simultaneously do something. In particular, I want everything on my stage to shake back and forth like an earthquake. It's a bit hard to single out everything on stage and add in the proper code because at a given time I don't always know the amount of children on stage.
Also, I'm not sure if it makes a difference, but sometimes when I addChild something, I don't go out of my way to add it to the stage...Like for a few buttons I'll do this.addChild(mybutton). I don't know if there's a way to access everything that has been addChilded even though I may not have added them directly to the stage? I'm pretty sure numChildren returns the value of the number of objects on screen, but I could be wrong...
I was thinking of doing something like
Inside my loop
if (numChildren >= 10 && nukeShakeTimer.currentCount == 0)
{
nukeShakeTimer.start();
}
if (nukeShakeTimer.currentCount > 0)
{
NukeShake();
}
NukeShake
public function NukeShake():void
{
numberChildren = numChildren;
trace(numberChildren);
while (numberChildren > 0)
{
var tempObject:Object = getChildAt(numberChildren)
if (nukeShakeTimer.currentCount % 2 == 1)
{
tempObject.x += 10;
}
if (nukeShakeTimer.currentCount % 2 == 0)
{
tempObject.x -= 10;
}
numberChildren -= 1;
}
if (nukeShakeTimer.currentCount > 30)
{
nukeShakeTimer.reset();
}
}
When I try this though, I get a runtime error for the line var tempObject:Object = getChildAt(numberChildren) and the error reads RangeError: Error #2006: The supplied index is out of bounds.
Also I feel like it may be possible to do this operation much faster without using a while loop, but maybe not. Help please, thanks!
If you want to address every child on the stage then you'll need to loop through them. As Cherniv says, changing your getChildAt call to use numberChildren-1 should resolve your error.
It shouldn't matter whether or not everything is directly on the stage or not. Looping through the stage children will get you whatever container you added the objects to. Moving that container will move its children too. (Though you'll have to do more work if you need to move those children independently).
But...
In your specific case, it looks like you just want to shake everything on the screen together, in the same direction at the same rate. In this case, I would probably just add all my objects to a single container Sprite on the stage. Then, when you want to do a shake effect, you only need to move that one container object and everything moves together.

drawing on CandlestickSeries LineSeries makes candles narrower

I'm, using Flex 4.5.
In my application I have a CandlestickChart with basic CandlestickSeries.
I give the user the ability to add a linechart on top of the candlesChart by adding a LineSeries to the CandleStickChart as explained in adobe's docs here: here
My problem is that when the line is added it changes the shape of the candle to be narrower. Moreover, every line that is added is making the candle be narrower.
I've looked around and found this thread in adobe's forums: "CandlestickChart problem with LineSeries" which describes the exact same problem.
Unfortunately, the thread is still not answered.
Does any one knows a solution for this problem?
Thanks in advance,
Ravid
Well, the solution I found is not nice and better solutions are still appreciated but for those of you that have the same problem this solution worked for me:
The problem occurs because the candle's default item renderer uses the item's width to calculate the size of the candle which is make sense.
The problem, is that when you add another series there is a bug and the item's width is changed thefore the candle is changed.
My solution is to create a copy of the default item renderer and draw the candles based on the graph's width and number of candles you wish to put there (so you don't use the item's width).
It fixes the problem but if you use tooltips functionality than it kinda messes its placement relative to the candle.
I know this answer is a bit late, but in case it helps anyone else, here's what I did.
(It's not thoroughly tested either)
Make a class that extends the CandleStickChart class and simply override the "applySeriesSet" function as so:
override protected function applySeriesSet(seriesSet:Array /* of Series */,
transform:DataTransform):Array /* of Series */
{
// filter out the non-candlestick series
var filteredSeriesSet:Array = new Array();
for each(var series:Series in seriesSet){
if(series is CandlestickSeries) filteredSeriesSet.push(series);
}
// call the CandlestickChart applySeriesSet function with the filtered set, ignore return value
super.applySeriesSet(filteredSeriesSet, transform);
// do the code that the CartesianChart applySeriesSet function would have done, but with the unfiltered seriesSet
// would have preferred to do something like super.super.applySeriesSet(seriesSet, transform);
var n:int = seriesSet.length;
for (var i:int = 0; i < n; i++)
{
var newSeries:IChartElement = seriesSet[i];
if (newSeries is Series)
customizeSeries(Series(seriesSet[i]), i);
}
return seriesSet;
}

Flex AS3 : Find and remove elements from container of certain class

How can I remove only every image found in a Bordercontainer which also holds Textinputs and Buttons ?
i tried:
for(var i:int=0;i<container.numElements;i++){
if(container.getElementAt(i) is Image){
container.removeElementAt(i);}
}
But as expected this loop does not fully work since the numElements changes which means that not all Images get removed.
I know there is some simple trick to this...but I cant think of it right now...Please help
As commenters have suggested, it seems like looping backwards would be the way to do it. I'd try something like this:
var totalElements : int = container.numElements;
for(var i:int=totalElements-1;i>=0;i--){
if(container.getElementAt(i) is Image){
container.removeElementAt(i);
}
}
By storing the numElements in a variable before starting the loop, you can be sure that the value will not change while processing the loop. Since your going backwards, you don't have to worry about the child index changing.
A second option would be to queue up the image instances in one loop and remove them in a second loop using the removeElement method. I suspect the two loop method will have significantly worse performance.
Looping backwards would be 1 way to do this.
Another would be
for(var i:int=0; i<container.numElements; i++){
if(container.getElementAt(i) is Image){
container.removeElementAt(i);
i--; //This nullifies the effect of removing an element
}
}