对象是否在数组中旋转另一个对象?

时间:2016-11-09 22:26:14

标签: arrays actionscript-3 rotation

大家好,所以我有一个对象character,它最初围绕第一个名为planet的影片剪辑旋转,它不是阵列的一部分。因此,用户可以点击屏幕并按照它正在旋转的方向进行character拍摄。目标是让character击中阵列中的其他行星,当它确实有它时,它就像原始一样开始围绕它旋转。一切都很好,除非我拍摄角色并且它落在outerplanets aPlanetArray中的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(); } } 之一时,它并没有开始围绕那个新星球旋转,而是消失了。

以下是我如何设置它:

角色轨道第一个不在阵列中的行星只是将它作为初始轨道然后在离开其他外部平面时将其移除:

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

        }
    }

我在数组中添加了外部行星:

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

在我的输入框处理程序中:

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

与外行星发生碰撞时:

****编辑*****

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
        }

    }

当角色与数组中的outerPlanets发生碰撞时,我的新轨道就会发生在它所着的OuterPlanet周围。或者我至少尝试:

startNewOrbit

我知道我在newClockWisevar currentPlanet:mcOuterPlanets = aPlanetArray[i];函数中做错了。我有一个for循环,我试图将它接触的数组中的对象作为目标。我使用此currentObject但不确定我是否需要使用{{1}}或类似内容。有没有人知道我做错了为什么它不会绕着它触及的新行星轨道而是完全消失?

1 个答案:

答案 0 :(得分:2)

var planetHit:MovieClip;

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

现在看看阵列中该位置的行星是(==)planetHit所以你可以做到

character.x = planetHit.x;

planetHit.addChild(character);