Create sequential variable names under SharedObject in ActionScript 3.0 - actionscript-3

How to create multi variables in a SharedObject with ActionScript 3.0, using the same name except with sequential numbers 1-100 behind?

Besides dot notation you can access any object member with square brackets and the member name as a String variable. The following variants will equally invoke the play() method of the current MovieClip.
// Dot notation.
this.play();
// Square brackets.
var playName:String = "play";
this[playName]();
So it goes with any other object.
var SO:SharedObject = SharedObject.getLocal("my_shared_object");
for (var i:int = 1; i <= 100; i++)
{
SO.data["thing" + i] = Math.random();
}
UPD: As #Vesper pointed out, you can store a whole Array which lets you keep the indexed things naturally:
var SO:SharedObject = SharedObject.getLocal("my_shared_object");
SO.data.things = new Array;
for (var i:int = 1; i <= 100; i++)
{
SO.data.things[i] = Math.random();
}

Related

How to make housie / bingo game in Flash AS3

It shows an error when written this script has a package that cannot be nested how can I resolve the problem.
If not can anyone give me a new code so that I can try to make the new file?
I have this file in previous stack AS3 Bingo ticket generator
but i couldnt understan how to use it.
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Main extends Sprite{
public var boards:Array = new Array();
private static const AMAUNT_BOARDS:uint = 6;
private static const NUMBER_FIELD_SIZE:uint = 20;
public function Main() {
for(var i:uint = 0; i < AMAUNT_BOARDS; i++)
{
var numbers:Array = genNumbers();
numbers = deleteFields(numbers);
var board:Sprite = getBoard(numbers);
board.y = NUMBER_FIELD_SIZE * 4 * i;
boards.push(board);
addChild(board);
}
}
//generates a 2 Dimensional Array (3x9) with TextFields
//populates the according Numbers and returns a board Sprite
private function getBoard(n:Array):Sprite
{
var s:Sprite = new Sprite();
var a:Array = new Array();
for(var i:uint = 0; i < 3; i++)
{
var b:Array = new Array();
for(var k:uint = 0; k < 9; k++)
{
//create TextFields
var tf:TextField = new TextField();
tf.x = k * NUMBER_FIELD_SIZE;
tf.y = i * NUMBER_FIELD_SIZE;
tf.border = true;
tf.width = NUMBER_FIELD_SIZE;
tf.height = NUMBER_FIELD_SIZE;
if(n[k][i] != 0) // adds the number if the value isn't 0
tf.text = n[k][i]; // Note that i am switching k & i because the number Array is 9x3
b.push(tf);
s.addChild(tf);
}
}
return s;
}
// Turns 4 random numbers out of the lines into 0 And returns the Array
private function deleteFields(a:Array):Array
{
for(var i:uint = 0; i < 3; i++)
{
var r:RandomPlus = new RandomPlus(8,0);
for(var k:uint = 0; k < 4; k++)
{
var t:uint = r.getNum();
a[t][i] = 0;
}
}
return a;
}
// Generates and returns a 2 Dimensional Array (9x3) with random numbers
private function genNumbers():Array
{
var a:Array = new Array();
var st:uint = 1;
var en:uint = 9;
for(var i:uint = 0; i < 9; i++)
{
var line:Array = new Array();
var ran:RandomPlus = new RandomPlus(en,st);
if(i == 0)//accounting for the number differnenz at start
st--;
if(i==7)//accounting for the number differnenz at end
en ++;
st += 10;
en += 10;
for(var e:uint = 0; e < 3; e++)
line[e] = ran.getNum();
a.push(line);
}
return a;
}
}
}
TL;DR: "Main" in the class definition may have to be changed conflict of already existing name. Extends sprite may need to be changed to extends movieclip. RandomPlus either needs an import you don't have or needs to be made into a symbol and exported to Actionscript.
Ok, so a few things:
If you are really putting the code into the stage code area, then you can't use the private identifier.
Unless RandomPlus is an object that you have defined in the stage it is probably either not a method you can use or you don't have the right imports for it. Look up what imports you need for RandomPlus or if it is an object defined in the stage then you may need to turn it into a symbol or if you have already export it to flash ActionScript I think. To do this you have to check the checkbox in the middle of the symbol creation page. It will always give you the error when you do this, but don't worry it is fine that way.
Third thing is that I never extend sprite in the class definition, I always do extends movieclip(not sure of the capitalization, but you can look that up). You may also be running into an error from using "Main" as the name because it may be a conflict with a name or method already defined in flash in general.
One last thing is for the variable declaration(mostly just for making the code more readable). While it is good to not declare variables as global when you don't have to, I tend to like having most of the variables up at the top, because I like to be able to see most of the declarations in one space. It's not necessary, really just a personal opinion, and I know a lot of experienced coders will say to do what you did, but if you want to use the arrays in multiple functions then sometimes it is easier to just declare them globally rather than having to pass a million things in the function call and then having to figure out later where all the variable declarations are called and where they are being passed as arguments. Again it's more of a coder's choice, but you can also just do whatever you feel more comfortable with rather than just following the already laid out rules by people who have more experience coding.
Another optional fix to the organization might be to name variables something meaningful so as to not forget what they are all doing. I am bad at naming as well, but I think it's really better to name them something better than just a single letter or two.
Another thing that can help is placing trace(code or "text"); in different places to see what's going wrong once you have the compiler happy.

Collision Detection and Arrays (AS3)

I have an array full of bitmaps of spikes and I'm wanting to detect when those spikes collide with the character bitmap. I've read this: http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/
But how would I implement that when I'm using an array to store the spike bitmaps?
See the redClipBmpData.hitTest part? Just put it inside the array loop:
var total:uint = bitmaps.length; // bitmaps is the array
for (var i:uint = 0; i < total; i++) {
var bitmap:Bitmap = bitmaps[i];
var bitmapData:BitmapData = bitmap.bitmapData;
bitmapData.hitTest(...)
}

AS3 Best way to declare variables inside loop

Is it possible better to declare variables without having to write one by one?
I need 200 but... no way.
My code i have:
var var0:Array = new Array();
var var01:Sprite = new Sprite();
var var02:Sprite = new Sprite();
var var03:Sprite = new Sprite();
etc to 200...
And inside for loop:
for( var i:Number = 0; i < arrayBig.length; i++ ){
var0[i] = new Sprite();
...
}
But Why cant I write var var0[i]:Sprite = new Sprite(); in the statments?
The error is Error #1086 Syntax error: expecting semicolon before
Thank you very much!!
You cannot index independent variables and you cannot concatenate variable names to form names of other variables (well, not the way you attempted).
You need to create an array of Sprite objects. An array is a data structure that stores several things at once. (A one-dimensional array is called a vector.)
To store sprites in a vector, you'd write:
var sprites:Vector.<Sprite> = new Vector.<Sprite> ();
for ( var i:int = 0; i < 200; i++ )
{
sprites.push ( new Sprite () );
}
This loop creates 200 Sprite instances and stores all of them in the sprites array (really, Vector). You can then access individual sprites by simply indexing them:
sprites[n]....
where n goes from 0 to N-1 if the Vector has N total elements.
i will answer for your question.
"But Why cant I write var var0[i]:Sprite = new Sprite(); in the statements?"
we cant override one object with that of the other same object in the class. thats the reason, we are declaring as "var0[i] = new Sprite();". spl case for sprite object in flex.

Duplicating/Cloning Vector.<MovieClip>

How do you duplicate the content of the Vector.?
private var slotMC:MovieClip;
private var slotV1:Vector.<MovieClip> = new Vector.<MovieClip>();
private var slotV2:Vector.<MovieClip>;
private var slotV3:Vector.<MovieClip>;
public function InputSlot():void {
registerClassAlias("MovieClip", MovieClip);
for (var i:int = 1; i < typeAmount + 1; i++) {
SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot0' + i) as Class;
slotMC = new SlotClass();
slotMC.name = "slot" + i;
//push to vector before randomly add to stage
slotV1.push(slotMC);
}
slotV2 = clone(slotV1);
trace('slotv2', slotV2);
}
private function clone(source:Vector.<MovieClip>):Vector.<MovieClip> {
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return myBA.readObject() as Vector.<MovieClip>;
}
It returns null for the slotV2.
Or in this case would a multidimensional Vector would be a better way?
I'm not sure what other info I would add into the MC in the future though.
You cannot deep-clone a vector of DisplayObjects of any kind, unless the objects' classes implement IExternalizable. This is because deeply cloning a DisplayObject requires cloning all its references including the entire children list and stage, and you cannot make another stage. Thus, you'd better create your "deep clone" by creating more instances of SlotClass in the cycle and stuffing them into corresponding vectors.
for (var i:int = 1; i < typeAmount + 1; i++) {
SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot0' + i) as Class;
slotMC = new SlotClass();
// slotMC.name = "slot" + i;
// drop name setting, you'd better use position in vector to refer to the clip
//push to vector before randomly add to stage
slotV1.push(slotMC);
slotMC = new SlotClass();
slotV2.push(slotMC);
slotMC = new SlotClass();
slotV3.push(slotMC);
}
Don't forget to initialize the vectors prior to running the cycle.
As a side point, yes it might be better to use a multidimensional vector, considering why do you need these several vectors.

Accessing pieces of bitmaps inside a movieclip

I have a movieclip with 10 bitmaps in it. I wanna access each of them.
myMovieClip is the movieclip containing those 10 bitmaps. I wanna access those bitmaps one by one. All 10 bitmaps are imported separately.
I tried this :
for ( var i =0 ; i< myMovieClip.numChildren ; i++)
{
trace ( myMovieClip.getChildAt(i) );
}
Problem is numChildren comes "1" only, as if it doesnot consider those 10 pieces of bitmap. Any other way, to access those bitmaps ?
thanks
V.
what do you mean by pieces of bitmaps?? Do you mean 10 different bitmap objects are children of the movieClip??
In addition, your code does have a syntax error.
var newMc:MovieClip = MovieClip();
should be:
var newMc:MovieClip = new MovieClip();
second off all, in your loop, numChildren will be always changing since you are taking the reference of a child from the myMoiveClip and moving it to the newMc object. there are two way to fix this.
either set a local variable to the value of myMovieClip.numChildren and use that value in your loop
example:
var numOfChildren:int = myMovieClip.numChildren;
for(var i:int = 0; i < numOfChildren; i++){
var newMc:MovieClip = new MovieClip();
newMc.addChild(myMovieClip.getChildAt(i));
}
this will move the bitmaps out of myMovieClip and into newMc, if you want to keep them there you can create a new bitmap inside the loop and then add the new bitmap to the newMc.
example:
for(var i:int = 0; i < myMovieClip.numChildren; i++){
var newMc:MovieClip = new MovieClip();
var b:Bitmap = new Bitmap(Bitmap(myMovieClip.getChildAt(i)).bitmapData);
newMc.addChild(b);
}