Here's my dilemma:
To start off, I've modeled the structure of my game similar to the standard used in Flash Games Studio, with a "gameWorld" parent object on the _root timeline, while all other objects used are children of the "gameWorld" object. Each object also exists on its own layer in its specific timeline.
_root:
..* gameWorld:
....- object "globals"
....- status (instance "statusclip")
....- ship ("ship")
....- shot ("shot")
....- star ("star##")
....- bgClip ("bgClip"):
......+ ship ("testship")
......+ planet
......+ port1
......+ jump ("jump1")
......+ jump ("jump2")
The way I've designed my system, I have an instance "ship" (of object "ship") which stays in the center of my 550x400 screen, rotating left and right with the corresponding arrow keys. When the up arrow key is pressed, the entire object/instance "bgClip" is moved according to the rules of the game physics engine (as well a routine which moves and 'twinkles' the various "star##" instances).
I've created an instance of object "ship" (called "testship") within the "bgClip" movieclip timeline. My intention was to have a player ship object, where the player ship remains stationary, and an object that serves as the "playfield" (in this case, "bgClip") within which all other objects in the game move around in response to user input. Thus, the illusion is that the player ship is moving though staying at the center of the screen at all times while all other objects in the game move off the screen. In this test, the "testship" instance is supposed to always move towards the "ship" instance and stop when within 100 pixels.
Now, my problems arise when I try to work out my AI system for NPC ships. Actually, before I even GET to the AI stuff! The problems really creep in when trying to utilize a shared co-ordinate system between the player ship and the NPC ship. Since the "testship" instance exists in the coordinate system of "bgClip", and the "ship" instance exists in coordinate system of "gameWorld" (which is the parent of "bgClip" too), none of the x/y coordinates match up. What ends up happening is "testship" steadily moves towards the lower-right edge of the screen.
http://www.totalmonkey.com/flash/spa...-temp-fD!.html
(note: should work fine with 200MHz+)
I've tried using localToGlobal in the actions for the "testship" movieclip instance, but that doesn't seem to convert the objects' coordinates to the root layer's coordinate system. In fact, debugging output shows the player "ship"s coordinates to be constantly changing even though there is no scripting for this. So something wacky is going on that I can't figure out. Here's a code sample from the "testship" instance showing my "convert & move" method:
[PRE]...
// create temp object for normalizing
// "ship" coordinates
point1 = new object();
point1.x = _parent._parent.ship._x;
point1.y = _parent._parent.ship._y;
localToGlobal(point1);
// create temp object for normalizing
// "testship" coordinates
point2 = new object();
point2.x = _x;
point2.y = _y;
localToGlobal(point2);
// find distance (via hypotenuse)
// between "ship" and "testship"
dist_x = point2.x - point1.x;
dist_y = point2.y - point1.y;
cSquared = (dist_x * dist_x) + (dist_y * dist_y);
hyp = Math.sqrt(cSquared);
// (thanks Pythagoras)
// set up move based on ratio of
// x and y distance components
move_x = dist_x/hyp;
move_y = dist_y/hyp;
if ( hyp <= 100 ) {
// don't move if less than
// 100 units away from target
} else {
// otherwise, move "testship"
_x -= move_x;
_y -= move_y;
}
...[/PRE]
I'm thinking that part of my problem has to do with the way I've set up the origins (x=0,y=0) of the different objects, and that what I may have to do to fix some of the problem is re-configure/re-align the objects' centers in some way. Due to the way I designed the game engine, the origins for "gameWorld" and "ship" are in the upper-left, while "bgClip" is in the lower-right. Either way, this still shouldn't affect the distance between the two ships, so the essence of the problem really does revolve around the coordinate system issue.
The FLA and SWF are available here:
fla: http://www.totalmonkey.com/flash/spa...6-temp-fD!.fla
swf: http://www.totalmonkey.com/flash/spa...6-temp-fD!.swf
So, can anyone figure this out? Any reasonable help is appreciated.
Thanks,
TotalMonkey!
[Edited by TotalMonkey on 03-17-2002 at 03:13 AM]
