views:

107

answers:

2

Ive probably ran into some code like this before. just cant remember where. I have 2 display objects on my stage. One is rotating and the other is positioned 90 degrees from the rotating object. As the object rotates, the other object adjusts its x and y positioning from the rotating object..

If this doesnt make sense, then let me explain what I am trying to do. I have a spaceship in my flash game. this ship has 2 torrets siting on the left and right wing. as the ship turns, the torrets need to remain on the wings. Does anyone know how I would go about doing this.

+3  A: 

why don't you just nest them inside the wings? if the wing rotates they will remain on top of the wing.

antpaw
the wing is part of the ship image. so i would have to go based of whole ship image
numerical25
Why not have them as children of the main ship and have their transformation matrix relative to the parent? Then as the parent rotates, you apply that down to the children
zebrabox
can you provide us a drawing of before and after rotation and how you scene should look like?
antpaw
If the whole ship is rotating, then everything that is part of the ship should be within the ship display object and rotate with it. It seems like overkill to not nest everything into a single ship object and have to add all kinds of listeners and frame events to match everything up. Nesting items is a core capability of Flash and you can still assign classes to each part of the ship in case you're trying to do something like collision detection for specific items.
wmid
I see what your saying. I didnt think of that. stupid me. thanks for that answer, it was a good one.
numerical25
only problem is that when I put them inside the image, flash doesnt seem to listen to turret class.
numerical25
+1  A: 

If you insist on keeping the DisplayObjects non-nested, use the power of trigonometry.

Assuming that at rotation = 0, your turret object is a distance, r, directly to the right of the ship center.

private function degreesToRadians(degrees:Number):Number {
    return degrees * Math.PI / 180;
}

turret.x = shipCenter.x + r*Math.cos(degreesToRadians(ship.rotation));
turret.y = shipCenter.y + r*Math.sin(degreesToRadians(ship.rotation));

However, learning how to use a proper transformation matrix or nesting your display objects will ultimately lead to a cleaner solution

ZackBeNimble
Hey thanks. What do you mean by non nested. I call these turrets from inside of the ship class. Is that what you mean by nested ? or do you mean to place these turrets on top of my ship from within the movieclip (i.e. drag and drop them in place from the library) I only tried the first solution.
numerical25
I mean nested in the sense of the display list -- placing the graphical representations of the various ship parts into a shared DisplayObjectContainer subclass. I'm not certain how to do that properly in CS4, but the underlying fundamentals of the display list are explained here: http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_01.html
ZackBeNimble