AS3激光武器

时间:2011-04-22 03:30:39

标签: actionscript-3

我是动作脚本3的新手(我在as2中做了一点点),我正在尝试创建一个向鼠标旋转的激光枪,并在鼠标触发时进行拍摄。

有点像这样但是在as3

http://www.freeactionscript.com/2009/04/laser-hitting-solid-objects-walls/

谢谢, 托尔

3 个答案:

答案 0 :(得分:2)

你可以开始玩这样的东西:

//adding listener to run all the time
m.addEventListener("enterFrame", runGame);
//listener for mouse is down
stage.addEventListener("mouseDown", md);
//listener for mouse is up
stage.addEventListener("mouseUp", mu);

//to know if mouse is down
var _fire:Boolean = false;

//function for all the time
function runGame(evt:*):void
{
    //to know the angle where the mouse is from the "canon" in radians
    var angle:Number = Math.atan2(stage.mouseY - m.y, stage.mouseX - m.x);

    //set the canon's rotation
    m.rotation = 180 * angle / Math.PI;

    //if mouse us down -> fire
    if(_fire)
    {
        //create a point far away, so it will always work if the screen is normal
        var point:Point = Point.polar(10000, angle);

        //shoot the laser
        graphics.lineTo(point.x, point.y);
    }
}

//when mouse is down
function md(evt:*):void
{
    //prepare graphics
    graphics.clear();
    graphics.lineStyle(2, 0xff0000);
    graphics.moveTo(m.x, m.y);

    //set fire to true
    _fire = true;
}

//when mouse is up
function mu(evt:*):void
{
    //set fire to false
    _fire = false;

    //clear the laser
    graphics.clear();
}

创建一个新项目,在舞台上放置一个movieclip,将其命名为“m”(不带引号;))。将脚本粘贴到第1帧上的动作脚本。

如果您希望激光器在关闭时跟随鼠标,请更改

runGame函数:

function runGame(evt:*):void
{
    //to know the angle where the mouse is from the "canon" in radians
    var angle:Number = Math.atan2(stage.mouseY - m.y, stage.mouseX - m.x);

    //set the canon's rotation
    m.rotation = 180 * angle / Math.PI;

    //if mouse us down -> fire
    if(_fire)
    {
        //create a point far away, so it will always work if the screen is normal
        var point:Point = Point.polar(10000, angle);

        //shoot the laser
        graphics.clear();
        graphics.lineStyle(2, 0xff0000);
        graphics.moveTo(m.x, m.y);
        graphics.lineTo(point.x, point.y);
    }
}

和md的功能:

//when mouse is down
function md(evt:*):void
{
    //set fire to true
    _fire = true;
}

答案 1 :(得分:0)

"Epic" laser,来源。也很棒的网站顺便说一句。

答案 2 :(得分:0)

在尝试解决这类问题之前,可能最好先学习编程的基本概念。切割/粘贴代码不是编程,可以有frankenstein-ish结果。

编程没有神奇的捷径,你需要学习基本概念并以此为基础。 Wonderfl.net是一个很酷的地方,但我建议开始更基本的概念。在你做到这一点之前,这一切都将成为你的伏都教。