让一个对象在actionscript 3.0中随机旋转

时间:2014-04-09 01:51:10

标签: actionscript-3

我做了大部分的事情并让球绕中心点旋转。我需要的是球从一个随机生成的点开始旋转,而不是沿着同一条线一起旋转。这是我的代码。感谢。

import flash.display.Sprite;

var ball1 : Sprite = new ballMC();
var ball2 : Sprite = new ballMC();
var ball3 : Sprite = new ballMC();
var ball4 : Sprite = new ballMC();
var centreball : Sprite = new ballMC();
var centerX:Number = stage.stageWidth / 2;
var centerY:Number = stage.stageHeight / 2;
var radius:Number;
var speed:Number = 0.09 ;
var angle:Number = 0;
var rand1:Number = (Math.round(Math.random()*(1 + 100-50))+50);
var rand2:Number = (Math.round(Math.random()*(1 + 100-50))+50);
var rand3:Number = (Math.round(Math.random()*(1 + 100-50))+50);
var rand4:Number = (Math.round(Math.random()*(1 + 100-50))+50);

var reversebutt:butttonreverse = new butttonreverse();
var orbitoryDirection:String = "forward";
init();


function init():void
{
    addChild(ball1);
    addChild(ball2);
    addChild(ball3);
    addChild(ball4);
    addChild(reversebutt);
    addChild(centreball);
    centreball.x = stage.stageWidth / 2;
    centreball.y = stage.stageHeight / 2;

    radius = (Math.round(Math.random()*(1 + 200-50))+50);


    addEventListener(Event.ENTER_FRAME, circleit);
    reversebutt.addEventListener(MouseEvent.CLICK,differentdir);
}

function circleit(e:Event):void
{
    ball1.x = centerX + Math.cos(angle) * (radius + rand1);
    ball1.y = centerY + Math.sin(angle) * (radius + rand1);
    ball2.x = centerX + Math.cos(angle) * (radius + rand2);
    ball2.y = centerY + Math.sin(angle) * (radius - rand2);
    ball3.x = centerX + Math.cos(angle) * (radius - rand3);
    ball3.y = centerY + Math.sin(angle) * (radius - rand3);
    ball4.x = centerX + Math.cos(angle) * (radius - rand4);
    ball4.y = centerY + Math.sin(angle) * (radius - rand4);

    reversebutt.x = centerX + (centerX / 2) ;
    reversebutt.y = centerY + (centerY / 2 ) ;

    reversebutt.name = "1";
    if (orbitoryDirection == "forward")
    {
        angle +=  speed;

    }
    else
    {
        angle -=  speed;
    }
}


function differentdir(event:MouseEvent):void
{
    if (event.target.name == "1")
    {
        if (orbitoryDirection == "forward")
        {
            orbitoryDirection = "backwards";
        }
        else
        {
            orbitoryDirection = "forward";
        }
    }

}

1 个答案:

答案 0 :(得分:0)

最好使用类而不是时间轴编码......无论如何,你应该创建4个角度变量,因为有4个球,给它们随机的初始值,那就是你完成了。

e.g:

var angle1:Number = Math.random()*360;
var angle2:Number = Math.random()*360;
var angle3:Number = Math.random()*360;
var angle4:Number = Math.random()*360;

function circleit(e:Event):void
{
    ball1.x = centerX + Math.cos(angle1) * (radius + rand1);
    ball1.y = centerY + Math.sin(angle1) * (radius + rand1);
    ball2.x = centerX + Math.cos(angle2) * (radius + rand2);
    ball2.y = centerY + Math.sin(angle2) * (radius - rand2);
    ball3.x = centerX + Math.cos(angle3) * (radius - rand3);
    ball3.y = centerY + Math.sin(angle3) * (radius - rand3);
    ball4.x = centerX + Math.cos(angle4) * (radius - rand4);
    ball4.y = centerY + Math.sin(angle4) * (radius - rand4);

    reversebutt.x = centerX + (centerX / 2) ;
    reversebutt.y = centerY + (centerY / 2 ) ;

    reversebutt.name = "1";
    if (orbitoryDirection == "forward")
    {
        angle +=  speed;

    }
    else
    {
        angle -=  speed;
    }
}