actionscript-3containersflash-cs6flashdevelop

Moving Object to another Objects position


Hey everyone so I am having some trouble trying to get this to work correctly. I have a MC Object called character and another called "points". I have a container object called planetContainer I add the character to the planetContainer the character is rotating around the planets that are also added to the container. The main issue I am having is when the points power up is activated I want the points to move off the other planets and to the charactercenter position. It was working perfect but had to update some code and remove the Points out of the planetContainer and attach them to the planets instead. I know I might have to use localToGlobal but not too sure.

Here is how I setup the character:

    private function newCounterClockWise():void 
    {
        planetContainer.addChild(character);
        character.rotation = (Math.atan2(character.y - planetHit.y, character.x - planetHit.x) * 180 / Math.PI);
    }

How the points are added to the Planets:

private function addPoints():void 
    {
        points = new mcPoints();
        var planetPosition:Point = planetContainer.parent.localToGlobal(new Point(0, 0));  
        points.x = planetPosition.x;
        points.y = planetPosition.y;
        outerPlanets.addChild(points);
        aPointsArray.push(points);
    }

Now this is the main function that handles the points to move to the character but it is not working correctly. The points move but they move off the screen or cause the game to kinda tweak out and do different things. Also the "magnetHandler(); is in my EnterFRame Event:

private function magnetHandler():void 
    {
        for (var i:int = 0; i < aPointsArray.length; i++)
        {
            var currentPoints:mcPoints = aPointsArray[i];
            var characterPosition:Point = planetContainer.parent.globalToLocal(new Point(character.x, character.y));  

            if (currentPoints.hitTestObject(playScreen.mcPointsHit))
            {
                trace("POINTS MID STAGE");
                currentPoints.x -= (currentPoints.x - characterPosition.x);
                currentPoints.y -= (currentPoints.y - characterPosition.y);
                //currentPoints.x = character.x;
                //currentPoints.y = character.y;
                //TweenMax.to(currentPoints, 0.5, {x:characterGlobalPosition.x, y:characterGlobalPosition.y , ease:Power1.easeInOut } );
            }
        }
    }

Can anyone see what I am doing wrong?


Solution

  • It's a hard to understand your question fully (or to understand why you're putting things that relate to each other in separate containers), but likely this line is where it's falling down:

    var characterPosition:Point = planetContainer.parent.globalToLocal(new Point(character.x, character.y)); 
    

    What you want to do, is get the characters x/y coordinates in the currentPoints parent space. To do that, you would do something like this:

    //first, find the global position of character:
    var globalCharacterPoint:Point = character.localToGlobal(new Point());
    
    //then, convert that to the currentPoints parent local space:
    var localCharacterPoint:Point = currentPoints.parent.globalToLocal(globalCharacterPoint);
    

    Also, in this code of yours:

        points = new mcPoints();
        var planetPosition:Point = planetContainer.parent.localToGlobal(new Point(0, 0));  
        points.x = planetPosition.x;
        points.y = planetPosition.y;
    

    You are getting the global space of the planetContainer's parent, which is probably NOT what you want. You likely want:

    planetContainer.localToGlobal(new Point());  //this gives you the global location of the planet container's top left corner
    

    And, since you're adding the points object to outerPlanets, you probably want to convert to its local space (unless it's positioned at 0,0 globally - then it doesn't especially matter).

    var outerPoint:Point = outerPlanets.globalToLocal(planetPosition);
    points.x = outerPoint.x;
    points.y = outerPoint.y;
    

    Needless to say, for games it's best to have everything in the global coordinate space unless it's truly encapsulated assets (like smoke on a rocket etc.)