I’m trying to put together some code that will approximate the arced flight of an arrow.
Currently, I’ve got the code so that the angle of the bow movieclip will move depending upon the position of the mouse. Pressing the mousebutton will then fire an arrow at this angle (albeit at a constant speed). I’ve played around with xmov and ymov in an attempt to introduce inertia and gravity (respectively) and therefore create a more natural flight of the arrow but aren’t really getting what I’m after. Added to this, I also need to alter the angle of the arrow clip over the course of it’s flight to imitate the charge in orientation (i.e. starts to point towards the ground in the downward 1/2 of the arc). Code so far is below.
Does anyone have any suggestions? Or even alternatives (such as draw a curve with the curveTo method and then animate the arrow clip along this path? … LOL, or is this simply wild/hopeful speculation on my part)
speed = 25;
// calculate angle based upon mouse position
_root.onMouseMove = function() {
updateAfterEvent();
xdiff = _root._xmouse-bow_mc._x;
ydiff = _root._ymouse-bow_mc._y;
angle = Math.atan2(ydiff, xdiff);
angle = angle180/Math.PI;
bow_mc._rotation = angle;
};
// fire arrow at this angle when MB clicked
_root.onMouseDown = function() {
angle = bow_mc._rotation;
angle = angleMath.PI/180;
++depth;
clipName = “arrow”+depth;
clip = _root.attachMovie(“arrow”, clipName, depth);
clip._rotation = bow_mc._rotation; // gives the arrow the same orientation as the bow when it is fired)
clip._x = bow_mc._xMath.cos(angle);
clip._y = bow_mc._yMath.sin(angle);
clip.xmov = speedMath.cos(angle);
clip.ymov = speedMath.sin(angle);
clip.onEnterFrame = function() {
this._x += this.xmov;
this._y += this.ymov;
};
};
Many thanks!! Wishing I’d gone to a few more physics lessons at school now ;¬)
t