0

所以,我试图让我的游戏中一些小行星随机移动,如果你击中一颗,它会移动到其他地方,并有一条新的、也是随机的飞行路径。我的问题是当我撞上一颗小行星时,它确实创建了一条新路径,但如果我击中它 3 次以上,它只会不断重置到同一路径的同一位置。谁能帮我这个?(常规层)导入 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 = 新的爆炸();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");

}
}
4

1 回答 1

0

我没有看到您的代码为 speed ofAsteroid6和其他 on分配了新值hitTestObject。这很可能是您的问题的根源。

但是,您似乎需要更多的帮助——您至少有 3 个相似Asteroid的对象,以及控制它们的 3 组相同的代码。首先,研究Array,您可以使用它来对小行星进行分组,然后对其进行迭代,对每个小行星应用相同的代码。其次,关于类的研究,毕竟这些小行星都是一类的对象,即asteroid“所有*小行星*s”。所以,上一堂课Asteroid,放一个方法来做你的随机分配,确保小行星的速度也被更新,然后放另一个方法,如果坐标超出范围,将按速度移动小行星并更新速度值。顺便说一句,您可以绘制一个小行星并将其用作库资源,如果您为其指定与您的类相同的名称,当您需要动态制作小行星时,您不需要重做图形!

于 2013-03-20T04:02:43.593 回答