You can convert between local and global coordinates using the localToGlobal and globalToLocal commands. Thus each sprite starts of on top of its parent and with the same rotation and scaling. Note that the default position of each sprite is (0,0), its default rotation is 0 degrees and its default scaling is 1 in both directions. When the parent sprite translates, rotates and scales its children translate, rotate and scale on its surface. We can think of the children of a sprite as drawn on the surface of that sprite. Similarly D:getRotation() returns the relative rotation and D:getScale() returns the x and y scaling relative to the parent. Thus D:getPosition() returns the coordinate of D in the reference frame of its parent, A. So we see rotations are relative also.Īlso sprite position functions operate in local coordinates via the parent sprite. If H is given its own rotation (H:setRotation(20)), then the global rotation of H would be the sum total of the two rotations (in this case 30+20=50 degrees). Since H is drawn on the coordinate system of D, then H will (by default) be rotated like D. Thus we position H down and to the right relative to D. But note the x'' is pointing down and right, not straight across. If we now set the coordinate of H to be (50,0), H is displaced 50 pixels along the x'' coordinate. As seen in the figure, when we rotate D, ( D:setRotation(30)) its coordinate system rotates. The exact same principle applies for rotation and scaling as well. Then C is added as the last (third) child of A. This causes D to move up to first child of A, while E moves to second child of A. Let us now consider what happens if we issue the command:įirstly, since C already has a parent, it is removed from the tree. A sprite is only drawn if it is connected to the display tree, that is, if it is a descendent of stage. Since stage is drawn first, everything overlaps it. H will also overlap D since it is D's child. ![]() Also, children are drawn in order so C is drawn before D which is drawn before E implying E will overlap D (etc). This implies parents are drawn first then their children so children always ovrerlap their parents. When Longford draws the scene, it draws stage first then works through the tree recursively. When we add new children using addChild, they are added to the end of the parent's list. Eg C can be accessed using A since it is the first child of A (or alternatively using A:getChildAt(1)). Note that each sprite has exactly one parent in the tree and sprites can be accessed by reference to their parent and child index number (shown in red above). To achieve this tree structure, we could enter these commands (assuming the sprites have already been prepared): stage:addChild(A) The next figure shows a typical display tree. 2 Positioning, rotating and scaling spritesĪ key structure in Longford is the display tree in which sprites are arranged in order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |