PDA

View Full Version : Replaceable Map MCs?



Jephz
January 26th, 2009, 05:27 PM
Hey all, I'm trying to make a replaceable map system for my rpg so that the code could come out cleaner. So far, I'm having no luck with my current code:



function boundPlayer(mapHolder:MovieClip, playerShell:MovieClip) {
if (_root.playerShell.hitTest(_root.boundLeft)) {
_root.mapHolder._x += mapSpeed;
_root.playesce("Hit Left");
}
}

See, I'm going to have different maps on each frame but they are all going to have the same codes (scrolling, hittest etc.) so I don't want to have to type in the same code in each and every frame. So I figured, if I could just make one function and just replace the code's target, it'll be a whole lot cleaner. I'm not exactly sure how to do this other then have tons of variables. Is there any other way?

-Jephz

pradvan
January 29th, 2009, 01:01 PM
Alright, here it goes:

You are on the right track in terms of "modulizing" everything, instead of writing the same code over and over. But, as you can see, there are still some issues with your current code design.

How do you solve your current problem? First lets figure out what the problem is.

You are putting code on multiple frames, when you change frame, the "modulized" code stops working (it doesn't see the map movieclips in the latter frames because the flash player hasn't "seen" them yet, so it can't use them).

So, how do you solve the problem?
Instead of putting all those maps on different frames, attach them dynamically at runtime. So when the game starts, you do something like:



function createMap(mapName:String)
{
var tempMap = _root.attachMovie(mapName, mapName, _root.getNextHighestDepth());
//Then you can actually call your boundPlayer function and it will work
boundPlayer(tempMap, player);
}

createMap("map01");


Hope that makes sense
-PR

Jephz
February 2nd, 2009, 10:26 AM
Figured out the problem, it was because I added _root to mapHolder when it wasn't there. Thanks anyway, prad.