View Full Version : random moving insect
hey guys,
Having trouble finding how to do this. Keep finding examples but not how to do it.
All I'm looken for is an easy way to have a movie clip with a graphic that randomly moves about and randomly pauses. Like some of the insect effects you see on websites. Where the bug crawls across the screen stops... turns... and crawls a different way etc.
Dont want to animate it although it wouldnt be too hard but was hopen for somthing that wouldnt cycle.
If it interacted with the mouse that would be cool too. Like moved away from it when it was close.
Thanks.
emergency_pants
02-17-2005, 02:53 PM
Here's one solution. Place a movieClip called "insect" on the stage:
var dice;
var speed;
var rotation;
var x;
var y;
var angle;
var intervalID;
//
function rollDice() {
dice = Math.random()*100;
trace(dice);
if (dice<5) {
moveInsect();
}
}
function moveInsect() {
rotation = Math.random()*360;
speed = 5;
//random length of time to move 1 to 2 seconds
intervalID = setInterval(stopInsect, Math.random()*2000+1000);
}
function stopInsect() {
speed = 0;
clearInterval(intervalID);
}
insect.onEnterFrame = function() {
rollDice();
this._rotation = rotation;
this._x += Math.sin(this._rotation*(Math.PI/180))*speed;
this._y += Math.cos(this._rotation*(Math.PI/180))*speed*-1;
};
is there an easy way to add the effect of the insect "looking" in the direction it moves?
for instance if there is a front and back end instead of just a circle, to get him to face the direction he moves in?
moon walk effect is cool but would be even better if it looked real. Thanks for the help!
emergency_pants
02-25-2005, 01:03 PM
When you make your insect thingy, make sure it is facing upwards. It will then face the correct way at runtime.
sinjun
03-25-2006, 05:59 PM
Hello All
I have a question about this script.
I have been loking for something like this but, there is one attribute that does not do exactly what I would like. Is there any way of showing the MC rotating to its next chosen random point. As it stands now when the script choses the next rotaion point before moving forward in that direction, it "jumps" from its current position to the next point of rotaion. I would love to actually see the mc slowy rotate to its next position, and then move forward to its next positon. Any ideas?
Thanks in advance!
-Sinjun
Dylan Marvin
03-25-2006, 06:34 PM
If this http://dylanmarvin.com/space.html is anything like what you need or helps, PM me and I'll send you the code engine of the insects. (Play through the game to see multiple bugs with random behavior.)
sinjun
03-26-2006, 12:23 AM
Thanks Dylan,
What I am trying to achieve is basically a small crawling insect that sits for a moment moves forward, turns, and moves a bit more etc. The script shown above is almost exactly what I am looking for with the exception of when the insect turns you don't actually see it turn it just goes from pointing in one direction, and suddenly is pointing in another without seeing the actual rotation to the new direction. So I was hoping that there might be just a slight modification to the above code to actualy show the "steps" in between facing one direction, and then facing the other. I guess there would be a need to randomize what direction it turns as well...sometimes clockwise, sometimes counter clockwise. I think the insects in you game move more like flying insects, not one that crawl, stop, and crawl again. Sorta looking to replicate the way say a ladybug crawls around.
Thanks for taking the time to chime in!
-Sinjun
Dylan Marvin
03-27-2006, 12:59 AM
Thanks Dylan,
What I am trying to achieve is basically a small crawling insect that sits for a moment moves forward, turns, and moves a bit more etc. The script shown above is almost exactly what I am looking for with the exception of when the insect turns you don't actually see it turn it just goes from pointing in one direction, and suddenly is pointing in another without seeing the actual rotation to the new direction. So I was hoping that there might be just a slight modification to the above code to actualy show the "steps" in between facing one direction, and then facing the other. I guess there would be a need to randomize what direction it turns as well...sometimes clockwise, sometimes counter clockwise. I think the insects in you game move more like flying insects, not one that crawl, stop, and crawl again. Sorta looking to replicate the way say a ladybug crawls around.
Thanks for taking the time to chime in!
-Sinjun
Sounds like an interesting idea. I can play around with it a bit for you, but not for a couple days.
Dylan Marvin
03-28-2006, 06:49 PM
I didn't have a whole lot of time, so here's the basic code engine I used for my flying bugs (the code's a bit outdated--and it's set up to be on the movieClip, not the stage). Maybe you or someone can combine elements and tweak it so it looks more like a crawling bug. I like the way this works when you put it on 20 bugs. Looks like a swarm.
onClipEvent (load) {
thrust = 1;
decay = .9;
maxSpeed = 30;
turn = 0;
leftright = 0;
autothrust = 0;
die = false;
_rotation = Math.random()*360;
}
onClipEvent (enterFrame) {
reset = Math.random();
if (reset<.01) {
turn = 0;
autothrust = 29;
}
turn += 1;
autothrust += 1;
if (autothrust>45) {
autothrust = 0;
}
if (turn == 20) {
turn = 0;
leftright = Math.random();
}
if ((leftright>0) && (leftright<.2)) {
_rotation += 15;
}
if ((leftright>.2) && (leftright<.4)) {
_rotation -= 15;
}
if (autothrust>20) {
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
} else {
xSpeed *= decay;
ySpeed *= decay;
}
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed));
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
_y -= ySpeed;
_x += xSpeed;
if (_y<0) {
_y = Stage.height;
}
if (_y>Stage.height) {
_y = 0;
}
if (_x<0) {
_x = Stage.width;
}
if (_x>Stage.width) {
_x = 0;
}
}
FlashPat
04-26-2006, 05:22 PM
I pasted the script on to the movieclip but the bug only rotates and doesn't move. What did I do wrong?
Dylan Marvin
04-26-2006, 11:33 PM
I pasted the script on to the movieclip but the bug only rotates and doesn't move. What did I do wrong?
:( Not sure. I just doubled checked the code and it seemed to work for me. See if you have any other variables in your script that might conflict with those in the above code.
soulberry
04-27-2006, 11:19 PM
http://www.levitated.net/daily/levWalkingBug.html
http://www.levitated.net/daily/levWalkingThings.html
http://www.ffiles.com/view_listing.php?sid=15&cat=20
Above are three good examples for study. There is one other one I had come across, I don't remember the link, where the bug turns towards the mouse always.
Hope they help.
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.