View Full Version : Drag plus Slide effect
jayoe
04-15-2009, 04:58 AM
box1.onPress = function() {
box1.startDrag();
box1.onRelease = function() {
box1.stopDrag();
};
box1.onReleaseOutside = function() {
box1.stopDrag();
};
};
This code let's me drag a MovieClip around.
I was thinking there's got to be a simple line or two of ActionScript that could be added to this that would give me that throw/slide effect.
bluemagica
04-15-2009, 05:45 AM
is today your first try with flash or as2? try to learn and understand the codes, don't just go around looking for snippets!
And what you want to do is simple physics simulation, find distance, direction, and time between start and end coordinates, then calculate the speed vector! but it isn't "a simple line or two of Actionscript"!
jayoe
04-15-2009, 05:59 AM
Wow I don’t appreciate the smart-aleck reply.
"is today your first try with flash or as2?"
I’m a senior member!
If I post to view snippets that’s my business!
Didn’t anyone ever tell you if you don't have anything good to say, don't say anything?
kkbbcute
04-15-2009, 06:48 AM
Agreed, bluemagica, that tone was a little harsh.
Back to the topic, you could use an onEnterFrame which alternates variables every alternate frame to get the different MC positions. Then deduct the X and Y coords you attained with the four vars tio get the velocity, and finally add that velocity to the XY coords of the object. Deduct the speed on each frame accordingly.
bluemagica
04-15-2009, 07:33 AM
Yeh, I am being harsh, because he considers himself to be a senior member, and such a lack of concept is the last thing expected from him!
going by his code, he can just use a counter variable, instead of alternating on enterFrames!
box1.onPress = function()
{
box1.startDrag();
_global.startx = box1._x;
_global.starty = box1._y;
}
box1.onRelease = function()
{
box1.stopDrag();
_global.finalx = box1._x;
_global.finaly = box1._y;
}
box1.onReleaseOutside = function()
{
box1.stopDrag();
}
that way just getting the coordinates is easy! But remember, a timer is also needed cause velocity depends on time, simply calculating the coords will give distance and direction, but not time.
here are some formulas required:-
distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
direction = Math.atan2(y2-y1 , x2-x1)*180/Math.PI;
Speed = distance/time;
kkbbcute
04-15-2009, 01:39 PM
You could return void on the functions and you made a typo in the second onRelease, you spelled it as Realease.
box1.onEnterFrame = function () {
this.speedX=this._x-this.prevX;
this.prevX=this._x;
//same for y
};
If you want the behaviour a little less twitchy, you can store 3 or 4 values and average them.
kkbbcute
04-16-2009, 08:36 AM
rrh: Your code makes no sense, your current speed equals to your currentX minus your previousX where your previousX is also equals to your currentX! Wouldn't you need an alternating Boolean to capture the prev and current X values instead?
bluemagica
04-16-2009, 09:10 AM
box1.onEnterFrame = function()
{
if(this._x!=this.prevX)
{
this.speedX = this._x - this.prevX;
this.prevX=this._x;
}
}
i think that should do it when both are properly initialised! But i still don't see much benefit of doing this!
kkbbcute
04-16-2009, 09:24 AM
I don't see te point of that code either.
rrh: Your code makes no sense, your current speed equals to your currentX minus your previousX where your previousX is also equals to your currentX!
Huge difference between this:
box1.onEnterFrame = function () {
this.speedX=this._x-this.prevX;
this.prevX=this._x;
};
and this
box1.onEnterFrame = function () {
this.prevX=this._x;
this.speedX=this._x-this.prevX;
};
The first works, the second doesn't.
And I guess I didn't make it clear, but this is to calculate the speed while the clip is being dragged, therefore I the value of _x can change between one call of onEnterFrame and the next.
After you've released you switch to basic movement loop.
runawayprisoner
04-16-2009, 11:33 PM
That is more likely the distance between the two points than the speed in my opinions.
To create a throw effect in AS 2.0, I'd use something like this:
var dx = 0;
var dy = 0;
var drag = false;
box1.onPress = function () {
dx = _root._xmouse - box1._x;
dy = _root._ymouse - box1._y;
drag = true;
}
box1.onRelease = function () {
drag = false;
}
box1.onReleaseOutside = function () {
drag = false;
}
this.onEnterFrame = function () {
if(drag){
box1._x += dx;
box1._y += dy;
dx = _root._xmouse - box1._x;
dy = _root._ymouse - box1._y;
}
else if(Math.abs(dx*10)>=10&&Math.abs(dy*10)>=10){ // multiplied by 10 to preserve precision. Use 100 if you need it to be even more accurate(!?)
box1._x += dx;
box1._y += dy;
dx -= dx/5;
dy -= dy/5;
}
}
Mmm... might need to improve on that as it still seems a tiny itsy bit glitchy.
kkbbcute
04-17-2009, 03:11 PM
Huge difference between this:
box1.onEnterFrame = function () {
this.speedX=this._x-this.prevX;
this.prevX=this._x;
};
and this
box1.onEnterFrame = function () {
this.prevX=this._x;
this.speedX=this._x-this.prevX;
};
The first works, the second doesn't.
As in, you claimed it would make the movement less twitchy, it doesn't.
And runawayprisoner, which part is glitchy?
runawayprisoner
04-17-2009, 04:38 PM
As in, you claimed it would make the movement less twitchy, it doesn't.
And runawayprisoner, which part is glitchy?
The "gravity" part is glitchy. You change it ever so slightly and it'll break the throw completely, due to the way it's terminated. I guess it'll work in general if you want to use it as is... but you may need to tweak and experiment with it if you want something else completely.
I'd rather use AS 3.0 to do it, though. But quite a number of devices still use 2.0, eh?
Edit: Wow... I had to ninja-edit in a part. Was doing movie._x instead of box1._x. I was using movie as the target rather than box1 so... but that was so very shocking...
As in, you claimed it would make the movement less twitchy, it doesn't.My code has absolutely no relation to any code anyone else here has posted. It is just calculating the current speed based on the change in _x value.
If you know the speed at the moment you release the object, then you know the starting speed for the slide. That's it. I wasn't doing anything fancy.
When I said "less twitchy" I wasn't comparing my code to anyone else's. I was saying you could make my code less twitchy by adding in some averaging code or something.
kkbbcute
04-18-2009, 04:32 AM
When I said "less twitchy" I wasn't comparing my code to anyone else's. I was saying you could make my code less twitchy by adding in some averaging code or something.
Oh I see, I thought you were saying that your way of calculating the speed made it less twitchy than normal, and I was like, isn't this the normal way :p
Anyway, let's not go off topic.
jayoe
04-18-2009, 06:27 AM
runawayprisoner thanks! It works! I'll study it and learn from it.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.