Jump to content
Search Community

Random tween

Luigi Vercotti test
Moderator Tag

Recommended Posts

Hello

Can you please help me with to add Random number...

var fireBallArray:Array=[fireBallCrashed.fireBallPeace01,fireBallCrashed.fireBallPeace03,fireBallCrashed.fireBallPeace05,fireBallCrashed.fireBallPeace07,fireBallCrashed.fireBallPeace09,fireBallCrashed.fireBallPeace11,fireBallCrashed.fireBallPeace13,fireBallCrashed.fireBallPeace15,fireBallCrashed.fireBallPeace17,fireBallCrashed.fireBallPeace19,fireBallCrashed.fireBallPeace02,fireBallCrashed.fireBallPeace04,fireBallCrashed.fireBallPeace06,fireBallCrashed.fireBallPeace08,fireBallCrashed.fireBallPeace10,fireBallCrashed.fireBallPeace12,fireBallCrashed.fireBallPeace14,fireBallCrashed.fireBallPeace16,fireBallCrashed.fireBallPeace18];
var xNumber=Number;
var yNumber=Number;
xNumber = Math.round(Math.random()*(1100-900))+900;
yNumber= Math.round(Math.random()*(-500 - (-350)))+(-350);
TweenMax.allTo(fireBallArray, .8, {y:yNumber,x:xNumber,blurFilter:{blurX:50, blurY:50}}, 0.1);

The code is working fine. I am trying to get the random "x" and "y" - like each of the "fireBallArray:Array" get the random x and y, when it is called every 0.1s.

At this moment it has random "x" and "y" whole "fireBallArray:Array. How should it be, please? ;-)

I've got this fireBall peaces in one mc and I am trying to shatter this ball to one direction. My code does the job but it doesn't look too good when all of the peaces go to one "x" and "y".

Please... ;-)

Link to comment
Share on other sites

Hi Luigi,

Check out Carl Schoof's random number post:

http://www.snorkl.tv/2011/02/use-the-pr ... s-or-else/

 

Basically you can create a random number function like below:

function randomNumber(min:Number, max:Number):Number {
//good
return Math.floor(Math.random() * (1 + max - min) + min);
}

 

then use:

randomNumber(1,5);

to generate the number, in the example above it would be a value between 1 and 5 (inclusive)

 

so in your code you could change:

TweenMax.allTo(fireBallArray, .8, {y:randomNumber(1,5),x:randomNumber(1,5),blurFilter:{blurX:50, blurY:50}}, 0.1);

Replace 1 and 5 with the values on the stage you want your fireball to go to.

 

I hope that helps, let us know how you get on.

X10

Link to comment
Share on other sites

in addition to X10's advice. if you want all the clips to go to different x and y values and not the SAME RANDOM x and y. Then an allTo isn't going to work.

allTo tweens the properties of the items in an array to the SAME values. if you want each tween to have unique values use a loop.

 

 

for (var i = 0; i
TweenMax.to(fireBallArray[i], .8, {y:randomNumber(0,1000) ,x:randomNumber(0,1000) ,blurFilter:{blurX:50, blurY:50, delay:.01*i);
}

function randomNumber(min:Number, max:Number):Number {
  //good
  return Math.floor(Math.random() * (1 + max - min) + min);
}

 

you can pass any values you want into randomNumber, i just used 0 and 1000

Link to comment
Share on other sites

Thank you guys, you are the best ;-)

 

The tutorial from Svorkl is great too.

I tweaked the numbers "x,y" to work well.

 

Now I am trying to move the fireBall (it is the beginnig before the code you helped me with :-) ).

I am trying to move the fireBall, so it look it is coming from distance towards you but not straight but in angle....

I found some code here

ou want to move x to 20*Math.cos(30*Math.PI/180)

 

you want to move y to 20*Math.sin(30*Math.PI/180)

 

The cos and sin gives you the x and y steps to keep your objects moving along 30 degrees.

 

If you want your object to move more change the 20 parameter.

from web site stackoverflow...http://stackoverflow.com/questions/1891397/flash-as3-tweening-at-an-angle

 

so I tried it...myself, but I was changing the numbers but it still goes in line....

var xAngle:Number = 260*Math.cos(26*Math.PI/180);
var yAngle:Number = 180*Math.sin(30*Math.PI/180);

var timeLineFireBall:TimelineLite = new TimelineLite();
timeLineFireBall.insert(TweenMax.to (fireBallCrashed, 4, {x:xAngle,y:yAngle}));

 

Can be done?

I noticed "TweenPlugin.activate([Physics2DPlugin]);" plugin which would do the work perfectly, right?

Link to comment
Share on other sites

That maths give the following results:

 

xAngle = 233.68645203778343
yAngle = 89.99999999999999

So all your code will do is move fireBallCrashed from its current (x,y) position to (233.7, 90). No matter how you work out these angles, that is always going to be a direct line between the two positions. If you are after a z-axis transform, ie animate from smaller to larger, there are a few different transforms you can use for this.

 

 

scaleX, scaleY

fireBallCrashed.scaleX = 0.5; // default = 1
fireBallCrashed.scaleY = 0.5; // default = 1
timeLineFireBall.insert(TweenMax.to (fireBallCrashed, 4, { x:xAngle, y:yAngle, scaleX:1, scaleY:1 } ));

This will give the appearance of fireBallCrashed zooming towards the screen, and in most cases should be pretty performance efficient, assuming fireBallCrashed is a reasonably optimised DisplayObject.

 

 

z

fireBallCrashed.z = -100; // default = 0
timeLineFireBall.insert(TweenMax.to (fireBallCrashed, 4, { x:xAngle, y:yAngle, z:0 } ));

This will apply a Matrix3D transformation to the object, which uses a 'camera' to work out perspective giving the illusion of a 3D space. This tween will look different to a scale tween as the position of fireBallCrashed is relative to the 'camera' and the 'vanishing point'. A DisplayObject with a Matrix3D transform applied will also affect some filters and Greensock plugins. This will probably perform worse than a 2D scale as well.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...