 A Flash Developer Resource Site

# Thread: Movieclip between two points

1. ## Movieclip between two points

I'm trying to generate two random points and move the movie clip smoothly like a motion tween between them and i can't figure out what type of equation i would use.
ball._x = 0;
ball._y = 0;
var x2=Math.round(Math.random()*200)
var y2=Math.round(Math.random()*200)
trace(x2);
trace(y2);
this.onEnterFrame = function() {
if(ball._x<x2)
{ball._x+=x2/30}
if(ball._y<x2)
{ball._y+=y2/30}
}
that's what i have right now but it will not go directly to the x2,y2 it will go to diagonally between the two and untill one reaches its max before stopping and then will slide vertically and horizontally, i need an idea on how to make it take a straighter path? thanks for your support.  Reply With Quote

2. code:
```
this.onEnterFrame = function() {
if(ball._x<x2)
{ball._x+=(ball._x-x2)/30}
if(ball._y<x2)
{ball._y+=(ball._y-y2)/30}
}
```

change the onEnterFrame function to that, it should work let me know if it doesn't i will know what i did wrong  Reply With Quote

3. This is a pretty simple fix

Code:
```ball._x = 0;
ball._y = 0;
var x2=Math.round(Math.random()*200)
var y2=Math.round(Math.random()*200)
trace(x2);
trace(y2);
this.onEnterFrame = function() {
if(ball._x<x2 && ball._y<x2){
ball._x+=Math.round(x2/30);
ball._y+=Math.round(y2/30);
}
}```
Here is a sample as well sample

hope this helped a little.

Cheers  Reply With Quote

4. thank you so much for your help i might ask somethin else in a sec... we'll see =D, oh btw do u know if theres a faster way for flash comm servers besides socket-servers?  Reply With Quote

5. Well... haha it seemed right in my head and might not make much sense but here's what i tried.

ball._x = 0;
ball._y = 0;
var rand:Number = Math.floor(Math.random() * 4);
var x1:Number = Math.round(Math.random() * 800) - 125;
var y1:Number = Math.round(Math.random() * 650) - 125;
var x2:Number = Math.round(Math.random() * 800) - 125;
var y2:Number = Math.round(Math.random() * 650) - 125;
var type;
trace(x1 + " x1");
trace(y1 + " y1");
trace(x2 + " x2");
trace(y2 + " y2");
trace(rand);
//ball starting point
if (rand == 0)
{
//comes from top
ball._x = x1;
ball._y = -50;
y2 = 550;
type = 0;
}
else if (rand == 1)
{
//comes from bottom
ball._x = x1;
ball._y = 450;
y2 = -150;
type = 1;
}
else if (rand == 2)
{
//comes from left
ball._x = -50;
ball._y = y1;
x2 = 700;
type = 2;
}
else
{
//comes from right
ball._x = 600;
ball._y = y1;
x2 = -150;
type = 3;
}
this.onEnterFrame = function()
{
//for top or bottom start
if (x1 < x2)
{
if (type == 0)
{
if (ball._x < x2 && ball._y < y2)
{
ball._x += Math.round(x2 / 30);
ball._y += Math.round(y2 / 30);
}
}
else if (type == 1)
{
if (ball._x < x2 && ball._y > y2)
{
ball._x += Math.round(x2 / 30);
ball._y -= Math.round(y2 / 30);
}
}
}
else
{
if (type == 0)
{
if (ball._x > x2 && ball._y < y2)
{
ball._x -= Math.round(x2 / 30);
ball._y += Math.round(y2 / 30);
}
}
else if (type == 1)
{
if (ball._x > x2 && ball._y > y2)
{
ball._x -= Math.round(x2 / 30);
ball._y -= Math.round(y2 / 30);
}
}
}
//for left and right start
if (y1 < y2)
{
if (type == 2)
{
if (ball._x < x2 && ball._y < y2)
{
ball._x += Math.round(x2 / 30);
ball._y += Math.round(y2 / 30);
}
}
else if (type == 3)
{
if (ball._x > x2 && ball._y < y2)
{
ball._x -= Math.round(x2 / 30);
ball._y += Math.round(y2 / 30);
}
}
}
else
{
if (type == 2)
{
if (ball._x < x2 && ball._y > y2)
{
ball._x += Math.round(x2 / 30);
ball._y - Math.round(y2 / 30);
}
}
else if (type == 3)
/*ball._x = 700;
ball._y = y1;
x2 = -150;
type = 3;*/
{
if (ball._x > x2 && ball._y > y2)
{
ball._x -= Math.round(x2 / 30);
ball._y -= Math.round(y2 / 30);
}
}
}
};

i can attach the fla if you want, its nothing more than a ball named ball on the stage... but yeah im trying to get it go from one side of the stage to the other randomly and my logic has failed me once again today... hopefully someone can help me, ill keep tryin to figure it out  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 