0

我正在寻找一个教程来帮助我每次点击鼠标射击一个箭头。

这是我的代码:

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);

我还希望箭头和弓保持面向鼠标。

4

1 回答 1

0

首先,你这样做的方式对我来说似乎是倒退的,但是嘿,如果它有效,那就去吧!

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

此链接将向您展示其他人如何开发简单的射弹射击游戏。如果你想要弓和箭,你将不得不根据重力引起的弧线来处理旋转。(除非你不打算使用重力计算)。看起来你会很好地找到计算。

在您的评论中,我不确定您所说的“它还会在所有其他框架上创建一个项目符号”是什么意思

希望您能在本教程的帮助下完成这项工作。

于 2011-08-25T15:31:34.997 回答