Collision Detection and Arrays (AS3) - actionscript-3

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(...)
}

Related

Create sequential variable names under SharedObject in ActionScript 3.0

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();
}

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.

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);
}

Actionscript 3: Slice a Bitmap into tiles

I have the Bitmap data from a Loader object and I would like to slice it up into 32x32 squares to use as tiles. What is the most efficient way to do so?
I have done the work for you. The basic idea is to use the BitmapData function copyPixels, see Adobe Reference for the same . It lets you copy pixels from one region(specified by a Rectangle) in the source BitmapData, to a specific Point in the destination BitmapData. I have created a new BitmapData for each 32x32 square and looped through the loaded object to populate the squares with copyPixels.
var imageLoader:Loader;
function loadImage(url:String):void
{
// Set properties on my Loader object
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
}
loadImage("Cow Boy.jpg");//--------->Replace this by your image. I hope you know to specify path.
function imageLoaded(e:Event):void
{
// Load Image
imageArea.addChild(imageLoader);
var mainImage:BitmapData = new BitmapData(imageArea.width,imageArea.height);
var tileX:Number = 36;
var tileY:Number = 36;
var bitmapArray:Array;
var tilesH:uint = Math.ceil(mainImage.width / tileX); // Number of Columns
var tilesV:uint = Math.ceil(mainImage.height / tileY);// Number of Rows
mainImage.draw(imageArea);
imageArea.x += 500;
bitmapArray = new Array();
for (var i:Number = 0; i < tilesH; i++)
{
bitmapArray[i] = new Array();
for (var n:Number = 0; n < tilesV; n++)
{
var tempData:BitmapData=new BitmapData(tileX,tileY);
var tempRect = new Rectangle((tileX * i),(tileY * n),tileX,tileY);
tempData.copyPixels(mainImage,tempRect,new Point(0,0));
bitmapArray[i][n]=tempData;
}
}
for (var j:uint =0; j<bitmapArray.length; j++)
{
for (var k:uint=0; k<bitmapArray[j].length; k++)
{
var bitmap:Bitmap=new Bitmap(bitmapArray[j][k]);
this.addChild(bitmap);
bitmap.x = (j+1)* bitmap.width + j*10;
bitmap.y = (k+1)* bitmap.height + k*10;
}
}
}
function imageLoading(e:ProgressEvent):void
{
// Use it to get current download progress
// Hint: You could tie the values to a preloader :)
}
You can use the BitmapData function copyPixels, see this link. It lets you copy pixels from one region(specified by a Rectangle) in the source BitmapData, to a specific Point in the destination BitmapData. Basically you could just create a new BitmapData for each 32x32 square and loop through the loaded object to populate the squares with copyPixels.

How to Create Sound Object Dynamically

How to create sound object dynamically in one movieClip.
Example
for(i=1;i<5;i++){var sound + i = new Sound();}
You can try putting all the sounds from your loop inside an Array:
var soundArray:Array = [];
for (var i:uint = 0; i < 5; i++) {
var sound:Sound = new Sound();
// don't forget to set the path of the file you want to play
soundArray.push(sound);
}
To play the the sound, all you need to do is take note of the index:
Sound(soundArray[0]).play();
Hope this helps.
irot
you need a MovieClip to store your sounds into then:
var mc:MovieClip = new MovieClip();
for( var i:int = 0; i < 5; i++ )
{
mc[ 'sound_' + i ] = new Sound();
}
to access a sound, you can then call:
mc[ 'sound_0' ].play();
if you're already in the scope of a MovieClip, ignore the mc creation and replace 'mc' with 'this' in the loop.