Groups | Blog | Home
all groups > flash actionscript > april 2005 >

flash actionscript : Object moving alone in a defined area


Nixy
4/19/2005 9:04:39 PM
I'm new to this king of logic. I want to create an object (Mc) that can move
alone inside a defined area. Like a bug in my screen. This is to create a small
game.

Any idea or tutorial or...
Tanx in advanced
kglad
4/19/2005 9:11:50 PM
Nixy
4/19/2005 9:19:44 PM
Humm
When it comes near of a border it can change direction. I want to learn how to
program some king of movement. I know there is Math.sin, Math.cos and other but
how to use it?

It's a general question. Nothing specific. Just want to learn more in this
king of programmation. It's different of building an application. It's like
Artificial Intelligence.
kglad
4/19/2005 10:09:38 PM
you'll need to initiate a loop. setInterval() is an easy way to do that.
you'll need to define your bug's speed and initial direction of travel. here's
an example of a bug moving within (roughly) a rectangle:

speed = 5;
dir = Math.PI-2*Math.random()*Math.PI;
moveI = setInterval(moveF, 80);
function moveF() {
if (bugMC._x+speed*Math.cos(dir)+bugMC._width>400) {
dir = (2*Math.round(Math.random())-1)*Math.PI/2+Math.random()*Math.PI/2;
}
if (bugMC._x+speed*Math.cos(dir)<200) {
dir = -Math.PI/2+Math.random()*Math.PI;
}
if (bugMC._y+speed*Math.sin(dir)+bugMC._height>400) {
dir = -Math.random()*Math.PI;
}
if (bugMC._y+speed*Math.sin(dir)<200) {
dir = Math.random()*Math.PI;
}
bugMC._x += speed*Math.cos(dir);
bugMC._y += speed*Math.sin(dir);
bugMC._rotation = 180*dir/Math.PI;
}
Nixy
4/20/2005 3:56:03 PM
Thats cool. It's something like that I want. Is there some tutorial or code
that makes an object (MC) following a curved line (guide). The clip follows
this line, when it arrived at the final point, a new line is create as a guide
for the clip. In that way the clip could move smoothly.
kglad
4/20/2005 6:34:46 PM
if you can define your curve (with parameters, for example), you can use
actionscript to move your bug. however, if you cannot define the curve you
must use a motion guide.

i'm not sure what you mean, by "...that way the clip could move smoothly."

in what way does the code i suggested fail to do what you want?
Nixy
4/21/2005 3:32:14 PM
Your code works great. I found something like what i'd like to be able to do.
Check that link :
http://www.macromedia.com/devnet/mx/flash/sample_files/html/amoebas.html

It's something like that but I would like that the small bugs (Mc) follow a
curved line. Actually they go straight to the point and then they change
direction.


AddThis Social Bookmark Button