Showing posts with label removeChild. Show all posts
Showing posts with label removeChild. Show all posts

Wednesday, February 5, 2014

Returns and Removing Children

Hello everyone!

I don't really have much to report on Winds of Commerce, I wrapped up the transaction list/receipts feature. When you bring up the receipts menu it will show you a list of all the cities you've been to, what day of the journey you visited it on, and how much you spent or earned there. You can then click on a city in the list and it will bring up an itemized list of what commodities you purchased/sold, how much you spent/earned on each commodity, and how much you spent on purchasing information.

The next thing I have to tackle is the navigation system. Right now I'm using exec functions to simulate going between cities. This will probably be the biggest system I've had to tackle developing this game. Unfortunately, before I can even start coding I have to get the routes figured out. That way I know how many days away each city is from one another and try and make the shortest route to each city. The hard part is trying to make it logical. Each time I get a route down I look at it and think, "Is that the most efficient route to that city?" then proceed to convince myself it isn't... and redo it again!

Returns


While I was working on getting this game put together I ran into how to use returns in a function. I had only used them a handful of times before, and didn't quite know how to use them. I knew you had to have 'return SomeVariable;' but I guess the usefulness didn't really hit me. Since I read more about them and did some tests I have started to use them more. I find them very useful for doing calculations, conversions, or simply checking if a function has completed running.
Setting up a function to return something is really simple, though it is different in Unrealscript and AS3. In Unrealscript the function setup looks like this:
function ReturnType FunctionName() { local ReturnType SomeVar; //Set SomeVar return SomeVar; }

Here is an example of a function that returns an int:

function int GetSum(int A, int B) { local int Sum; Sum = A + B; return Sum; } //To call use it Total = GetSum(5, 10); //GetSum would return 15

The return type can be any of the standard variable types: bool, int, string, array, and even a class. An example of a class being returned can be found in the GameInfo class, with the SetGameType function. This function returns the GameInfo class to use for the game.
static event class<GameInfo> SetGameType(string MapName, string Options, string Portal) { return Default.Class; }

A bool return can be useful if used in an if statement to make sure that function has ran completely before moving on with the if statement. An example of this is in a lot of the bot AI code that is floating around the internet. It is being used to make sure that a path has been found or not, basically:
if(FindPath()) { //Commence moving }

Return functions work the same in AS3, just with the different declaration. Here is how you would declare a retunr funciton in AS3:
function functionName():ReturnType { var someVar:ReturnType; //Set someVar return someVar; }

Speaking of ActionScript, in my last post I mentioned the different ways to call a function in AS3 from Unrealscript(ActionScriptVoid, ActionScriptBool, AsctionScriptInt, and so on). These are there to allow you to receive a return value from your AS3 function. For instance, if you had the GetSum function in AS3 and needed to call it from Unrealscript it would look like this:
//In AS3 function getSum(a:Number, b:Number):Number { var sum:Number; sum = a + b; return sum; } //In Unrealscript Total = ActionScriptInt("_root.getSum");

Now you have several examples of how to use a function to return a value!

Removing Children


The next code bit I found extends what I talked about in my last post with the removeChild function. Sometimes it is necessary to remove all the children from a MovieClip, and since there isn't a function for removeAllChildren there is a work around using a for loop. It's pretty simple, you get the number of children in the MovieClip, then go backwards through the children and remove them one at a time. Here is an example of what I'm talking about:

for (var i:int = container_mc.numChildren-1; i >= 0; i--) { container_mc.removeChildAt(i); }
I think that is all I have for you this week. Hopefully by my next update I'll have all of the routes figured out.

Saturday, January 25, 2014

Chuggin' Along - Winds of Commerce

Hey guys!

Just a quick update on Winds of Commerce. I've been steadily getting components of the code done. I recently finished up opening and filling the information page which will give you a random amount of information about the city you click on. From there you can purchase additional information if you'd like. I also decided to add a transaction list to the game, that way you can look back and see how much you spent, or made, at a given city and on what day that was on. It will also show you the net profit from each city.

While I have completed quite a few things, I still have a lot of work to do with the menus, choosing the starting options, and the end game.

Here is a neat bit of code I found while doing all of this that would have made some of my previous projects a little smoother. This allows you to create a movieclip in Flash, from Unrealscript. This helps because you then have an easier reference to dynamically created objects than you would spawning them through AS3. Here is the code to add the movie:
//In your custom GFxMovieplayer class var GFxObject RootMC, TestMC; RootMC = GetVariableObject("_root"); RootMC.AttachMovie("mySymbol", "myInstanceName"); TestMC = GetVariableObject("_root.myInstanceName");

This part of the code is pretty self explanatory. The function AttachMovie takes 4 parameters, but only two are required.
/** Attaches a symbol to specified movie instance. If no instance is found in this object's scope with the InstanceName, a new instance is created and returned */ native final function GFxObject AttachMovie(string symbolname, string instancename, optional int depth = -1, optional class<GFxObject> type = class'GFxObject');

The first parameter is the name of the linkage name you set in your Flash file.
The second is what you want the instance name to be of the newly created movieclip. You  can also set the depth of the new movie clip, or where it falls on the z-order in the SWF. The last parameter is what class to cast the return as, but I'm not entirely sure.

Now, even though they have a function to add a movieclip, they don't have one pre-built to remove one through Unrealscript. Matt Doyle, on the Epic Games forums, posted a work around(here) that works like a charm. First you'll need to create a new class extending from GFxObject. This will allow us to add the proper functions to remove the clip.
class GFxDisplayObject extends GFxObject; function RemoveChild(GFxObject childObject) { ActionScriptVoid("removeChild"); } defaultproperties { }

This will allow us to call the removeChild function easily on a movieclip cast to this class. When ActionScriptVoid(or similar, like ActionScriptBool, or ActionScriptObject) is called it will automatically pass whatever parameters are passed to the function that is calling it. In this case it will pass childObject onto the removeChild function in AS3. To actually make use of this you just need to add this line into your custom GFxMoviePlayer class:
//In your custom GFxMoviePlayer class GFxDisplayObject(TestMC.GetObject("parent", class'GFxDisplayObject')).RemoveChild(TestMC);

Here, you are grabbing the parent of the movieclip you want to remove, casting it into the custom GFxObject class you made, then calling the RemoveChild function of it, passing the desired movieclip. This line is really versatile since you don't have to set a hardcoded path to the object, since you are getting the parent.

Well enough rambling. hope this was helpful to someone.