0

i am looking for a tutorial to help me with shooting one arrow per click towards the mouse.

this is my code:

import flash.events.*;
var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int=25;
// size of the arrowshot
var arrowshotSpeed:int=50;
// defines some variables


function mcFunction(event:Event):void {

    xd=bowframe.bow.x-stage.mouseX;
    yd=bowframe.bow.y-stage.mouseY;
    // finds the x and y difference of the sprite and the mouse
    radAngle=Math.atan2(yd,xd);
    // finds the angle in radians 
    bowframe.bow.rotation = int(radAngle*360/(Math.PI*2)-90);

}

stage.addEventListener(Event.ENTER_FRAME, mcFunction);

function shootarrowshot(event:MouseEvent):void {
    var arrowshot:Sprite = new Sprite();
    with (arrowshot.graphics) {
        lineStyle(1, 0x000000, 1);
        moveTo(-size/2,-size);
        lineTo(size/2,-size);
        lineTo(size/2,size);
        lineTo(-size/2,size);
        lineTo(-size/2,-size);
    }
    addChild(arrowshot);
    //attaches
    arrowshot.x=bowframe.bow.x;
    arrowshot.y=bowframe.bow.y;
    arrowshot.rotation=bowframe.bow.rotation;
    // sets the arrowshot x,y, and rotation to the bowframe.bow
    arrowshot.addEventListener(Event.ENTER_FRAME, movearrowshot);
    // adds and enterFrame to the arrowshot, to move it
}
function movearrowshot(event:Event) {
    with (event.target) {
        // with the object that called the event
        x+= arrowshotSpeed*Math.sin(rotation*(Math.PI/180));
        y-= arrowshotSpeed*Math.cos(rotation*(Math.PI/180));
        // moves the arrowshot depending on its rotation, uses build in math sin and cos functions
        if (x>=550||x<=0||y>=400||y<=0) {
            // if the arrowshot is out of the screen
            event.target.removeEventListener(Event.ENTER_FRAME, movearrowshot);
            this.removeChild(DisplayObject(event.target));
            // removes the arrowshot sprite and the enterFrame on it
        }
    }
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, shootarrowshot);

i also want the arrow and the bow to stay facing the mouse.

Thor625
  • 35
  • 2
  • 9
  • i forgot to say it works but it only does a turns a 90 degree angle and it doesn't shoot strait forward it shoot toward the side and it also creates a bullet on all of the other frames i need it to stay on the one frame – Thor625 Aug 23 '11 at 23:55
  • 2
    Just a hint, I'd make a property _ang that has a setter which updates the rotation of the graphic, but is a radian value itself, way easier. – Marty Aug 24 '11 at 00:37

1 Answers1

0

First of all the way that you going about doing this seems backwards to me but hey if it works go with it right!

http://www.freeactionscript.com/2011/07/projectile-weapon/

This link will show you how another person has developed a simple projectile shooter. If you want bow & arrows the major changes you will have to make with deal with rotation based on the arc caused by the gravity. (unless you not going to use a gravity calculation). It looks like you'll do fine finding the calculation though.

In your comment I'm not sure what you meant by "it also creates a bullet on all of the other frames"

Hopefully you get this to work with the help of the tutorial.

Cleanshooter
  • 2,314
  • 4
  • 19
  • 29
  • it creates a bullet on all of the other frames: i have 5 frames axe,sword,spear,bow,nothing. i can switch between frames to change weapons. but whenever i click on a frame that is not a bow it creates a bulet arround the cahrachter. – Thor625 Aug 29 '11 at 21:46
  • can you upload the .FLA and provide a download link so I can see what it does because your code alone is not painting the complete picture. – Cleanshooter Oct 05 '11 at 18:40