Is there a difference between MovieClip(X) and X as Movieclip? [duplicate] - actionscript-3

This question already has answers here:
AS3: cast or "as"?
(8 answers)
Closed 7 years ago.
Maybe in performance.
I'm constantly using MovieClip(getChildByName("x")).stop();
Is it better than (getChildByName("x") as MovieClip).stop(); ?

results:
(ui.getChildAt(0) as SpectrumAnalyzer) = 174 ms
SpectrumAnalyzer(ui.getChildAt(0)) = 200 ms
For the programmer it's the same - but i don't know if the code is doing the same thing in two different ways. Anyways the difference is 26 ms for 1 mio iterations.
var sa:SpectrumAnalyzer = new SpectrumAnalyzer()
var ui:UIComponent = new UIComponent()
ui.addChild(sa)
addElement(ui)
var start:Number = new Date().getTime()
for(var i:int=0; i<1000000; i++){
(ui.getChildAt(0) as SpectrumAnalyzer)
}
var end:Number = new Date().getTime()
trace(end-start)
start = new Date().getTime()
for(var i:int=0; i<1000000; i++){
SpectrumAnalyzer(ui.getChildAt(0))
}
end = new Date().getTime()
trace(end-start)

Related

handling position of multiple movieclips in relation to each other

I'm not that new to using Flash, but I mostly used it for making animations and I don't really use actionscript much but this time I wanted to try making layouts by code.
I'm trying to position square movieclips to form a grid like menu.
I positioned the each of the movieclips all in code but I think perhaps there's a better and more efficient way of doing this.
The code is very basic but I'll post it anyway so you have an image of what I'm trying to make it look like. I'm not really good at explaining so, I apologize.
//1st row//
var btn1:MovieClip = new dBtn();
btn1.x = -210;
btn1.y = -90;
addChild(btn1);
var btn2:MovieClip = new dBtn();
btn2.x = btn1.x+70;
btn2.y = btn1.y;
addChild(btn2);
var btn3:MovieClip = new dBtn();
btn3.x = btn2.x+70;
btn3.y = btn2.y;
addChild(btn3);
//2nd row//
var btn4:MovieClip = new dBtn();
btn4.x = btn1.x;
btn4.y = btn1.y+70;
addChild(btn4);
var btn5:MovieClip = new dBtn();
btn5.x = btn4.x+70;
btn5.y = btn4.y;
addChild(btn5);
var btn6:MovieClip = new dBtn();
btn6.x = btn5.x+70;
btn6.y = btn5.y;
addChild(btn6);
//3rd row//
var btn7:MovieClip = new dBtn();
btn7.x = btn4.x;
btn7.y = btn4.y+70;
addChild(btn7);
var btn8:MovieClip = new dBtn();
btn8.x = btn7.x+70;
btn8.y = btn7.y;
addChild(btn8);
var btn9:MovieClip = new dBtn();
btn9.x = btn8.x+70;
btn9.y = btn8.y;
addChild(btn9);
Loops + simple math.
var buttonsList:Array = new Array;
for (var i:int = 0; i < 9; i++)
{
// You can omit () with "new" operator if there are no arguments.
var aBut:MovieClip = new dBtn;
// Value of i % 3 goes 0 1 2 0 1 2 0 1 2.
aBut.x = -210 + 70 * (i % 3);
// Value of int(i / 3) goes 0 0 0 1 1 1 2 2 2.
aBut.y = -90 + 70 * int(i / 3);
addChild(aBut);
buttonsList[i] = aBut;
}
Then to address each one of them you can use their indices from 0 to 8 respectively:
// Make the central one semi-transparent.
buttonsList[4].alpha = 0.5;

Action Script 3. How to add multiple children of 1 instance?

I'm creating flash game. I need to make player lives - heart images. If player have 5 lives should be added 5 hearts <3 <3 <3 <3 <3. I have image with instance name heart. How to add them correctly?
I've tried this:
var lives:Number = 4;
var currentHP = lives;
var heart:Heart = new Heart();
var hpArr:Array = new Array();
function hp() {
for (var i=0; i<lives; i++) {
heart = new Heart();
hpArr[i] = heart;
hpArr.push(heart);
heart.x += heart.width+20;
addChild(heart);
}
trace("Array length" + hpArr.length);
}
This correctly return 5 trace("Array length" + hpArr.length);, so that means hearts successfully added to array. Problem is that only 1 heart is added. What can be problem?
Change your for loop like this:
for (var i=0; i<lives; i++) {
heart = new Heart();
hpArr.push(heart);
heart.x = ( heart.width + 20 ) * i; // here is the trick!
addChild(heart);
}

Can you create new sprite out of two or more already existing by taking their overlapping part

I have two spites created by drawPath(). Sometimes they might overlap each other and when it happens i want to create new sprite, which contains only overlapping part. Maybe it is really simple but i couldn't find any solution exept draw a rectangle of zone where they overlaps. So if it could be done i really appreciate your help.
for (var j:int = 0; j < zonesAmount; j++) {
var sp:Sprite = new Sprite();
var zoneCoord:Vector.<Number> = new Vector.<Number>(0, false);
var zoneCommands:Vector.<int> = new Vector.<int>(0, false);
var o:XML;
for each (o in xml.ZONE[j].POINT)
{
var tmpCoord:int = o.#X;
zoneCoord.push(tmpCoord);
tmpCoord = o.#Y;
zoneCoord.push(tmpCoord);
}
zoneCommands.push(GraphicsPathCommand.MOVE_TO);
for (var i:int = 0; i < (zoneCoord.length - 2)/4; i++)
zoneCommands.push(GraphicsPathCommand.CURVE_TO);
sp.graphics.beginFill(xml.ZONE[j].#COLOR);
sp.graphics.drawPath(zoneCommands, zoneCoord);
sp.graphics.endFill();
sp.alpha = 0.1;
zones[xml.ZONE[j].#ID - 1] = sp;
var picsToolt:mapTooltip = new mapTooltip(15, 15, xml.ZONE[j].SOURCE.#NMB, xml.ZONE[j].SOURCE.#SRC);
picsToolt.Register(zones[xml.ZONE[j].#ID - 1], "");
addChild(zones[xml.ZONE[j].#ID - 1]);
}
Don't bother for addChild(zones[xml.ZONE[j].#ID - 1]) it doesn't matter
Here is examples of what i'm trying to do:
first and second
Second one describes exactly in what i'm intrested, red rect is what i can do and blue line is what i want

ActionScript 3.0 Exponent Calculator

If a user enters a base = 3 and an exponent = 4, your program should be able to use a loop to calculate powers as follows: i know I have to use power=Math.pow(baseIn, exponentIn) but other then that im very lost.. learning this my self since im the only person taking the class
Example:
31 = 3
32 = 9
33 = 27
34 = 81
![enter image description here][1]
tip: power=Math.pow(baseIn, exponentIn);
http://i.stack.imgur.com/EUkdx.png
To use a loop to calculate you would do this.
function powerCalc(base:Number, power:Number):Number
{
var result:Number = 1;
for (var i:int = 0; i < power; i++)
{
result = result * base;
}
return result;
}
Then you can call:
var power = powerCalc(3,4);

AS3: How do I save out the data from a datagrid into a line-by-line text file?

I've got a datagrid populated with text file that has line-by-line words(ie. one per line). The datagrid has 5 columns. Every 5 words out of the text file is put in a new row. The datagrid can then be added to or removed from. How would I go about rewriting the text file using all the values in the datagrid?
This is how the datagrid gets populated. How can I go through the entire datagrid cell for cell and rewrite the data in the text file?
var loadFavourites: URLLoader = new URLLoader();;
var arFavourites = new Array();
loadFavourites.addEventListener(Event.COMPLETE, onLoadedFavourites);
loadFavourites.load(new URLRequest("lists/Favourites.txt"));
function onLoadedFavourites(event:Event):void
{
arFavourites = event.target.data.split("\r\n");
for (var i:int = 0; i <= arFavourites.length; i++)
{
if (i != 0 && i % 5 == 0)
{
dg.addItem({Place: arFavourites[i-5],Subject:arFavourites[i-4],Object:arFavourites[i-3],Feeling:arFavourites[i-2],Action:arFavourites[i-1]});
}
}
}
Add Record:
var i:int=0;
var tPlace = mc_places.mc_places_text.txtPlaces.text;
var tSubject = mc_subject.mc_subject_text.txtSubject.text;
var tObject = mc_object.mc_object_text.txtObject.text;
var tFeeling = mc_feeling.mc_feeling_text.txtFeeling.text;
var tAction = mc_action.mc_action_text.txtAction.text;
arFavourites[FavCount+1]=tPlace;
arFavourites[FavCount+2]=tSubject;
arFavourites[FavCount+3]=tObject;
arFavourites[FavCount+4]=tFeeling;
arFavourites[FavCount+5]=tAction;
dg.addItem({Place: arFavourites[FavCount+1],Subject:arFavourites[FavCount+2],Object:arFavourites[FavCount+3],Feeling:arFavourites[FavCount+4],Action:arFavourites[FavCount+5]});
dg.scrollToIndex(dg.length-1);
FavCount=FavCount+5;
for (i=0; i<arFavourites.length; i++) {
trace(arFavourites[i]);
}
Remove Record:
var k:int;
var i:int;
trace("Selected Index: " + dg.selectedIndex);
if (dg.length == 0) {
return;
}
for (k=0; k<dg.length; k++) {
if (dg.isItemSelected(dg.getItemAt(k))) {
for (i=0; i<arFavourites.length; i++) {
if (arFavourites[i] == dg.getItemAt(k)[1]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[2]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[3]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[4]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[5]) {
arFavourites.splice(i,1);
}
}
dg.removeItemAt(k);
dg.scrollToIndex(k);
dg.clearSelection();
break;
}
}
Well, what I would do is basically just the reverse. First you'd get the values of the DataGrid and put them back in an Array in the same order as before:
var newFavArray:Array = new Array();
for (var i:int = 1; i <= dg.length; i++)
{
newFavourites[i * 5 - 5] = dg.getItemAt(i - 1).Place;
newFavourites[i * 5 - 4] = dg.getItemAt(i - 1).Subject;
newFavourites[i * 5 - 3] = dg.getItemAt(i - 1).Object;
newFavourites[i * 5 - 2] = dg.getItemAt(i - 1).Feeling;
newFavourites[i * 5 - 1] = dg.getItemAt(i - 1).Action;
}
(I whipped this up fairly quick, if you can find a better way of using i to access the Array and the DataGrid feel free to use it.)
Then you convert it to a String:
var newFavString:String = newFavArray.join("\r\n");
And then finally save it to the file however you want (overwriting the old values of course). Since it's an AIR app, it's best to use the File and FileStream classes for this:
var favFile:File = File.applicationStorageDirectory.resolvePath("lists/Favourites.txt");
var favStream:FileStream = new FileStream();
favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();
I'm not sure what your workflow is but personally I'd put all of this in a function:
function saveFavourites():void
{
var newFavArray:Array = new Array();
for (var i:int = 1; i <= dg.length; i++)
{
newFavArray[i * 5 - 5] = dg.getItemAt(i - 1).Place;
newFavArray[i * 5 - 4] = dg.getItemAt(i - 1).Subject;
newFavArray[i * 5 - 3] = dg.getItemAt(i - 1).Object;
newFavArray[i * 5 - 2] = dg.getItemAt(i - 1).Feeling;
newFavArray[i * 5 - 1] = dg.getItemAt(i - 1).Action;
}
var newFavString:String = newFavArray.join("\r\n");
var favFile:File = File.applicationStorageDirectory.resolvePath("lists/Favourites.txt");
var favStream:FileStream = new FileStream();
favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();
}
which you can run whenever you want to.
If you haven't used File and FileStream before, it'd be worth it to check them out and see how they work. They're only available in AIR but they're much better and superior to URLRequest and URLLoader. You may also want to use them to load your file. They are only useful for local files though, if the file is online or something then I'm sure URLLoader is fine.