AS3代码的AS2代码有助于转换!

时间:2011-04-21 20:35:48

标签: actionscript-3

帮助!!!这个as2代码给了我一个头脑麻木的头痛。有人可以帮助我将其转换为AS3。这是代码:

import flash.filters.GlowFilter;

//Settings
var maxDist:Number = 400;
var accuracy:Number = 1;

//other vars (do not edit)
var dx:Number;
var dy:Number;
var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;
var realDist:Number;
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;
map_mc.createEmptyMovieClip("laser_mc", map_mc.getNextHighestDepth());

//Glow Filter 
var glow:GlowFilter = new GlowFilter();
glow.color = 0xFF0000;
glow.alpha = 1;
glow.blurX = 7;
glow.blurY = 7;
glow.quality = 2;
map_mc.laser_mc.filters = new Array(glow);

/**
*
* Mouse Controls
*
*/
//create an object that we'll listen to
mouseListener = new Object();

//on Click, fire the weapon
mouseListener.onMouseDown = function() {
    fireWeapon();
}
//on release, stop weapon firing
mouseListener.onMouseUp = function() {
    stopWeapon();
}
//on mouse move, rotate the player
mouseListener.onMouseMove = function() {
    rotatePlayer();
}
//add listener
Mouse.addListener(mouseListener);


/**
*
* Laser Weapon
*
*/
//fire weapon function - creates an onEnterFrame function to repeat updateLaser() over and over
function fireWeapon():Void 
{
    _root.onEnterFrame = function ():Void {
        updateLaser();
    }
}

//stop weapon function - deletes onEnterFrame and clears laser movieclip
function stopWeapon():Void 
{   
    delete _root.onEnterFrame;
    map_mc.laser_mc/**/.clear();
}

//Update Laser Function
function updateLaser() :Void
{       
    //run a loop 
    for (realDist=0; realDist<maxDist; realDist += accuracy) 
    {       
        //get end X&Y
        endX = map_mc.player_mc._x + Math.cos(map_mc.player_mc._rotation * rad) * realDist;
        endY = map_mc.player_mc._y + Math.sin(map_mc.player_mc._rotation * rad) * realDist;

        //calculate hit test
        if (map_mc.walls_mc.hitTest(endX, endY, true)) {
            break;
        }
    }

    //calculate the tip of the barrel (start X & Y)
    startX = map_mc.player_mc._x + 15 * Math.cos(map_mc.player_mc._rotation * rad);
    startY = map_mc.player_mc._y + 15 * Math.sin(map_mc.player_mc._rotation * rad);

    // Clear it before we draw, so the line doesnt stay there
    map_mc.laser_mc.clear();

    //draw laser
    map_mc.laser_mc.lineStyle(2, 0xFF0000, 75);
    map_mc.laser_mc.moveTo(startX, startY);
    map_mc.laser_mc.lineTo(endX, endY);
}

//rorate player towards mouse function
function rotatePlayer():Void
{
    //get distance between mouse and player
    dx = map_mc._xmouse-map_mc.player_mc._x;
    dy = map_mc._ymouse-map_mc.player_mc._y;

    //calculate rotation of player
    map_mc.player_mc._rotation = Math.atan2(dy, dx)*rad2;
}

THX

1 个答案:

答案 0 :(得分:1)

这是其中的大部分 你现在不应该得到编译错误 另外我把它清理干净了 你还必须修改你的旋转点player_mc

import flash.filters.GlowFilter;

//Settings
var maxDist:Number = 400;
var accuracy:Number = 1;

//other vars (do not edit)
var dx:Number;
var dy:Number;
var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;
var realDist:Number;
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;

var laser_mc:MovieClip = new MovieClip()
map_mc.addChild(laser_mc)

//Glow Filter 
var glow:GlowFilter = new GlowFilter();
glow.color = 0xFF0000;
glow.alpha = 1;
glow.blurX = 7;
glow.blurY = 7;
glow.quality = 2;
laser_mc.filters = new Array(glow);

/**
*
* Mouse Controls
*
*/
stage.addEventListener( MouseEvent.MOUSE_DOWN, fire);
//on Click, fire the weapon
function fire( e:Event ):void {
    stage.addEventListener( Event.ENTER_FRAME , updateLaser)
}

stage.addEventListener( MouseEvent.MOUSE_UP, stopFire);
//on release, stop weapon firing
function stopFire( e:Event ):void {
    laser_mc.graphics.clear();
    stage.removeEventListener( Event.ENTER_FRAME , updateLaser)
}

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved)
//on mouse move, rotate the player
function mouseMoved( e:Event ):void {
    //get distance between mouse and player
    dx = map_mc.mouseX-map_mc.player_mc.x;
    dy = map_mc.mouseY-map_mc.player_mc.y;

    //calculate rotation of player
    map_mc.player_mc.rotation = Math.atan2(dy, dx)*rad2;
}

//Update Laser Function
function updateLaser( e:Event ):void {
    //run a loop 
    for (realDist=0; realDist<maxDist; realDist += accuracy){       
        //get end X&Y
        endX = map_mc.player_mc.x + Math.cos(map_mc.player_mc.rotation * rad) * realDist;
        endY = map_mc.player_mc.y + Math.sin(map_mc.player_mc.rotation * rad) * realDist;

        //calculate hit test
        if (map_mc.walls_mc.hitTestPoint(endX,endY)) {
            break;
        }
    }

    //calculate the tip of the barrel (start X & Y)
    startX = map_mc.player_mc.x + 15 * Math.cos(map_mc.player_mc.rotation * rad);
    startY = map_mc.player_mc.y + 15 * Math.sin(map_mc.player_mc.rotation * rad);

    //draw laser
    laser_mc.graphics.clear();
    laser_mc.graphics.lineStyle(2, 0xFF0000);
    laser_mc.graphics.moveTo(startX, startY);
    laser_mc.graphics.lineTo(endX, endY);
}