smooth random movement and collision detection

  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

Hi all

Hopefully you can help, I have a series of buttons, I want them to all randomly and smoothly move around a variable axis from their start point on the stage.

On top of this I need the buttons to be able to detect if they collide with each other and move away from each other.

Thanks in advance

NCI
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Mp2D
  • Newbie
  • Newbie
  • Mp2D
  • Posts: 8

Post 3+ Months Ago

i think the only objects that can move around are movieclips, so convert the object to button then convert it again to movieclip, it will still be clickable...but you might encounter some problems later on...to make them move at random, use <variable>=random() then make them move to a certain direction, like if (<variable>=1)(_x+=1), (use 1 or less for smooth movement), then the value of <variable> will be taken at random for every _xmod5=0..... for collision detection, use the hittest function and "roll the dice" everytime theres a collision....get it?
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

I thought I understood, but I can't make it work.

I only want the buttons to gracefully move maybe 10pixels max in all directions from a centre point. the collision detection is just so they do not overlap each other.

I have never used the hit test function. I have already set all my buttons as movies and have added this script can anyone help with it and show how to add the hitest function.

onClipEvent (load) {
variable = "random()";
variable = "1)(_x+=1)";
}
  • Tchuki
  • Mastermind
  • Mastermind
  • Tchuki
  • Posts: 1774
  • Loc: Edinburgh

Post 3+ Months Ago

Here's a tutorial for random movements :

http://www.kirupa.com/developer/actions ... motion.htm

Then, just add a simple hitTest function to the onClipEvent handler and your sorted.
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

Thanks for the help. Im almost there now, however...

The code Im using now is:
onClipEvent (load) {
width = 20;
height = 20;
speed = Math.round(Math.random()*1)+1;
// initial positions
x = this._x=Math.random();
y = this._y=Math.random();
x_new = Math.random();
y_new = Math.random();
}
onClipEvent (enterFrame) {
// x movement
if (x_new>this._x) {
sign_x = 1;
} else {
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) {
this._x += sign_x*speed;
} else {
x_new = Math.random()*width;
}
// y movement
if (y_new>this._y) {
sign_y = 1;
} else {
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) {
this._y += sign_y*speed;
} else {
y_new = Math.random()*height;
}
}
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

Thanks for the help. Im almost there now, however...

The code Im using now is:
onClipEvent (load) {
width = 20;
height = 20;
speed = Math.round(Math.random()*1)+1;
// initial positions
x = this._x=Math.random();
y = this._y=Math.random();
x_new = Math.random();
y_new = Math.random();
}
onClipEvent (enterFrame) {
// x movement
if (x_new>this._x) {
sign_x = 1;
} else {
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) {
this._x += sign_x*speed;
} else {
x_new = Math.random()*width;
}
// y movement
if (y_new>this._y) {
sign_y = 1;
} else {
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) {
this._y += sign_y*speed;
} else {
y_new = Math.random()*height;
}
}
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

Thanks for the help. Im almost there now, however...

The code Im using now is:
onClipEvent (load) {
width = 20;
height = 20;
speed = Math.round(Math.random()*1)+1;
// initial positions
x = this._x=Math.random();
y = this._y=Math.random();
x_new = Math.random();
y_new = Math.random();
}
onClipEvent (enterFrame) {
// x movement
if (x_new>this._x) {
sign_x = 1;
} else {
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) {
this._x += sign_x*speed;
} else {
x_new = Math.random()*width;
}
// y movement
if (y_new>this._y) {
sign_y = 1;
} else {
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) {
this._y += sign_y*speed;
} else {
y_new = Math.random()*height;
}
} //This is great but I need to resrict the movement of each button to about 10-20 pixels and they must always start from a fixed location and move around that location. Also I have never used hittest before how can I do it so that it will not hit any other movie on the stage. Thanks for your help. NCI
  • roarmeow
  • Professor
  • Professor
  • User avatar
  • Posts: 861
  • Loc: BKNY

Post 3+ Months Ago

wish i could help, but i don't understand what your problem is... looks like the same script as on the kirupa site...

also, do you realize that you posted that 3 times?
and, check it out, they have "code" tags on here, so you can put all your AS in those... [ code ] and [ /code ] (but without the spaces...
looks better and makes it much more clear to whoever's working on your issue...

so what do you need help with?
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

It is the same code without the duplication script.

This is great but I need to resrict the movement of each button to about 10-20 pixels and they must always start from a fixed location and move around that location.

Also I have never used hittest before how can I do it so that it will not hit any other movie on the stage.

Thanks for your help and sorry for posting 3 times previously. NCI
  • roarmeow
  • Professor
  • Professor
  • User avatar
  • Posts: 861
  • Loc: BKNY

Post 3+ Months Ago

aight...
since the original location is determined at:
Code: [ Select ]
x = this._x=Math.random();
y = this._y=Math.random();
  1. x = this._x=Math.random();
  2. y = this._y=Math.random();

and i don't see the variables "x" and "y" being used later on, i think you can just call them up later on in the script...
so make some if/else statements to the effect of:
if the next position is more than 20 px in any direction, reverse direction...

make sense?

hope this helps,
puck
  • Tchuki
  • Mastermind
  • Mastermind
  • Tchuki
  • Posts: 1774
  • Loc: Edinburgh

Post 3+ Months Ago

I dont see what roarmeow is doing there, I would personaly do it differently [ no offence intended to roarmeow ].



Create yourself a new flash file 20px X 20px and follow the Kirupa tutorial, then ...

Replace :
Code: [ Select ]
onClipEvent (load) {
/* The two lines below determine the amount of area in use */
width = 300;
height = 200;

speed = Math.round(Math.random()*2)+1;
//initial positions
x = this._x=Math.random()*width;
y = this._y=Math.random()*height;
x_new = Math.random()*width;
y_new = Math.random()*height;
}
  1. onClipEvent (load) {
  2. /* The two lines below determine the amount of area in use */
  3. width = 300;
  4. height = 200;
  5. speed = Math.round(Math.random()*2)+1;
  6. //initial positions
  7. x = this._x=Math.random()*width;
  8. y = this._y=Math.random()*height;
  9. x_new = Math.random()*width;
  10. y_new = Math.random()*height;
  11. }


With:
Code: [ Select ]
onClipEvent (load) {
/* You want approx 20px either way so set area to 20x20 */
width = 20;
height = 20;

speed = Math.round(Math.random()*2)+1;
//initial positions
x = this._x=Math.random()*width;
y = this._y=Math.random()*height;
x_new = Math.random()*width;
y_new = Math.random()*height;
}
  1. onClipEvent (load) {
  2. /* You want approx 20px either way so set area to 20x20 */
  3. width = 20;
  4. height = 20;
  5. speed = Math.round(Math.random()*2)+1;
  6. //initial positions
  7. x = this._x=Math.random()*width;
  8. y = this._y=Math.random()*height;
  9. x_new = Math.random()*width;
  10. y_new = Math.random()*height;
  11. }
  • nci
  • Newbie
  • Newbie
  • nci
  • Posts: 7

Post 3+ Months Ago

Thanks for all your help.

I have my buttons moving randomly around a 20px axis, but my stage is 700 x 500 and the buttons all drift up to the top left corner and move randomly from the 0,0 axis (half of the stage).

I need them to move just around there initial starting position on the stage size I mentioned above. i.e. x=100 and y=100 with random movement around this point.

Sorry but I am just beginning with flash.

NCI
  • Tchuki
  • Mastermind
  • Mastermind
  • Tchuki
  • Posts: 1774
  • Loc: Edinburgh

Post 3+ Months Ago

Take the replacement code I supplied you and set the width and height properties to 100 and that should do the job.

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 11 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.