Jump to content
Search Community

Random Movement

Luigi Vercotti test
Moderator Tag

Recommended Posts

Hello guys

I've this code which is working fine...the issue is... I've got those "boxesFlying.boxesFlying" ...about 70, so I know I've have to use array..? the second issue I've got is that the random is fine but those boxes act same random x and y so I've changed the delay time, but again if I will have to do about 70mc I will be sitting here all night... :-(

Can you please help me... ;-) to simplify and modify, please ;-)

var min:Number = -15;
var max:Number = 15;
var fiveToFive:Number = Math.round(Math.random()*(1+ max - min) + min);
var p49:Number = boxesFlying.boxesFlying49.x;
var p49Y:Number = boxesFlying.boxesFlying49.y;
var p40X:Number = boxesFlying.boxesFlying40.x;
var p40Y:Number = boxesFlying.boxesFlying40.y;



var boxesFlyingTweenSpace40:TimelineLite = new TimelineLite();
boxesFlyingTweenSpace40.insert(new TweenLite(boxesFlying.boxesFlying40,3,{x:p40X + fiveToFive,ease:Bounce.easeOut}));
boxesFlyingTweenSpace40.append(new TweenLite(boxesFlying.boxesFlying40,3,{y:p40Y + fiveToFive,ease:Bounce.easeOut}),-2.5);
boxesFlyingTweenSpace40.append(new TweenLite(boxesFlying.boxesFlying40,3,{x:p40X - fiveToFive,ease:Bounce.easeOut}),-2);
boxesFlyingTweenSpace40.append(new TweenLite(boxesFlying.boxesFlying40,3,{y:p40Y - fiveToFive,ease:Bounce.easeOut}),-2.5);
boxesFlyingTweenSpace40.append(new TweenLite(boxesFlying.boxesFlying40,3,{x:-59.30, y:-46}),-2);


   var boxesFlyingTweenSpace:TimelineLite = new TimelineLite();
   boxesFlyingTweenSpace.insert(new TweenLite(boxesFlying.boxesFlying49,2.5,{x:p49 + fiveToFive,ease:Bounce.easeOut}));
   boxesFlyingTweenSpace.append(new TweenLite(boxesFlying.boxesFlying49,3,{y:p49Y + fiveToFive,ease:Bounce.easeOut}),-2.4);
   boxesFlyingTweenSpace.append(new TweenLite(boxesFlying.boxesFlying49,3,{x:p49 - fiveToFive,ease:Bounce.easeOut}),-2);
   boxesFlyingTweenSpace.append(new TweenLite(boxesFlying.boxesFlying49,3,{y:p49Y - fiveToFive,ease:Bounce.easeOut}),-2.9);
   boxesFlyingTweenSpace.append(new TweenLite(boxesFlying.boxesFlying49,3,{x:51.50, y:-66}),-2);

Link to comment
Share on other sites

assuming all boxes are named boxesFlying.boxesFlying1 through boxesFlying.boxesFlying70

 

 

import com.greensock.*;
import com.greensock.easing.*;

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

var masterTimeline:TimelineLite = new TimelineLite();

for(var i:int = 0; i
var childTimeline:TimelineMax = new TimelineMax({repeat:10, yoyo:true});

//get a reference to each movieclip in flyingBoxes
var mc:MovieClip = flyingBoxes["flyingBoxes" + i] as MovieClip


//tween to random x and y at same time
childTimeline.insertMultiple([
		    new TweenLite( mc, 2, { x:String(randomNumber(-200, 200)),  ease:Bounce.easeOut}),
                            new TweenLite( mc, 2, { y:String(randomNumber(-200, 200)),  ease:Bounce.easeOut})
						   ])

//tween to another randomx and y at same time (after previous tweens occur);

childTimeline.appendMultiple([
		     new TweenLite( mc, 1, { x:String(randomNumber(-200, 200)),  ease:Bounce.easeOut}),
                            new TweenLite( mc, 1, { y:String(randomNumber(-200, 200)),  ease:Bounce.easeOut})
						   ])
masterTimeline.insert(childTimeline);
}

 

that will make each movie clip do a bunch of random stuff over and over again. its a bit technical, but ultimately easier than 800+ lines of code.

 

to explain it all would be like a master class in TimelineMax.

 

I recommend you learn about loops and then read the timelinelite/max documentation thoroughly and focus on the

 

append, appendMultiple, insert, insertMultiple methods.

 

give it a shot

Link to comment
Share on other sites

OK, here is what I came up with...still it is long way from I wish but I think I am on good track...hopefully...

I used some of your codes ;-) thanks for the advices...well It seems simple code but I really spent some long time on it :-)

var boxesFlyingPictures:Array = [boxesFlying.boxesFlying01,boxesFlying.boxesFlying02,boxesFlying.boxesFlying03,boxesFlying.boxesFlying04,boxesFlying.boxesFlying100,boxesFlying.boxesFlying101,boxesFlying.boxesFlying10,boxesFlying.boxesFlying11,boxesFlying.boxesFlying12,boxesFlying.boxesFlying13,boxesFlying.boxesFlying130,boxesFlying.boxesFlying27,boxesFlying.boxesFlying28,boxesFlying.boxesFlying29,boxesFlying.boxesFlying90,boxesFlying.boxesFlying91,boxesFlying.boxesFlying92,boxesFlying.boxesFlying93,boxesFlying.boxesFlying94,boxesFlying.boxesFlying68,boxesFlying.boxesFlying69,boxesFlying.boxesFlying75,boxesFlying.boxesFlying76,boxesFlying.boxesFlying77,boxesFlying.boxesFlying78,boxesFlying.boxesFlying79,boxesFlying.boxesFlying80,boxesFlying.boxesFlying81,boxesFlying.boxesFlying82,boxesFlying.boxesFlying83,boxesFlying.boxesFlying70,boxesFlying.boxesFlying14,boxesFlying.boxesFlying24,boxesFlying.boxesFlying25,boxesFlying.boxesFlying26,boxesFlying.boxesFlying40,boxesFlying.boxesFlying41,boxesFlying.boxesFlying42,boxesFlying.boxesFlying43,boxesFlying.boxesFlying44,boxesFlying.boxesFlying45,boxesFlying.boxesFlying46,boxesFlying.boxesFlying47,boxesFlying.boxesFlying48,boxesFlying.boxesFlying30,boxesFlying.boxesFlying31,boxesFlying.boxesFlying32,boxesFlying.boxesFlying33,boxesFlying.boxesFlying49,boxesFlying.boxesFlying71,boxesFlying.boxesFlying120,boxesFlying.boxesFlying121,boxesFlying.boxesFlying122,boxesFlying.boxesFlying123,boxesFlying.boxesFlying124,boxesFlying.boxesFlying15,boxesFlying.boxesFlying88,boxesFlying.boxesFlying89,boxesFlying.boxesFlying60,boxesFlying.boxesFlying61,boxesFlying.boxesFlying16,boxesFlying.boxesFlying17,boxesFlying.boxesFlying18,boxesFlying.boxesFlying19,boxesFlying.boxesFlying20,boxesFlying.boxesFlying21,boxesFlying.boxesFlying110,boxesFlying.boxesFlying111,boxesFlying.boxesFlying112,boxesFlying.boxesFlying113,boxesFlying.boxesFlying22,boxesFlying.boxesFlying23,boxesFlying.boxesFlying72,boxesFlying.boxesFlying73,boxesFlying.boxesFlying74,boxesFlying.boxesFlying50,boxesFlying.boxesFlying51,boxesFlying.boxesFlying52,boxesFlying.boxesFlying95,boxesFlying.boxesFlying96,boxesFlying.boxesFlying97,boxesFlying.boxesFlying98,boxesFlying.boxesFlying99,boxesFlying.boxesFlying55,boxesFlying.boxesFlying56,boxesFlying.boxesFlying57,boxesFlying.boxesFlying58,boxesFlying.boxesFlying85,boxesFlying.boxesFlying86,boxesFlying.boxesFlying87,boxesFlying.boxesFlying65,boxesFlying.boxesFlying66,boxesFlying.boxesFlying67,boxesFlying.boxesFlying105];


//generate random num between a min and max value;
function randomRange(min:Number, max:Number):Number
{
return min + (Math.random() * (max - min));
}

//how much wiggling / zig zagging
var wiggle:Number = randomRange(-5,5);
//zig or zag?
wiggle = Math.random() > .5 ?  -  wiggle:wiggle;

var time:Number = Math.random() * 3;
for (var sem=0; sem{
//record position
var tadyX:Number = boxesFlyingPictures[sem].x;
var tadyY:Number = boxesFlyingPictures[sem].y;
//random position
var randP:Number = Math.random() * 3;

var timeLineSemTam:TimelineLite = new TimelineLite();
timeLineSemTam.insert(new TweenLite(boxesFlyingPictures[sem],time, {x:tadyX+wiggle,ease:Bounce.easeOut,repeat:-1,repeatDelay:0.2}),sem*.1);
timeLineSemTam.append(new TweenLite(boxesFlyingPictures[sem],time, {y:tadyY+wiggle,ease:Bounce.easeOut,repeat:-1,repeatDelay:0.2}),sem*.1);
trace("position of wiggle" +wiggle);
trace("time"+time);

}

well now it at least works.

The repeat doesn't work for some reason :-(

1) I you can please help me, how can I record the original position of the arrays mc, so it can go back.

2) If I may please... :-) how can be each of the movement of the arrays mc be random...when I trace the random, it same for all of them at this moment.

Link to comment
Share on other sites

Carl you might to start to hate me but I just noticed the code you wrote me earlier...I did not notice scroller!!! which I feel very stupid now :-(

anyway I tried the code with simple boxes which are on the stage...five of them named box1-box5

When I test it...it does nothing and after few milliseconds errors are jumping at me...

TypeError: Error #1006: value is not a function.

at com.greensock::TweenLite/renderTime()

at com.greensock.core::SimpleTimeline/renderTime()

at com.greensock::TweenLite$/updateAll()

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at com.greensock::TweenLite/init()

at com.greensock::TweenLite/renderTime()

at com.greensock.core::SimpleTimeline/renderTime()

at com.greensock::TweenLite$/updateAll()

here is the whole code I did not noticed...sorry...

import com.greensock.*;
import com.greensock.easing.*;


//var boxesFlyingPictures:Array = [boxesFlying.boxesFlying01,boxesFlying.boxesFlying02,boxesFlying.boxesFlying03,boxesFlying.boxesFlying04,boxesFlying.boxesFlying100,boxesFlying.boxesFlying101,boxesFlying.boxesFlying10,boxesFlying.boxesFlying11,boxesFlying.boxesFlying12,boxesFlying.boxesFlying13,boxesFlying.boxesFlying130,boxesFlying.boxesFlying27,boxesFlying.boxesFlying28,boxesFlying.boxesFlying29,boxesFlying.boxesFlying90,boxesFlying.boxesFlying91,boxesFlying.boxesFlying92,boxesFlying.boxesFlying93,boxesFlying.boxesFlying94,boxesFlying.boxesFlying68,boxesFlying.boxesFlying69,boxesFlying.boxesFlying75,boxesFlying.boxesFlying76,boxesFlying.boxesFlying77,boxesFlying.boxesFlying78,boxesFlying.boxesFlying79,boxesFlying.boxesFlying80,boxesFlying.boxesFlying81,boxesFlying.boxesFlying82,boxesFlying.boxesFlying83,boxesFlying.boxesFlying70,boxesFlying.boxesFlying14,boxesFlying.boxesFlying24,boxesFlying.boxesFlying25,boxesFlying.boxesFlying26,boxesFlying.boxesFlying40,boxesFlying.boxesFlying41,boxesFlying.boxesFlying42,boxesFlying.boxesFlying43,boxesFlying.boxesFlying44,boxesFlying.boxesFlying45,boxesFlying.boxesFlying46,boxesFlying.boxesFlying47,boxesFlying.boxesFlying48,boxesFlying.boxesFlying30,boxesFlying.boxesFlying31,boxesFlying.boxesFlying32,boxesFlying.boxesFlying33,boxesFlying.boxesFlying49,boxesFlying.boxesFlying71,boxesFlying.boxesFlying120,boxesFlying.boxesFlying121,boxesFlying.boxesFlying122,boxesFlying.boxesFlying123,boxesFlying.boxesFlying124,boxesFlying.boxesFlying15,boxesFlying.boxesFlying88,boxesFlying.boxesFlying89,boxesFlying.boxesFlying60,boxesFlying.boxesFlying61,boxesFlying.boxesFlying16,boxesFlying.boxesFlying17,boxesFlying.boxesFlying18,boxesFlying.boxesFlying19,boxesFlying.boxesFlying20,boxesFlying.boxesFlying21,boxesFlying.boxesFlying110,boxesFlying.boxesFlying111,boxesFlying.boxesFlying112,boxesFlying.boxesFlying113,boxesFlying.boxesFlying22,boxesFlying.boxesFlying23,boxesFlying.boxesFlying72,boxesFlying.boxesFlying73,boxesFlying.boxesFlying74,boxesFlying.boxesFlying50,boxesFlying.boxesFlying51,boxesFlying.boxesFlying52,boxesFlying.boxesFlying95,boxesFlying.boxesFlying96,boxesFlying.boxesFlying97,boxesFlying.boxesFlying98,boxesFlying.boxesFlying99,boxesFlying.boxesFlying55,boxesFlying.boxesFlying56,boxesFlying.boxesFlying57,boxesFlying.boxesFlying58,boxesFlying.boxesFlying85,boxesFlying.boxesFlying86,boxesFlying.boxesFlying87,boxesFlying.boxesFlying65,boxesFlying.boxesFlying66,boxesFlying.boxesFlying67,boxesFlying.boxesFlying105];


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

var masterTimeline:TimelineLite = new TimelineLite();

for (var i:int = 0; i<=5; i++)
{

var childTimeline:TimelineMax = new TimelineMax({repeat:10,yoyo:true});

//get a reference to each movieclip in flyingBoxes
var mc:MovieClip = ["box"+ i] as MovieClip;

//tween to random x and y at same time
childTimeline.insertMultiple([
             new TweenLite( mc, 2, { x:String(randomNumber(-200, 200)),  ease:Bounce.easeOut}),
                             new TweenLite( mc, 2, { y:String(randomNumber(-200, 200)),  ease:Bounce.easeOut})
                        ]);

//tween to another randomx and y at same time (after previous tweens occur);

childTimeline.appendMultiple([
              new TweenLite( mc, 1, { x:String(randomNumber(-200, 200)),  ease:Bounce.easeOut}),
                             new TweenLite( mc, 1, { y:String(randomNumber(-200, 200)),  ease:Bounce.easeOut})
                        ]);
masterTimeline.insert(childTimeline);
}

Link to comment
Share on other sites

the main probelem is that your instances are box1 - box5

 

but you look starts counting at 0 so the first childTimeline you create is trying to tween box0... which doesn't exist.

 

attached is a working file : Flash Cs4

 

Carl

Link to comment
Share on other sites

Well Carl...it's so cool. I played with the code a bit for some time and I think it is something I wished for :-)

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

var masterTimeline:TimelineLite = new TimelineLite();

for (var i:int = 0; i<=10; i++)
{

var childTimeline:TimelineMax = new TimelineMax({repeat:10,yoyo:true});

//get a reference to each movieclip in flyingBoxes
var mc:MovieClip = this["box" + i] as MovieClip;

//tween to random x and y at same time
childTimeline.insertMultiple([
             new TweenMax( mc, 1, { x:String(randomNumber(-20, 20)),  ease:Bounce.easeOut,alpha:.5, blurFilter:{blurX:String(randomNumber(10, 50)), blurY:String(randomNumber(0, 50))}}),
                             new TweenMax( mc, 2, { y:String(randomNumber(-20, 20)),  ease:Bounce.easeIn, blurFilter:{blurX:String(randomNumber(10, 50)), blurY:String(randomNumber(0, 50))}})
                        ],i*.1);

//tween to another randomx and y at same time (after previous tweens occur);

childTimeline.appendMultiple([
              new TweenMax( mc, 3, { x:String(randomNumber(-20, 20)),  ease:Bounce.easeOut,alpha:1, blurFilter:{blurX:0, blurY:0}}),
                             new TweenMax( mc, 1, { y:String(randomNumber(-20, 20)),  ease:Bounce.easeIn, blurFilter:{blurX:0, blurY:0}})
                        ],i*.2);
masterTimeline.insert(childTimeline);
trace ("position of box1...x ", box1.x, "...y ", box1.y);
}

I put some delayed time which look more random and some blur and alpha. I will see how it will look in my project but it is working I was hoping for. Thank you very much ;-)

Let me ask one more question though, please...when I disabled the line "masterTimeline.insert(childTimeline);" in preview I don't see any difference, is it for another loop? why can not see it? or what does it do?

Thanks and have a nice Sunday ;-)

Link to comment
Share on other sites

good question.

 

the childTimeline will play regardless of whether or not it is in a master timeline.

 

by putting it in the master timeline you gain the ability to tell the master timeline to pause() or reverse or adjust the playback rate with timeScale and then all the child timelines will obey.

 

if in the future you wanted to stop all the animations or control them all, you can do it through masterTimeline.

 

Carl

Link to comment
Share on other sites

  • 5 weeks later...

Hello Carl

I've made the flying boxes with your great help and it is working very fine. If you have a minute, can you please take a look at it and tell what do you thing. I wish it would look more "space look" or more space feel...

;-)

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
TweenPlugin.activate([TintPlugin]);


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

var masterTimeline:TimelineLite = new TimelineLite();

for (var i:int = 0; i<=89; i++)
{

var childTimeline:TimelineMax = new TimelineMax({repeat:10,yoyo:true});

//get a reference to each movieclip in flyingBoxes
var mc:MovieClip = this["bxFl" + i] as MovieClip;

//tween to random x and y at same time
childTimeline.insertMultiple([
             new TweenMax( mc, 2, { x:String(randomNumber(-3, 3)),  ease:Bounce.easeOut,alpha:.5, blurFilter:{blurX:String(randomNumber(10, 30)), blurY:String(randomNumber(0, 50))}}),
                             new TweenMax( mc, 2, { y:String(randomNumber(-3, 3)),  ease:Bounce.easeIn, blurFilter:{blurX:String(randomNumber(10, 30)), blurY:String(randomNumber(0, 50))}})
                        ],i*.5);

//tween to another randomx and y at same time (after previous tweens occur);

childTimeline.appendMultiple([
              new TweenMax( mc, 3, { x:String(randomNumber(-3, 3)),  ease:Bounce.easeOut,alpha:1, blurFilter:{blurX:0, blurY:0}}),
                             new TweenMax( mc, 1, { y:String(randomNumber(-3, 3)),  ease:Bounce.easeIn, blurFilter:{blurX:0, blurY:0}})
                        ],i*.8);
masterTimeline.insert(childTimeline);
}

 

[attachment=0]flying_boxes_v10_space.rar[/attachment]

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...