[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

a range for a random animation AS3

Posted on 2008-06-13
11
Medium Priority
?
1,206 Views
Last Modified: 2013-11-11
Hi
i am using an animation which uses the random property for the x and the y. the problem: it uses the space beyond the stage.
i need to create a range that limits the randomness of the animation. tries several things but its not working.

i attach my code.


best regards

ron
import caurina.transitions.*;
 
 
var holder_mc:Sprite = new Sprite();
holder_mc.alpha =0; 
 
var widthRange:Number = 200;
var heightRange:Number = 200;
 
function dropPics ():void {
	for(var i:int =0 ; i < 8; i++){
	holder_mc = new Sprite();
	holder_mc.graphics.beginFill(0x000000);
	holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
	holder_mc.graphics.endFill();
	//trace(i);
	holder_mc.name = "holder" + i;
	Tweener.addTween(holder_mc,{height: 200,width:200 ,alpha: 1,time: 1,transition: "easeOut"});
	var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
	holder_mc.filters = [ds];
	addChild(holder_mc);
	holder_mc.buttonMode = true;
	
	if(x < widthRange  && y < heightRange){
	y += y+ Math.random();
	x += x+ Math.random();
	holder_mc.x -= x+ Math.random()* 380;
	holder_mc.y -= y+ Math.random()* 250;
	}
	
	holder_mc.addEventListener(MouseEvent.CLICK ,tellname);
	}
}
 
 
dropPics();
 
function tellname (e:MouseEvent):void {
	trace(e.target.name);
}

Open in new window

0
Comment
Question by:derrida
[X]
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
  • 5
  • 4
  • 2
11 Comments
 
LVL 12

Expert Comment

by:SamuelRostol
ID: 21778253
Hi there,

I don't have time to test it - but the solution should be something like:

<code snippet>

Try it out :)

Kindly,
Samuel
holder_mc.x -= x+ Math.random()* 380;
holder_mc.y -= y+ Math.random()* 250;
 
// New code, tests position
if (holder_mc.x < 0) holder_mc.x = 0;
if (holder_mc.y < 0) holder_mc.y = 0;
if ((holder_mc.x + holder_mc.width) < stage.stageWidth) holder_mc.x = stage.stageWidth- holder_mc.width;
if ((holder_mc.y + holder_mc.height) < stage.stageHeight) holder_mc.y = stage.stageHeight - holder_mc.height;

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21778927
hi
thanks for the answer, but it does not work. i tried to play with the code you gave (which specify more the boundaries than my code) but i cannot make it work.

any suggestion?

ron
0
 
LVL 12

Expert Comment

by:SamuelRostol
ID: 21779903
Could you upload the swf so that we can see the effect without my code? :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 21781340
Are you sure that this line is OK?

holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);

This means every Sprite you create will be 200 pixels bigger than your movie entire width and height, thus always being outside. (Aside from the random x and y positions)

-V
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 21781359
Ah, nevermind, I suppose that the custom Tweener class makes an effect so they're 200x200.

Knowing that, I think you'll need a onMotionFinished handler and then check the x and y positions with the code SamuelRostol provided.

-V
0
 
LVL 1

Author Comment

by:derrida
ID: 21781622
hi
first of all,you are right about the tween. i have entered that code on the onComplete but it still does not limit itself to the stage size. i played with the code again and again, it all looks so reasonable but it will not work.

i uploaded the files (fla and swf) here:
https://filedb.experts-exchange.com/incoming/ee-stuff/7179-TweenerGallery.zip

thanks for the help

best regards

ron
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 21782065
Maybe something like the following will do the trick:

(I used the normal Tween class you can safely replace those two Tweens for your custom Tweener class)

<code>

-V
//import caurina.transitions.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
 
var holder_mc:Sprite = new Sprite();
holder_mc.alpha =0; 
 
var widthRange:Number = 200;
var heightRange:Number = 200;
 
function dropPics ():void {
	for(var i:int =0 ; i < 8; i++){
	holder_mc = new Sprite();
	holder_mc.graphics.beginFill(0x000000);
	holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
	holder_mc.graphics.endFill();
	//trace(i);
	holder_mc.name = "holder" + i;
	y = Math.random()*200;
	x = Math.random()*200;
	holder_mc.x = x;//+ Math.random()* 500;
	holder_mc.y = y;//+ Math.random()* 500;
	holder_mc.x > 240 ? holder_mc.x -= widthRange : false;
	holder_mc.y > 120 ? holder_mc.y -= heightRange : false;
	holder_mc.x < 0 ? holder_mc.x = 0 : false;
	holder_mc.y < 0 ? holder_mc.y = 0 : false;
	var heightTween:Tween = new Tween(holder_mc, "height", Strong.easeOut, holder_mc.height, 100, 1, true);
	var widthTween:Tween = new Tween(holder_mc, "width", Strong.easeOut, holder_mc.width, 100, 1, true);
	//Tweener.addTween(holder_mc,{height: 100,width:100 ,alpha: 1,time: 1,transition: "easeOut",onComplete:rangeit()});
	var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
	holder_mc.filters = [ds];
	addChild(holder_mc);
	holder_mc.buttonMode = true;
 
	holder_mc.addEventListener(MouseEvent.CLICK ,tellname);
	}
}
 
dropPics();
 
function tellname (e:MouseEvent):void {
	trace(e.target.name);
}

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21782189
Hi
thanks for the help. it does scatter and does not cross the stage, but i want a bigger scattering and when i add to the:
y = Math.random()*450;
x = Math.random()*300;
it allow for a bigger scattering but then again, goes beyond the boundaries of the stage.
so how can i widen the scattering without losing the stage?

i guess we are right before solving it, i hope:)

thanks for your help

best regards

ron
0
 
LVL 18

Accepted Solution

by:
Antonio Estrada earned 2000 total points
ID: 21782740
Yep, we're quite close :)

Try the following:

<code>

-V
//import caurina.transitions.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
 
var holder_mc:Sprite = new Sprite();
holder_mc.alpha =0; 
 
var widthRange:Number = 200;
var heightRange:Number = 200;
 
function dropPics ():void {
	for(var i:int =0 ; i < 8; i++){
	var tx:Number = 0;
	var ty:Number = 0;
	holder_mc = new Sprite();
	holder_mc.graphics.beginFill(0x000000);
	holder_mc.graphics.drawRect(0,0, stage.stageWidth + 200,stage.stageHeight + 200);
	holder_mc.graphics.endFill();
	//trace(i);
	holder_mc.name = "holder" + i;
	ty = Math.random()*450;
	tx = Math.random()*300;
	holder_mc.x = tx;
	holder_mc.y = ty;
	holder_mc.x > 240 ? holder_mc.x -= widthRange : false;
	holder_mc.y > 280 ? holder_mc.y -= heightRange : false;
	holder_mc.x < 0 ? holder_mc.x = 0 : false;
	holder_mc.y < 0 ? holder_mc.y = 0 : false;
	//Tweener.addTween(holder_mc,{height: 100,width:100 ,alpha: 1,time: 1,transition: "easeOut",onComplete:rangeit()});
	var heightTween:Tween = new Tween(holder_mc, "height", Strong.easeOut, holder_mc.height, 100, 1, true);
	var widthTween:Tween = new Tween(holder_mc, "width", Strong.easeOut, holder_mc.width, 100, 1, true);
	var ds:DropShadowFilter = new DropShadowFilter(5,45,0x999999,0.5,4,4,1,5);
	holder_mc.filters = [ds];
	addChild(holder_mc);
	holder_mc.buttonMode = true;
 
	holder_mc.addEventListener(MouseEvent.CLICK ,tellname);
	}
}
 
dropPics();
 
function tellname (e:MouseEvent):void {
	trace(e.target.name);
}

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21784358
thanks :)
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 21784398
You're welcome, glad it worked for you ;)

-V
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
Suggested Courses

650 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