Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

looping a sprite AS3

Posted on 2008-06-12
10
1,004 Views
Last Modified: 2008-11-15
Hi
my question should have been so simple but somehow i get it wrong.
i need to loop through and put on the stage 8 instances of a sprite.
it adds 1 visually but it does loop it 8 times. and the sprites are not scattered around like i need them to do.

i attach the code

i have another question: how do i set a registration point for the sprite with code?

best regards

ron
import caurina.transitions.*;
 
var holder_mc:Sprite = new Sprite();
holder_mc.graphics.beginFill(0x000000);
holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
holder_mc.graphics.endFill();
 
 
var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
holder_mc.filters = [ds];
holder_mc.alpha =0;
 
function dropPics ():void {
	Tweener.addTween(holder_mc,{height: 100,width:100,alpha: 1,time: 1,transition: "easeOut"});
	for(var i:int =0 ; i < 8; i++){
	//trace(i);
	addChild(holder_mc);
	holder_mc.x += 5;
	holder_mc.y += 5;
	}
}
 
 
dropPics();

Open in new window

0
Comment
Question by:derrida
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:section25
ID: 21769571
I don't do AS3, but for what it is worth, it looks like you keep adding the same movie clip "holder_mc" as a child. Maybe you could try naming the clip something different each pass. Like ["holder_mc+i] and add the addTween as part of the loop. Don't you need a separate tween for each child?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 21773507
You need to create a new one each time ...

 You can make the code more elegant with an Array or an Object

WC
var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
 
function dropPics ():void
{
	var holder_mc:Sprite = new Sprite();
	holder_mc.graphics.beginFill(0x000000);
	holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
	holder_mc.graphics.endFill();
	holder_mc.filters = [ds];
	holder_mc.alpha =0;	
	
	Tweener.addTween(holder_mc,{height: 100,width:100,alpha: 1,time: 1,transition: "easeOut"});
	for( var i:int=0 ; i < 8; i++)
	{
		//trace(i);
		addChild(holder_mc);
		holder_mc.x += 5;
		holder_mc.y += 5;
	}
}
 
 
dropPics();

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21776559
hi
i have tried your code ,and like mine, the i is ok but i get on stage only one holder_mc.
i think maybe it is the name (just read it) so i go to check that.

best regards

ron
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:derrida
ID: 21776705
hi
i managed to fix the problem myself (remembering that once i had the same problem) but i do nor understand why it works (did not understood it then and do not understand it now:
i just need to loop the x and y of the stage itself, like that:
y += 110;
x += 110;
holder_mc.y -= y+50;
holder_mc.x -= x+50;
hope someone can explain this, cause it is a really a bizarre idea.

best regards

ron
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 21781011
I see what is going wrong
holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);

This actuall draw a rectangle BIGGER than the stage

holder_mc.x += 5;
holder_mc.y += 5;

Draws a rectangel the SAME COLOR and places it on top of the other one since it has no border it all just looks like one big rectangle

try this line instead
holder_mc.graphics.drawRect(0,0, stage.stageWidth - 200,stage.stageHeight -200);



0
 
LVL 1

Author Comment

by:derrida
ID: 21781453
hi
thanks for the answer but as you can see i managed to fix the problem. the rectangle SHOULD be BIGGER than the stage cause it animate into position.


ron
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 21781663
Ah ok gotcha so you want a bunch of tiles to animate in and look like they are stacked ... are you wrting a card game :)
0
 
LVL 1

Author Comment

by:derrida
ID: 21781735
Hi
yes that is my aim but its a gallery (it will be).

ron
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 21781789
I see what you want. I revisted my code above looks like I pasted the wrong thing.

Try this on below

import caurina.transitions.*;
 
 
 
 
var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
 
 
function dropPics ():void {
 
	for(var i:int =0 ; i < 8; i++){
	//trace(i);
var holder_mc:Sprite = new Sprite();
holder_mc.graphics.beginFill(0x000000);
holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
holder_mc.graphics.endFill();
holder_mc.filters = [ds];
holder_mc.alpha =0;
addChild(holder_mc);
holder_mc.x = 0;
holder_mc.x = 0;
	Tweener.addTween(holder_mc,{height: 100,width:100,alpha: 1,time: 1,transition: "easeOut"});
	holder_mc.x += (i*5);
	holder_mc.y += (i*5);
	}
}
 
 
dropPics();

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21781935
hi
it is looping and i can make is scatter before that (obviously i appreciate the help). my problem now is to restrict the scattering to the stage size. seem easy but no:).

if you like you can continue to help via this question:
http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/Q_23482361.html

since i solve this problem myself and then got stuck in that problem i asked a new question.

best regards

ron
 
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pushing Adobe Flash Player Fails with Error %%2 1 489
How  to reduce the .AVI file size of 90GB 4 354
System Center Configuration Manager 2012R2 7 90
Actionscript in Flash 3 70
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question