无法获得真正随机的结果AS3

时间:2013-03-20 00:26:25

标签: actionscript-3 math random

所以,我正试图让我的游戏中的一些小行星随机移动,如果你击中一个,它会移动到其他地方,并且有一个新的,也是随机的飞行路径。我的问题是,当我击中一颗小行星时,它会创建一条新路径,但是如果我击中它的次数超过3次,它就会一直重置到同一路径的同一个地方。谁能帮我这个? (常规层)     import flash.events.Event;     addEventListener(Event.ENTER_FRAME,massGain);     停止();

var speedX = (Math.random() * 20 - 10);
var speedY = (Math.random() * 20 - 10);
var speedX1 = (Math.random() * 20 - 10);
var speedY1 = (Math.random() * 20 - 10);
var speedX2 = (Math.random() * 20 - 10);
var speedY2 = (Math.random() * 20 - 10);
var speedX3 = (Math.random() * 20 - 10);
var speedY3 = (Math.random() * 20 - 10);
var speedX4 = (Math.random() * 20 - 10);
var speedY4 = (Math.random() * 20 - 10);
var speedX5 = (Math.random() * 20 - 10);
var speedY5 = (Math.random() * 20 - 10);
var speedX6 = (Math.random() * 20 - 10);
var speedY6 = (Math.random() * 20 - 10);




function massGain(e:Event)
{



Mouse.hide();

Cosmo.x = mouseX;
Cosmo.y = mouseY;

Asteroid5.x = Asteroid5.x + speedX;
Asteroid5.y = Asteroid5.y + speedY;
if (Asteroid5.x > 1100 || Asteroid5.x < -100)
{
    speedX = -speedX
}
if (Asteroid5.y > 800 || Asteroid5.y < -100)
{
    speedY = -speedY
}

Asteroid6.x = Asteroid6.x + speedX1;
Asteroid6.y = Asteroid6.y + speedY1;
if (Asteroid6.x > 1100 || Asteroid6.x < -100)
{
    speedX1 =  -  speedX1;
}
if (Asteroid6.y > 800 || Asteroid6.y < -100)
{
    speedY1 =  -  speedY1;
}

Asteroid7.x = Asteroid7.x + speedX2;
Asteroid7.y = Asteroid7.y + speedY2;
if (Asteroid7.x > 1100 || Asteroid7.x < -100)
{
    speedX2 =  -  speedX2;
}
if (Asteroid7.y > 800 || Asteroid7.y < -100)
{
    speedY2 =  -  speedY2;
}

Asteroid8.x = Asteroid8.x + speedX3;
Asteroid8.y = Asteroid8.y + speedY3;
if (Asteroid8.x > 1100 || Asteroid8.x < -100)
{
    speedX3 =  -  speedX3;
}
if (Asteroid8.y > 800 || Asteroid8.y < -100)
{
    speedY3 =  -  speedY3;
}

Deathroid1.x = Deathroid1.x + speedX4;
Deathroid1.y = Deathroid1.y + speedY4;
if (Deathroid1.x > 1100 || Deathroid1.x < -100)
{
    speedX4 =  -  speedX4;
}
if (Deathroid1.y > 800 || Deathroid1.y < -100)
{
    speedY4 =  -  speedY4;
}

Deathroid2.x = Deathroid2.x + speedX5;
Deathroid2.y = Deathroid2.y + speedY5;
if (Deathroid2.x > 1100 || Deathroid2.x < -100)
{
    speedX5 =  -  speedX5;
}
if (Deathroid2.y > 800 || Deathroid2.y < -100)
{
    speedY5 =  -  speedY5;
}

Deathroid3.x = Deathroid3.x + speedX6;
Deathroid3.y = Deathroid3.y + speedY6;
if (Deathroid3.x > 1100 || Deathroid3.x < -100)
{
    speedX6 =  -  speedX6;
}
if (Deathroid3.y > 800 || Deathroid3.y < -100)
{
    speedY6 =  -  speedY6;
}

}

(动作图层)     var mySound1:Sound = new menuSelectionClick();     var mySound2:Sound = new explosion();     var nCount1:Number = 0;

timer_Text1.text = nCount1.toString();
addEventListener(Event.ENTER_FRAME,massCollect);

function massCollect(e:Event)
{

if (Cosmo.hitTestObject(Asteroid5))
{

    mySound1.play();
    nCount1++;
    timer_Text1.text = nCount1.toString();

    Asteroid5.y = (Math.random() * 20 - 5);
    Asteroid5.x = (Math.random() * 20 - 15);
    Asteroid5.x = Asteroid5.x + (Math.random() * 20 - 15);//+ speedAgainX1;
    Asteroid5.y = Asteroid5.y + (Math.random() * 20 - 5);//+ speedAgainY1;
}

if (Asteroid5.x > 1100 || Asteroid5.x < -100)
{
    Asteroid5.x = Asteroid5.x * -1;

}

if (Asteroid5.y > 800 || Asteroid5.y < -100)
{
    Asteroid5.y =  -  Asteroid5.y * -1;

}

if (Cosmo.hitTestObject(Asteroid6))
{

    mySound1.play();
    nCount1++;
    timer_Text1.text = nCount1.toString();

    Asteroid6.y = (Math.random() * 20 - 5);
    Asteroid6.x = (Math.random() * 20 - 15);
    //Asteroid6.x = Math.floor(Math.random() * 20 - 15);
    // Asteroid6.y = Math.floor(Math.random() * 20 - 5);
    Asteroid6.x = Asteroid6.x + (Math.random() * 20 - 15);//+ speedAgainX1;
    Asteroid6.y = Asteroid6.y + (Math.random() * 20 - 5);//+ speedAgainY1;
}

if (Asteroid6.x > 1100 || Asteroid6.x < -100)
{
    Asteroid6.x =  -  Asteroid6.x * -1;
}

if (Asteroid6.y > 800 || Asteroid6.y < -100)
{
    Asteroid6.y =  -  Asteroid6.y * -1;

}


if (Cosmo.hitTestObject(Asteroid7))
{

    mySound1.play();
    nCount1++;
    timer_Text1.text = nCount1.toString();

    Asteroid7.y = (Math.random() * 20 - 5);
    Asteroid7.x = (Math.random() * 20 - 15);
    Asteroid7.x = Asteroid7.x + (Math.random() * 20 - 15);//+ speedAgainX1;
    Asteroid7.y = Asteroid7.y + (Math.random() * 20 - 5);//+ speedAgainY1;
}
if (Asteroid7.x > 1100 || Asteroid7.x < -100)
{
    Asteroid7.x =  -  Asteroid7.x * -1;

}

if (Asteroid7.y > 800 || Asteroid7.y < -100)
{
    Asteroid7.y =  -  Asteroid7.y * -1;

}

if (Cosmo.hitTestObject(Asteroid8))
{

    mySound1.play();
    nCount1++;
    timer_Text1.text = nCount1.toString();

    Asteroid8.y = (Math.random() * 20 - 5);
    Asteroid8.x = (Math.random() * 20 - 15);
    Asteroid8.x = Asteroid8.x + (Math.random() * 20 - 15);//+ speedAgainX1;
    Asteroid8.y = Asteroid8.y + (Math.random() * 20 - 5);//+ speedAgainY1;
}

if (Asteroid8.x > 1100 || Asteroid8.x < -100)
{
    Asteroid8.x =  -  Asteroid8.x * -1;

}

if (Asteroid8.y > 800 || Asteroid8.y < -100)
{
    Asteroid8.y =  -  Asteroid8.y * -1;

}

if (Cosmo.hitTestObject(Deathroid1))
{
    mySound2.play();
    removeEventListener(Event.ENTER_FRAME,massCollect);
    removeEventListener(Event.ENTER_FRAME,massGain);
    gotoAndStop(352, "Scene 1");
}
if (Cosmo.hitTestObject(Deathroid2))
{
    mySound2.play();
    removeEventListener(Event.ENTER_FRAME,massCollect);
    removeEventListener(Event.ENTER_FRAME,massGain);
    gotoAndStop(352, "Scene 1");
}

if (Cosmo.hitTestObject(Deathroid3))
{
    mySound2.play();
    removeEventListener(Event.ENTER_FRAME,massCollect);
    removeEventListener(Event.ENTER_FRAME,massGain);
    gotoAndStop(352, "Scene 1");
}

if (nCount1 >= 25)
{
    removeEventListener(Event.ENTER_FRAME,massCollect);
    removeEventListener(Event.ENTER_FRAME,massGain);
    gotoAndStop(351, "Scene 1");

}
}

1 个答案:

答案 0 :(得分:0)

我没有看到您的代码在Asteroid6上为hitTestObject和其他人的速度分配了新值。这可能是你问题的根源。

但是,似乎你需要更多帮助而不仅仅是这个 - 你至少有3个类似Asteroid个对象,以及3组控制它们的相同代码。首先,研究Array,你可以用来对小行星进行分组,然后迭代它,对每个小行星应用同一个代码。第二,关于类的研究,毕竟那些小行星是一个类的对象,即asteroid和“all * asteroid * s”。因此,创建一个类Asteroid,放置一个方法来进行随机分配,确保小行星的速度也更新,并设置另一种方法,通过速度移动小行星并更新速度值,如果坐标超出范围。顺便说一句,您可以绘制一个小行星并将其用作库资源,如果您为其分配与您的类相同的名称,当您需要动态制作小行星时,您不需要重做图形!