View Full Version : sticky mc behaviour
subquark
01-29-2003, 09:14 PM
Okay, I looked in the forums and it has to be there but I can not find it. I would like to make sticky jpgs like on mjau-mjau (http://www.mjau-mjau.com) .
I downloaded his source files and even found a pre-made component at flashcomponent.net, BUT . . . I can't figure out how to apply it (boy, am I dense, I can't even figure out the component!!!). The code is too complex for me from mjau-mjau and I just need to understand a simpler code.
All I'm looking for is the sticky behaviour for an one frame mc (not the actual button).
If anyone knows a thread that has this, I'd appreciate it. Thanks in advance! :confused:
Billy T
01-30-2003, 01:38 PM
see if this makes more sense
cheers
You need to look up elastic movement, which I guess will be some sort of decayed oscillation (exp and sin? can't remember) see bit tutorials. Then you need to move the mc to the mouse position (almost) if the distance between MC and mouse is less than 'a small distance'. Now the almost is the trick, take the distance between the original spot which you have stored and the current spot and move the MC a percentage of the distance between the mouse position and the original position ie 1%. Eventually as the mouse is moved away from the MC original position the 1% will be larger than the 'small distance', when this happens you need to release the decayed oscillation. Well that how I would go about it, looking at the behaviour, but its not really newbie stuff?
subquark
01-30-2003, 02:59 PM
thanks for the direction! :) I did not know where to start. I'll clog up the actionscript forum once I get a bit of code going.
Billy T
01-30-2003, 03:06 PM
errr...did you look at the file?
subquark
01-30-2003, 04:04 PM
big doh!!!! :eek:
I'll check it out tonight. See, I told you I was dense!
Thanks and I'll get back to you!
subquark
01-30-2003, 10:55 PM
gee!!! this forum is really hard on the ego! Billy T! How did you do that!?! that file is spot on!
now, I see that you are calling the prototype mc and I would like to condense the code so that I do not duplicate it for each of the 6 mcs it's being applied to, so would I do something like this:
//set some vars in the box
box.speedY = 0;
box.speedX = 0;
box.damping = .7;
box.easing = .2;
box.homeX = box._x;
box.homeY = box._y;
//higher is more sticky
box.stickiness=5;
//bounce prototype
MC_Names = ["about", "portfolio", "extras", "privacy", "credits", "audio"];
for (i=0; i<=MC_Names.length-1; i++) {
item = MC_Names[i];
this[item+"_mc"].mov = item;
this[item+"_mc"].bounceMe = function(targX, targY) {
this.speedY = this.speedY*this.damping+(targY-this._y)*this.easing;
this._y += this.speedY;
this.speedX = this.speedX*this.damping+(targX-this._x)*this.easing;
this._x += this.speedX;
}
};
this[item+"_mc"].onEnterFrame = function() {
//if the mouse is not over the box
if (!this.hitTest(_root._xmouse, _root._ymouse)) {
//then bounce me home
this.bounceMe(this.homeX, this.homeY);
} else {
//otherwise, drag me with some friction
difX = _root._xmouse-this.homeX;
difY = _root._ymouse-this.homeY;
this._x = _root._xmouse-(difX/this.stickiness);
this._y = _root._ymouse-(difY/this.stickiness);
}
};
Obviously, I've got something wrong, 'cause it just makes the mc jump up to position 0,0 :o
Billy T
01-30-2003, 11:05 PM
umm you don't need to duplicate the bounceMe prototype for each mc - it's a movieclip prototype so every mc automatically has access to it
you need to duplicate the vars and the enterframe action for each mc though - you should be able to do that with your for loops
cheers
subquark
01-30-2003, 11:15 PM
great explanation! I've been reading everything I can (bookwise) for two years now, but, in the last two months I have learned so much from this forum and the gracious nature of people like you.
okay, enough of that . . I cry easily, really! i cried at the Muppett Christmas Carol!! (and I eat quiche too). so i need to do this:
box1.onEnterFrame = function() {
//if the mouse is not over the box
if (!this.hitTest(_root._xmouse, _root._ymouse)) {
//then bounce me home
this.bounceMe(this.homeX, this.homeY);
} else {
//otherwise, drag me with some friction
difX = _root._xmouse-this.homeX;
difY = _root._ymouse-this.homeY;
this._x = _root._xmouse-(difX/this.stickiness);
this._y = _root._ymouse-(difY/this.stickiness);
}
};
box2.onEnterFrame = function() {
//if the mouse is not over the box
if (!this.hitTest(_root._xmouse, _root._ymouse)) {
//then bounce me home
this.bounceMe(this.homeX, this.homeY);
} else {
//otherwise, drag me with some friction
difX = _root._xmouse-this.homeX;
difY = _root._ymouse-this.homeY;
this._x = _root._xmouse-(difX/this.stickiness);
this._y = _root._ymouse-(difY/this.stickiness);
}
};
and so forth?
boy am i dense . . . :eek:
hey are you really in Australia, what the hell time is it!?!
Billy T
01-30-2003, 11:26 PM
yep or if they are all called box you could do something like
for(i=1;i<10;i++){
_root["box"+i].damping=.8;
//etc for all the vars and then -
_root["box"+i].onEnterFrame = function() {
//if the mouse is not over the box
if (!this.hitTest(_root._xmouse, _root._ymouse)) {
//then bounce me home
this.bounceMe(this.homeX, this.homeY);
} else {
//otherwise, drag me with some friction
difX = _root._xmouse-this.homeX;
difY = _root._ymouse-this.homeY;
this._x = _root._xmouse-(difX/this.stickiness);
this._y = _root._ymouse-(difY/this.stickiness);
}
}
}
should do the trick
yep I'm really in oz - it's 11.47am (and I should be working!)
;)
cheers
subquark
01-30-2003, 11:52 PM
wow!! thanks, that's exactly what I was looking for, nice compact code! I think (big assumption) I can get it from here. I really appreciate the explanation because it helps me understand this neat stuff.
Work!?! Ick, I'm done for the day (on the day job anyway), so I wish you a speedy afternoon.
Hey, I'm Canadian, is it a bit easier for me to work in Australia than an American? I've always heard that and my wife would love it there. Hell, I even have a didgeridoo (sp?) but can't play it too well . . . Long live the Queen!
super thanks again and I vote that you should receive 100,000 points for your solution!!
Billy T
01-31-2003, 12:02 AM
happy to help ;)
I'm not sure of the difference between canada and america in terms of immigrating to australia, but I do know that if you come from the Middle east (and can prove that you have been persecuted), then our government will happily accomodate you in a 4x4 ft cell for a couple of years.
Sound appealing? ;)
subquark
01-31-2003, 12:29 AM
persecuted! i'm french-canadian, i come from a history of persecution!
i really like the momentum thing you have going on in the scrolling text boxes on your site!
and you use moock's fpi, most excellent
cab remix station is outstanding
Billy T
01-31-2003, 12:32 AM
thanks very muchly ;)
subquark
01-31-2003, 12:58 AM
oh god, I'm back . . . like a bad burrito!
so, I thought I understood BUT I can't get it to work with the array thing. Here's the fla:
Billy T
01-31-2003, 01:05 AM
the enterframe action has to be in the for loop so it gets assigned to all the mcs
for(i=1;i<7;i++){
_root["box"+i].damping=.8;
_root["box"+i].speedY = 0;
_root["box"+i].speedX = 0;
_root["box"+i].damping = .7;
_root["box"+i].easing = .2;
_root["box"+i].homeX = _root["box"+i]._x;
_root["box"+i].homeY = _root["box"+i]._y;
//higher is more sticky
_root["box"+i].stickiness=5;
_root["box"+i].onEnterFrame = function() {
//if the mouse is not over the box
if (!this.hitTest(_root._xmouse, _root._ymouse)) {
//then bounce me home
this.bounceMe(this.homeX, this.homeY);
} else {
//otherwise, drag me with some friction
this.difX = _root._xmouse-this.homeX;
this.difY = _root._ymouse-this.homeY;
this._x = _root._xmouse-(this.difX/this.stickiness);
this._y = _root._ymouse-(this.difY/this.stickiness);
}
}
}
//bounce prototype
MovieClip.prototype.bounceMe = function(targX, targY) {
this.speedY = this.speedY*this.damping+(targY-this._y)*this.easing;
this._y += this.speedY;
this.speedX = this.speedX*this.damping+(targX-this._x)*this.easing;
this._x += this.speedX;
};
also made it so that difX and difY get set in each mc instead of _root
oh and increase the frame rate too
cheers
subquark
01-31-2003, 01:23 AM
simply outstanding! it works and I shall spend the weekend studying why it works
i bought Moock's new definitive guide and will read and study it (I see you recommend it on your tutorial site)
i hope to gain your level of understanding for actionscript someday, and although i am 14 years your senior, i will never be able to catch up to you (you tarzan, me cheetah)
i sincerely wish you the very best in your career and much prosperity in this year
Billy T
01-31-2003, 01:31 AM
LOL ;)
read that book and you'll know everything you need to know (and more)
all the best
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.