***************
-- frame one -- *************** stop(); define constants _root._gravity=8; //check to see if this is the initial target if(_root._startitone!=1){ init(); }//end if //generate the initial target function init(){ checkmovebegin(); }//end function //fire the projectile _root.onMouseUp = function(){ //add to shot counter _root._counter++; //point bullet bullet._x=cannon._x; bullet._y=cannon._y; //calculate speed of bullet degrees=cannon._rotation-90; radians=degrees/180*Math.PI; _root._dx=Math.cos(radians); _root._dy=Math.sin(radians); _root._dx*=initialSpeed; _root._dy*=initialSpeed; //clear any onscreen messages or lines from previous //shots and initialize pen to trace shot _root.clear(); _root._message=""; _root.lineStyle(1, 0.000000, 65); _root.moveTo(cannon._x, cannon._y); //disable random target generation _root.newrandom=1; gotoAndStop(2); }//end mouseUp event _root.onEnterFrame = function(){ //turn off the code that creates the initial target _root._startitone=1; //move target after successful hit if(_root._newrandom==0){ checkmove(); }//end if //calculates vector and speed aim(); //points the cannon cannon._rotation=angleDeg+90; _root._angle=Math.ceil(angleDeg*-1); }//end enterFrame event function aim(){ //get coords of cannon and mouse x=_root._xmouse; x0=cannon._x; y=_root._ymouse; y0=cannon._y; //calculate distance between cannon and mouse a_dx=x-x0; b_dy=y-y0; distanceHyp=Math.sqrt(a_dx*a_dx+b_dy*b_dy); initialSpeed=distanceHyp; //calculate angle between cannon and mouse angleRad=Math.atan(b_dy/a_dx); angleDeg= angleRad*180/Math.PI; //compensate for negative distance in the x axis if(a_dx<0){ angleDeg-=180; }//end if //round the angle up to the nearest integer for display _root._angle=Math.ceil(angleDeg*-1); //round the power up to the nearest integer for display power=Math.ceil(initialSpeed); }//end function aim() function checkmove(){ //generate new coordinates newx=Math.random()*Stage.width; xtolerance=Stage.width*.2; //check to see if the new coords are too close to the //stage borders if they are move them inward //this code is still buggy if(newx>Stage.width-xtolerance){ newx=Stage.width-xtolerance; } if(newx <= xtolerance){ newx=Stage.width-xtolerance; } //make sure target isn't directly above the cannon above=newx-cannon._x; if(above <= xtolerance){ newx=Stage.width-tolerance; } if (above<0){ above*=-1; if (newx < cannon._x+cannon.width){ newx=Stage.width-tolerance; } } //move the target to the new coords in the x axis targetvisible._x=newx; target._x=newx; //now we do the same for the y coord newy=Math.random()*Stage.height; ytolerance=Stage.height*.25; if(newy<70){ newy=70 } if (newy>Stage.height-ytolerance){ newy=Stage.height-ytolerance; } targetvisible._y=newy target._y=newy; //turn off random coord generation _root._newrandom=1; }//end function checkmove() function checkmovebegin(){ //generate the initial target //generate random coords newx=(Math.random()*Stage.width); newy=(Math.random()*Stage.height); //load the target onto the stage _root.attachMovie("movtarget", "targetvisible", 10); _root.attachMovie("target copy", "target", 20); //place the target at the generated coords targetvisible._x=newx; target._x=newx; targetvisible._y=newy; target._y=newy; //turn off initial target generation _root._newrandom=1; }//end function checkmovebegin() *************** -- frame two -- *************** stop(); //if this is the first shot initialize the counters if(_root._startit != 1){ init(); }//end if function init(){ //initialize the counters _root._counter=1; _root._hits=0; }//end function init() _root.onEnterFrame = function(){ //turn off counter initialization _root._startit=1; //moves the bullet bullet._x+=_root._dx; bullet._y+=_root._dy; //draw line _root.lineTo(bullet._x,bullet._y); //compensate for gravity _root._dy+=_root._gravity; //check to see if bullet hits boundaries hit(bullet); }//end function enterframe() function hit(sprite){ //collision detection if (target.hitTest(sprite)){ //if bullet hits target from above if(_root._dy>0){ _root._hits+=1; _root._newrandom=0; gotoAndStop(1); }else { //if bullet hits target from below _root._message="must hit target from above to count as a hit" gotoAndStop(1); }//end if else }//end if //check collision with edges of the stage if ((sprite._x>Stage.width) || (sprite._x<0) || (sprite._y>Stage.height) || (sprite._y<0)){ //miss--do not generate new coords _root._newrandom=1; gotoAndStop(1); }//end if }//end hit test |