0
votes

So, I'm trying to make my game where some asteroids move randomly, and if you hit one, it moves somewhere else and has a new, also random flight path. My issue is when I hit an asteroid, it does create a new path, but if i hit it 3+ times, it just keeps resetting to the same place at the same path. Can anyone help me with this? (Regular Layer) import flash.events.Event; addEventListener(Event.ENTER_FRAME, massGain); stop();

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;
}

}

(Actions Layer) 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
oh dear god there are no objectsaustinbv
...I'm assuming that's bad.. I'm a very entry - level coder.user2072135
I think what austinbv is trying to say is that your code is really hard to read. You should edit your question to show only the problematic part of your code. As ActionScript-3 is an object-oriented language, you should learn how to create classes and factorize duplicate code into functions as wellduTr

1 Answers

0
votes

I don't see that your code assigns new values for speed of Asteroid6 and others upon hitTestObject. It's likely that this is the source of your problems.

But, it seems that you need more help than just this - you have at least 3 similar Asteroid objects, and 3 sets of identical code that controls them. First, research about Array, which you can use to group the asteroids, then iterate through it, applying one and the same code to each asteroid. And second, research about classes, after all those asteroids are objects of one class, namely asteroid as in "all *asteroid*s". So, make a class Asteroid, put there a method that will do your random assignment, make sure that velocity of the asteroid is also updated, and put another method that will move the asteroid by velocity and update velocity values if coordinates are out of bounds. By the way, you can draw one single asteroid and use it as a library asset, if you assign it the same name as your class, when you will need to dynamically make asteroids, you don't need to redo the graphics!