Please can anyone teach me about movement in code?

um… maybe im slow at pickin up on stuff… but ive looked through tutorials… and i see that a lot of them deal with choosing a new x and y value and then moving the object to it… for example the random motion tutorial… it chooses a random point on the stage(using Math.random() which im also not totally sure about… but thats later)… so anyway… gets a point… then somehow it moves toward it… i see calculations such as (from the random motion thing)

-thanks to anyone who helps-

speed = Math.random()4+2;
targx = Math.random()
(movie_width-_width);
targy = Math.random()*(movie_height-_height);
dist = _root.getdistance(_x, _y, targx, targy);

norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;

ok… you get a speed (from 0 to 4 im assuming… then add 2 making the range 2 to 6 if im not mistaken)… then you get a target x and a target y… which are anywhere on the stage… not sure why you do the “-_width” or
“-_height”… but anyway… then get distance… i got that… its just a simple slope formula… but ok… the last 3 lines… i dont really get…

norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;

what is norm used to hold?.. i think im correct in assuming the diffx and diffy are the new coordinates to where we move the object… but i dont understand how u got that using “(targx-_x)*norm;” (same for y)… what does this do?..

can anyone clarify this stuff for me? i think it could be really useful for me if i could understand what it does…

just to recap in case i overdid it with what im lookin to figure out…

  1. Math.random() works how?

  2. how you get the targx and targy (why you subtract _width from movie_width (etc. for height)

  3. what norm does… (maybe specify what its use is)

  4. why its used in calculating the diffx and diffy

and finally…

  1. am i correct in assuming that diffx and diffy are used for the new coordinates of the object in question

once again, any help would be greatly appreciated… and thank you in advance to anyone who can take the time to teach me these few things…

-scott

Don’t use this tutorial as a starting point. That is a very advanced tutorial created by a very advanced flasher. I can’t answer all of your questions now…sorry gotta run…

i really don’t understand exactly how that code works, but i’m pretty sure supra would, since i think he wrote it. but for Math.random(), it chooses a random number between 0 and 1, so basically, all decimals. an alternative to that is random(n) where a random number is generated between 0 and n, not including n. so random(9) would get a number from 0…8.

thanks thoriphes… that random(n) seems more my style… im used to c++ and thats a little more familiar… ill use that instead… any help on the other stuff thogh would still be very helpful…

supra… if u could enlighten me on your calculations it would be great… i understand how and where to use it… i just want to figure out how to make stuff move…

um… jubba… if this isnt a good tute to start at… could u lemme know a better one maybe… one that explains code would be awesome… but any u think would be suitable would be great…

thanks to everyone who can spend time to help me…

-scott

A few sites with some helpful hints. :wink:

www.flashkit.com

www.flashzone.com/tutorials

www.enetserve.com/tutorials/

§

Well, if thoriphes does not fully understand that code, then there ain’t no chance in hell you are. Sorry brother, but thats how it works. Thor is one of the scripters and if you’re just starting out, then that is a far too advanced tutorial for you to follow. Personally, i would tell you to do all the tutorials you can. But start out on the basic ones. And once you get the hang of the basics then go up to the intermediate level tutorials. Eventually you will understand everything. It just takes time…

I can’t explain what Supra was doing here… I can give you a couple of basics to think about though.

this is a simple code that makes the Movie clip it’s attached to follow the mouse around.

onClipEvent(enterFrame){
xDiff=_x-_root._xmouse;
yDiff=_y-_root._ymouse;
_x+=xDiff/5;
_y+=yDiff/5;
}

The difference is calculated every time the movie clip’s playhead enters one of it’s frames. The difference for both the x and y locations are calculated separately and stored in variables. Then each turn the x and y are updated by this difference. Since the diff can be + or - the object can move any direction towards the mouse. The division by 5 is used to produce a slow down as the object gets closer to the mouse.

That’s probebly the most basic example I can show you.

Can you give me a link to the tutorial that you took the above code from? I want to see the effect before I comment on exactly what that’s doing.

Hey Ups, thats the Random Motion Tut.

cool… I’ll check it out.

//Random Movement: kirupa.com
//Thanks to Suprabeener for the original code!

function getdistance (x, y, x1, y1) {
&nbsp &nbsp &nbsp &nbsp var run, rise;
&nbsp &nbsp &nbsp &nbsp run = x1-x;
&nbsp &nbsp &nbsp &nbsp rise = y1-y;
&nbsp &nbsp &nbsp &nbsp return (hyp(run, rise));
}
function hyp (a, b) {
&nbsp &nbsp &nbsp &nbsp return (Math.sqrt(aa+bb));
}
MovieClip.prototype.reset = function () {
var dist, norm, movie_height, movie_width;

// movie_height: refers to the height of your movie
// movie_width: refers to the width of your movie

//---------------------------------

movie_height = 150;
movie_width = 300;

//---------------------------------

speed = Math.random()4+2;
targx = Math.random()
(movie_width-_width);
targy = Math.random()*(movie_height-_height);

As I thought… I needed to see some more of this to understand… The “movie_width” and “movie_height” are variables which are set above. This code ensures that the random location which is picked on the screen is only within a square the size of the edges of the stage minus the total size of the movie clip itself. If the movie clip were 20 by 20 then you would get an random x location between 0 and 280, and a random y location between 0 and 130.

dist = _root.getdistance(_x, _y, targx, targy);

now this is calling out to a function called getdistance and it’s passing four arguements to that function. That function as discribed above in turn exicutes a number of other functions. That code is in turn returned to here… wherever here is. That information is used in the following…

norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;
};

MovieClip.prototype.move = function () {
var cycle;

// cycle: specifies the number of milliseconds waited
// between movements

//--------------------------------------------

cycle = 200;

//--------------------------------------------

if (_root.getdistance(_x, _y, targx, targy)>speed) {x += diffx;y += diffy;
} else {x = targx;
y = targy;
if (!this.t) {
t = getTimer();
}if (getTimer()-t>cycle) {reset();
t = 0;
}
}
_x = x;
_y = y;
}

and everything culminates here… the problem is… it’s not easy to explain what those other peices of the code are doing. It’s all linked together into this end function “move” which each of the movie clips uses every frame.

The thing is, it’s not too tough to do random movement, but what Supra is solving with this example is the idea of choosing a random location on the stage and moving the object towards that point until it reaches it, then choosing a new random location to go to. THAT becomes a little more difficult as we see in this example.

My suggestion is, learn the more basic movement things that you can, like my first example above. You’re welcome to use this code from Supra in your own projects until such a time as you are ready to break the code apart. Truthfuly though, I’m not even sure if Supra can explain this without confusing a lot of us, myself included.

thanks a lot everyone… ill look at those links… and thanks for clearin up at least a little of it upu… everything you guys have done is appreciated…

-scott

Haha !! Tricky, isn’t it ? Took me long hours to understand that… Anyway, here it is :

 targx = Math.random()*(movie_width-_width);

// You have to put here - _width. Imagine your scene is 550 pixels wide. If you remove it, 
// the final position of the object could be at 550, which would mean that it goes out of bound
// as the position refers to the center. That' why you remove the width of the object. It can
// go as far as it is still fully visible.

targy = Math.random()*(movie_height-_height);
dist = _root.getdistance(_x, _y, targx, targy);

norm = speed/dist;

// This is just a number Supra chose. He could have put only speed, but then the motion 
// would have been too fast. That's why he devides it by the distance, so that the motion
// has always the same speed (except that this speed is random, that's why you can't see it

diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;

// You're right about that.

Nice piece of code. :smiley:

pom 0]

thanks pom… thats awesome…
-scott