AS2 carousel gallery to AS3 - actionscript-3

I'm making carousel gallery for my class, however we got a code written in AS2 and have to transform it into AS3. I have a problem with two fragments:
// create the objects for the circular motion
for (var i:Number = 0; i < numberOfObjects; i++)
{
this.createEmptyMovieClip('object'+i, i);
this['object'+i].loadMovie('p'+(i+1)+'.jpg');
}
Where I'm adding movie clips as objects, and then I want to use them as:
// create the motion along the circular path
this.addEventListener(Event.ENTER_FRAME, function()
{
// loop over all the objects
for (var i:Number = 0; i < numberOfObjects; i++)
{
thisObj = this['object'+i];
placeObj(thisObj,i);
displayObj(thisObj);
}
})
Of course I get an exception:
Scene 1, Layer 'actions', Frame 1, Line 85 1120: Access of undefined property thisObj.
I know that createEmptyMovieClip as well as loadMovie don't work in AS3, and I probably should make MovieClip as new MovieClip, however I still have troubles how to make this work. I'll be truly grateful for any advices.

You need to apply var keyword before you introduce a new variable.
Correct your as3 code this:
var thisObj:Sprite = this['object'+i];

Related

check if shape is bitmap in as3

I have many single frame movieclips on stage.
some of them are drawn using vector shapes in flash.
some of them are bitmaps loaded from an external .png file. note that in this case the display objects are of type shape, not of type bitmap.
I would like to export the movieclips containing vector shapes to svg documents. and the movieclips with bitmaps to bitmapData.
the problem is I am unable to differentiate between the 2 in actionscript.
I have only had success when converting the shape into Bitmap type with AS Linkage. however, this is not ideal as we would have to inspect all movieclips manually. Is there a way to differentiate between a shape that is vector and a shape that is bitmap?
I found a solution.
ensuring that there is not GraphicsBitmapFill in it was enough for my use case
for(var i :int = 0; i < target.numChildren; i++) {
var s:Shape = target.getChildAt(i) as Shape;
if (s) {
var g:Vector.<IGraphicsData> = s.graphics.readGraphicsData();
for(var j:int = 0; j < g.length;j++) {
var bitmap:GraphicsBitmapFill = g[i] as GraphicsBitmapFill;
if (bitmap) {
hasBitMapChildren = true
}
}
}
}

Play random frames in movie clip actionscript3

I am trying to create small gaming app in flash.
So have one movie clip , inside that I have 4 frames.
I am calling one frame from those four randomly but I need to call it randomly again and again. setTimeout not working in my case, so how can I achieve randomly call any frame out of 4 repeatedly.
function createRandomCoinValues()
{
for(var i = 0; i< 5; i++)
{
var my_timedProcess:Number = setTimeout(createMc(i),4000);
if(my_timedProcess)
{
trace(my_timedProcess);
clearTimeout(my_timedProcess);
}
}
addListeners();
}
function createMc(i:Number)
{
associatedRandomValues[i] = randomNumbers(1,5);
var mc:MovieClip = this.getChildByName("coin"+ (i + 1) + "_mc") as MovieClip;
mc.gotoAndStop(generateRandomAnimation(1,100));
//By generateRandomAnimation() giving input to only either go to vertical or horizontal named frame.
}
Any help suggestion is very important to me. Thanks in advance.
What you need is addFrameScript().
Here is an example, not real code.
mc.addFrameScript( frame, function(){ mc.gotoAndStop(randomFrame) } )
Note: addFrameScript() start from frame 0 and end to totalFrame - 1.

Order of instances in 3D Flash

I'm making cube of 9 cubes in Flash As3. However i cant rotate it properly due to order of indexes whole adding then to stage.
First im creating cube of 6 squares, then wall of 9 cubes, and at the end cube of 3 walls.
It is all fine, however when i rotate it to the left, order of cubes is inverted and it ruins whole composition. I know i coul dinamicly change index based on rotation but it would be a loooooooot of work.
Any ideas how could i do it better way?
Here is actual model:
http://test.mrowa.topdivision.pl/kostka/3DTest.html
If you are using the Flash's display list you would have to sort the sprites based on their z.
Here is some code that would sort the children of a DisplayObjectContainer based on their z position, call this whenever some object changes its position.
public function sortChildren(container:DisplayObjectContainer):void
{
var objects:Vector.<DisplayObject> = new Vector.<DisplayObject>;
for (var i:int = 0; i < container.numChildren; i++)
{
objects.push(container.getChildAt(i));
}
objects.sort(sortCompare);
var index:int = 0;
for (var j:int = 0; j < objects.length; j++)
{
index = container.getChildIndex(objects[j]);
if (index != j)
container.setChildIndex(objects[j], j);
}
}
private function sortCompare(a:DisplayObject, b:DisplayObject):int
{
return (a.z - b.z);
}
You can move the objects member to be a class member and add/remove items to it whenever you add/remove items to/from the stage so that you don't have to fill the whole array every time this function is called.

How do you select a random colortransform from an array and applying it to a movieclip in Flash ActionScript 3?

I am trying to make a Tetris game in ActionScript 3. I am using a movieClip and colorTransform array to create colorful, random, unique pieces. Randomizing the frame in the movie clip works well enough, but when I try to apply a random color tint using the clip's colorTransform property, I get this:
Tetris.as, Line 342 1067: Implicit coercion of a value of type Number to an unrelated type flash.geom:ColorTransform.
Here is some sample code:
private function LandTetromino():void
{
var cT:int = currentTetromino;
var landed:Tetris_Shapes;
for (var i:int=0; i<shapeBuilder[cT][currentRotation].length; i++)
{
for (var j:int=0; j<shapeBuilder[cT][currentRotation][i].length; j++)
{
if (shapeBuilder[cT][currentRotation][i][j]==1)
{
landed = new Tetris_Shapes();
landed.transform.colorTransform = Math.floor(Math.random()*allcolorTransforms.length);
landed.gotoAndStop(Math.floor(Math.random()*12));
addChild(landed);
landed.name="r"+(startingRow+i)+"c"+(startingCol+j);
boardArray[startingRow+i][startingCol+j]=1;
}
}
}
removeChild(tetrisShape);
dropTime.removeEventListener(TimerEvent.TIMER, OnTimeTick);
dropTime.stop();
CheckForCompleteLines();
}
allcolorTransforms is ColorTransform Array?
if right. correct follow code.
landed.transform.colorTransform = allcolorTransforms[Math.floor(Math.random()*allcolorTransforms.length)];

AS3 For statement access movieclip within movieclip

I have added:
sub1_btn
Within sub1_btn there is a movieclip called "arrow".
Using this code I am able to access it and tween it:
TweenMax.to(sub2_btn.arrow, 1, {rotation: -0});
However, using this code within a FOR statement (as there are 2), I am not
for (var i:int = 1; i<3; i++){
TweenMax.to(["sub"+i+"_btn"].arrow, 1, {rotation: -0});
}
What is wrong with the above code? The error is:
Error: Cannot tween a null object.
at com.greensock::TweenLite()
at com.greensock::TweenMax()
at com.greensock::TweenMax$/to()
at src::main/pullSub()
Try this instead:
for (var i:int = 1; i<3; i++){
TweenMax.to(this["sub"+i+"_btn"].arrow, 1, {rotation: -0});
}
The problem is that ["sub"+i+"_btn"] creates a new array, and that array doesn't contain an object arrow. But when you use this["sub"+i+"_btn"] you access the movie clip sub[i]_btn as you want.