|
-
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]
-
Gross Pecululatarian
You talk about the LocalToGlobal method making it jump about, whats happening is that the ship moves to the right coords, and then it has moved, so what it returns the next time will be different. It moves there, and then the output once again will be different as the clips arn't in the same alignment, geddit?
I would put the other ships in either the bit of BG that scrolls, or in the same place as the player and move them according to the players movment. You could make them move faster for a depth feeling. Does that work? Did you already put them in the BG MC?
-
incredibulus-actionscriptum
ummm i cant help you much on this but if i were you i would eleiminate that square with stroke fills and use regular
hairline
cia
-
Gross Pecululatarian
Why?
This may help with the localToGlobal, try finding the differences between where it should be and where it is.
Also, try putting the enemies in the same place as the MC, that's going to make life a lot easier.
-
I didn't read all your code but the problem might be that you're not using localToGlobal() properly. If I remember correctly, the key thing in using it is which clip performs localToGlobal(). For example, if you have a
pt = new Object();
pt.x = clip1.clip2.clip3._x;
pt.y = clip1.clip2.clip3._y;
now say you want the coords of clip3 in the coordinate system of the main timeline. If you just do :
_root.localToGlobal(pt)
you'll get garbage. you need to do :
_root.clip1.cip2.localToGlobal(pt)
This is because the clip's _x and _y are always relative to the parent clip, so it's the parent clip that needs to perform localToGlobal().
-
Originally posted by Ed_Mack
You talk about the LocalToGlobal method making it jump about, whats happening is that the ship moves to the right coords, and then it has moved, so what it returns the next time will be different. It moves there, and then the output once again will be different as the clips arn't in the same alignment, geddit?
I'm not sure I get it, probably because my movieclip names are so similar that things get confusing.
I would put the other ships in either the bit of BG that scrolls, or in the same place as the player and move them according to the players movment. You could make them move faster for a depth feeling. Does that work? Did you already put them in the BG MC?
Well, the NPC ship is within the BG clip, so that if it were not moving, it would still move with the rest of the background. It does this correctly; it's when I try to move the NPC ship that the problems come about. So really, so looking at it with a clearer head, it's looking to me like a problem with the way I'm doing the localToGlobal() function. Unfortunately, the documentation on localToGlobal()is pretty poor and limited.
-
Originally posted by magnetos
ummm i cant help you much on this but if i were you i would eleiminate that square with stroke fills and use regular hairline
Thanks! I'm assuming then that the various alternative stroke types are slower than hairline strokes?
-
Originally posted by Ge0rge
I didn't read all your code but the problem might be that you're not using localToGlobal() properly...
I still don't get it though. Maybe I'm being dense, but something about the localToGlobal() is not registering in my head.
So let me see if I have this right? Since I instantiated the point1 object in the "testship" clip's actions, then no matter what clip I point the 'point1.x = ...' property to, it's going to convert the coordinate system of the point1 object to the coordinate system of the "testship" clip's parent?
Is there somewhere I can find good documentation or a good tutorial on localToGlobal() and globalToLocal()? I'd hate to think that getting this concept will keep me from starting on developing the AI! 
Thanks, all, for the help, by the way.
-
Syntax
anyMovieClip.localToGlobal(point);
Arguments
point: a pair of numbers;
Description
Method; converts the point object from the movie clip's (local) coordinates, to Stage (global) coordinates.
It doesn't matter where you create the point object because it's just a pair of numbers. See that "anyMovieClip" in the syntax? That's what matters. The function converts the point from "anyMovieClip"'s coordinates to stage coordinates. It always converts to stage coordinates, not to parent coordinates. Think about it, I can't make it any more clearer.
[Edited by Ed_Mack on 03-18-2002 at 05:44 AM]
-
Yes we can
if you still have problems,send me the fla and i´ll fix it and at comments on it what i did
-
ism
a couple of things helped me understand localToGlobal() and globalToLocal() a little better;
//
1. you pass loocalToGlobal() a co-ordinate (point.x,point.y). the function has no idea where you
got the point from except by it's qualification (MC.localToGlobal() MC=qualification)
//
2. when you examine an MC's _x, _y co-ordinate it is not relative to the MC but to it's parent MC or the main stage if it is not contained within an MC.
//
3. the returned value from localToGlobal() will always
be relative to the main stage so the co-ordinates will reflect your movies dimensions. so if your MC is visible then localToGlobal() will return a point.x value between 0 and 500 and a point.y value between 0 and 400 if you movie's dimensions are 500x400.
//
4. the best way to ensure you are using localToGlobal() properly is to append a _parent to the qualification for the localToGlobal() function ie.
Code:
point.x = MC._x;
point.y = MC._y;
MC._parent.localToGlobal(point);
should always work unless MC already contains the _parent keyword.
hope this helps
BlinkOk
-
Thanks guys! That's exactly the kind of explanation I was looking to get! Here's where I am now (this is a stripped-down version of the game-thus-far using the basic game engine): http://www.totalmonkey.com/flash/localToGlobal.html
(ps pressing the spacebar toggles some stats for the coord system, but they're really not important at this point)
Next, I'm working on getting the NPC rotation working, then on getting the NPC to use the physics engine. Afterwards, I have a variety of "behaviors" I want to apply to NPCs, such as: wander, orbit, go to jump point, guard, patrol, etc. These behaviors are what NPCs do when the hero ship is out of the NPC's "sensor range".
I'm certain to have some problems at some point, so I'll post again very soon. 
Thanks again for the help,
TotalMonkey!
(ps I'm scared to know why Ed_Mack felt he had to edit GeOrge's post )
[Edited by TotalMonkey on 03-18-2002 at 11:24 PM]
-
heh, don't be scared. I had mistyped a vb tag.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|