问题描述:

Hey everyone so I have an object character that initially rotates around the first Movie Clip called planet which isn't apart of the array. So the user can tap the screen and the character shoots in direction it is rotating. The objective is to have the character hit the other planets in a array and when it does have it start rotating around it just like the original. Everything works fine except when I shoot the character and it lands on one of the outerplanets in aPlanetArray it doesn't start rotating around that new planet but rather just disappears.

Here is how I set it up:

character Orbits first planet that is not in the array just have it as intial orbit then remove it once leaves to other outerPlanets:

private function startOrbit():void

{

//add Planet

planet.x = (stage.stageWidth / 2);

planet.y = (stage.stageHeight / 2) + 100;

stage.addChild(planet);

//radnom number set up in main class

if (randomOrbit == 1) //If equals to one then clock wise

{

clockWiseOrbiter();

}else

if (randomOrbit == 2) //If equals to one then counter clock wise

{

counterClockWiseOrbiter();

}

}

I add outer Planets in array:

private function addOuterPlanets():void

{

for (var i:int = 0; i < nPlanets; i++)

{

outerPlanets = new mcOuterPlanets();

outerPlanets.x = startPoint.x + (xSpacing * i);

outerPlanets.y = startPoint.y - (ySpacing * i);

stage.addChild(outerPlanets);

aPlanetArray.push(outerPlanets);

}

}

In my Enter Frame Handler:

private function logicHandler(e:Event):void

{

if (!tap && !nextlevel)

{

startOrbit(); //start player orbit around planet

}

if (nextlevel)

{

startNewOrbit();

trace("Starting new Orbit");

}

//When tapped have outher planets move down

if (tap)

{

characterTap(); //Character shoots from position

}

collisionPlanetHandler();

}

When the collision happens with Outer Planets:

**** EDIT*****

private function collisionPlanetHandler():void

{

for (var i:int = 0; i < aPlanetArray.length; i++)

{

var currentPlanet:mcOuterPlanets = aPlanetArray[i];

if (character.hitTestObject(currentPlanet[i]) && !nextlevel)

{

trace("HIT");

//Have charcter orbit that planet

//startNewOrbit();

//tap back to false

tap = false;

nextlevel = true;

character.destroy();

planet.destroy();

planetHit = currentPlanet[i];

}

}

}

When the character collides with the outerPlanets in array I have the New orbit happen around the OuterPlanet it lands on. Or I at least try:

private function startNewOrbit():void

{

newClockWise();

}

private function newClockWise():void

{

for (var i:int = 0; i < aPlanetArray.length; i++)

{

var currentPlanet:mcOuterPlanets = aPlanetArray[i];

stage.addChild(character);

character.rotation = (Math.atan2(character.y - currentPlanet.y, character.x - currentPlanet.x) * 180 / Math.PI);

var rad:Number = angle * (Math.PI / 180); // Converting Degrees To Radians

character.x = currentPlanet.x + radius * Math.cos(rad); // Position The Orbiter Along x-axis

character.y = currentPlanet.y + radius * Math.sin(rad); // Position The Orbiter Along y-axis

angle += speed; // Object will orbit clockwise

}

}

I know I am doing something wrong in the startNewOrbit and newClockWise functions. I have a for loop and I am trying to target the object in array it touches. I use this var currentPlanet:mcOuterPlanets = aPlanetArray[i]; But not sure if I need to use currentObject or something equivalent. Does anyone have any idea what i am doing wrong why its not orbiting the new planet it touches but rather just disappears completely?

网友答案:
var planetHit:MovieClip;

function collisionTest():void{
    for (loop through planets){
        if (planet[i].hitTestObject(character)){
            planetHit = planet[i];
        }
    }
}

See now the planet at that location in the array is (==) planetHit so then you can do

character.x = planetHit.x;

or

planetHit.addChild(character);

etc.

相关阅读:
Top