Программирование персонажа, чтобы стрелять

Я ищу учебник, который поможет мне стрелять по одной стрелке за щелчок мыши.

это мой код:

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

Я также хочу, чтобы стрела и лук были обращены к мышке.

1 ответ

Во-первых, то, как ты это делаешь, кажется мне задом наперед, но эй, если это работает, иди с этим правильно!

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

Эта ссылка покажет вам, как другой человек разработал простой снаряд шутер. Если вам нужны лук и стрелы, основные изменения, которые вам нужно будет сделать, касаются вращения, основанного на дуге, вызванной гравитацией. (если вы не собираетесь использовать гравитационный расчет). Похоже, что вы все сделаете, найдя расчет.

В вашем комментарии я не уверен, что вы имели в виду под "он также создает маркер на всех остальных кадрах"

Надеюсь, вы получите это для работы с помощью учебника.

Другие вопросы по тегам