Search code examples
actionscript-3flashmovieclipgraphic

Getting graphic/movie clip x,y position from within another movieclip?


This should be fairly simple I'd think, I'm just not that familiar with actionscript haha.

I have a game where I have the background moving behind a character that stays in one position on screen. I'm relatively new to actionscript 3 but I'm wanting to have text boxes pop up whenever the player presses a key over certain objects passing in the background.

So, basically the background itself is a movie clip, and I have other graphics and movie clips within the background mc.

I was thinking of getting the player.x and y position and then "comparing" that position (>= and <=, etc.) with the graphic/movie clip in the background. But I just don't know how to obtain the x and y coordinate of the graphics/movie clips in the background mc.


Solution

  • You could try to target your movie clips in the background by getting their coordinates, then removing their parent's position (the background container).

    Something like :

    var finalXPosition:int = targetMovieClip.x - backgroundContainer.x;

    var finalYPosition:int = targetMovieClip.y - backgroundContainer.y;

    By substracting the target movieclip parent's position to its position, you gain the final position in the parent's scope coordinates. It should work for you as soon as your character and your background container are situated at the same level of the display list.

    Here is a quick diagram of what I try to explain (please forgive my inaptitude to draw nice and explicit drawings ^^) enter image description here

    Usually, when I stumble upon such a case, I try to make a quick and even dirty drawing, starting with what I want, then breaking down every useful data I have to achieve that result, you should keep that method in mind and try it the next time ! :-)