A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Loading a different script per level

  1. #1
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191

    Loading a different script per level

    I have a project that I'm currently working on that is dynamically setup via an XML document.

    Each level pulls it's data from individual nodes in that XML document. This way it knows what items to put on the screen, what background image to load, etc.


    What I'm trying to figure out, is how to load/unload individual scripts depending on what level is currently loaded.


    Say the current level == hotel, so I would want to load scripts that are specific to the hotel level. Maybe my hero walks offscreen and into the alley level. I would need to unload the scripts from the hotel and then load the scripts specific to the alley.


    I know that I can include .as files at COMPILE time, but is there a way to pull the code at runtime? Could I store the actual actionScript in the XML file and pull it from there?

    <level id="hotel" description="looks like a stinky hotel from the movies" code="door._x+=5;"> </level>


    Is something like the above code possible or "allowed"?
    i'm obsessed with video games.

  2. #2
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    I think the way you are describing it just complicates it. I think you need to tell us exactly what your custom "script" will be doing and from there we can work how to incorporate it into an xml file.

    Frankly door.x+=5 doesnt say much.

  3. #3
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191
    Say the hero is currently in the "hotel" level.

    I have items in that level that interact with the hero, that are constantly checking each frame to see if an event has been triggered.


    If the hero changes to a new level, say the "school yard", I don't want the constant checks from the "hotel" level running.
    i'm obsessed with video games.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Quote Originally Posted by Flyingcow
    Frankly door.x+=5 doesnt say much.
    That IS the script, Flying. If I'm not mistaken, he want's to run this event every frame. The += instead of = is a pretty dead giveaway, and considering that it's modifying an objects x value, yeah, another big clue. As far as is it plausible to do? I've often given it a bit of though, but I've never come up with a means to do so. Mostly because the algorithm to encompass/handle actionscript would be ridiculously large and bulky. You're better off making the "5" a variable, and then setting that variable to the the parsed information from the xml file. That would be the solution to this particular example, anyhow.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    Quote Originally Posted by ImprisonedPride
    That IS the script, Flying. If I'm not mistaken, he want's to run this event every frame. The += instead of = is a pretty dead giveaway, and considering that it's modifying an objects x value, yeah, another big clue. As far as is it plausible to do? I've often given it a bit of though, but I've never come up with a means to do so. Mostly because the algorithm to encompass/handle actionscript would be ridiculously large and bulky. You're better off making the "5" a variable, and then setting that variable to the the parsed information from the xml file. That would be the solution to this particular example, anyhow.
    Obviously, sir master of the obvious, no need to get so dramatic over something so simple . All I'm trying to do is simplify it rather than trying to parse some useless format.

    He's basically asking for non-reusable code that only works in one level but if you see his second post, he is a little clearer, but still really vague nonetheless. he refers to things as "an" Event and checking "something" each frame. Do you understand my first post now? He is not going to get any help thats of any use to him if he cant be more clear than random and miscellaneous word fillers.

    If literally, all you want is the door to move 5 to the right, just create an xml property with 5 in it and a function that moves doors according to this property.

    Now back to his SECOND post, note the emphasis i put on words like you. It's pretty simple to do in concept but I think you need to post some of your code so i can see the way your script handles the xml. In Pseudo code, you have 2 xml files, "hotel" xml and "schoolyard" xml.

    Basically upon loading a new level, everything needs to be reset/erased and then reloaded in the map creation function(if you even have one, honestly i have no idea what you are trying to do). You can stop the onenterframe events with the null or delete, i believe.

    My answer is as clear as your vague question can be. Level specific codes are pretty complicated and also very useless. You can either try and write different functions for every single level or try and incorporate them into a few reusable functions. There has to be some similarity that you can work with. Ill take a guess, you can make a move function, that moves things based on the data in the xml, and you can also make a description function that displays the contents of the description string.
    Last edited by Flyingcow; 07-11-2007 at 11:39 PM.

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    No drama here, you're clearly mistaken. Oh and just about anyone who knows anything about Flash could take an educated guess that "checking "something" each frame" meant onEnterFrame. The text in the <code> tag of his xml file needs to be converted to actionscript and ran every frame until it is changed.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  7. #7
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    Quote Originally Posted by ImprisonedPride
    No drama here, you're clearly mistaken. Oh and just about anyone who knows anything about Flash could take an educated guess that "checking "something" each frame" meant onEnterFrame. The text in the <code> tag of his xml file needs to be converted to actionscript and ran every frame until it is changed.
    Thats the whole point of my post. Coding that way is worthless and basically impossible. Also note the edits on my post all the way up to 1 minute before you posted before you start some kind of "formal" flame war where I am a "Clearly mistaken person who doesn't know anything about flash".
    Last edited by Flyingcow; 07-11-2007 at 11:46 PM.

  8. #8
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Well, there's a few things you could do. You could have references to function names in your xml and call them dynamically when needed. For instance, you could do something like <object x="10" y="20" onPickup="someAsFunction" />.

    Or, you could use my XML-based scripting language I made a while back. It allows you to write scripts within XML (pretty basic, even though it does support loops, conditions and even functions, and it can be interupted for a few frames and continue afterwards). Let me know if you would want this and I'll dig it up.

  9. #9
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Nevermind, it's not even worth it. You win. I'm ashamed of myself. Guess obvious things offend people.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  10. #10
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    yep, i'm with fallX, you can either go down the scripting language route and define the behaviour of a level in xml or your engine (actionscript) interprets xml data and modify's the game.

    so you could do something like this:
    Code:
    <game>
    
    <level id="1">
    
    <exits>
    
    <exit x="0" y="0" width="10" height="500" dest="2" entry="2">
    
    </exits>
    
    </level>
    
    <level id="2">
    
    <entrys>
    
    <entry id="1" x="500" y="490" />
    
    <entry id="2" x="0" y="490" />
    
    </entrys>
    
    </level>
    its then up to you on how you interpret that data. As with custom objects (enemies, levelData ect.) What i do is define those objects in xml with their init data. x, y, type... then create the instances of the classes when parsing the xml.

    example:
    Code:
    import mx.utils.ClassFinder; //required
    
    class ObjectCreator {
    
    public static function getConstructor(path:String, name:String):Function {
    
    return ClassFinder.findClass(path + name);
    
    }
    }
    
    var data:Object = //xml object data
    
    var clazz:Function = ObjectCreator.getConstructor("classFiles.", data.className);
    var myClass:BaseClass = new clazz(data)
    AS3 uses getDefinitionByName() to retrieve the constructor

    hope that helps
    Last edited by mr_malee; 07-12-2007 at 12:50 AM.
    lather yourself up with soap - soap arcade

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I would just write different functions for each level and then point the main function to whatever name current level has.

    function fhotel(){
    //do hotel stuff
    }
    function fpark(){
    //do stuff in the park
    }
    placename="hotel";
    maingameFunction=this["f"+placename];

  12. #12
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    Not sure this is the best way to do things but...
    How about you have a main swf that keeps all of your main mc's/soundfx/etc in that you will use throughout your game, and then split all the levels amongst other swfs that load into a movie clip?

    That way you could go step-by-step. Keeping each filesize down to whatever you can get into 500k or 1mb or whatever you would consider reasonable. You could deal with 2 or 3 levels at a time depending on their size. And any important variables you need to pass around to the next file could be registered globally. You won't have to muck around with XML.
    By the way, if the game is under 2mb in total I wouldn't even bother loading anything externally. And as a last word of advice, if you do it the way I've suggested here, you may have to bear in mind to be careful about using _root -as it might cause you problems at a later stage.


    ps.
    Obviously, sir master of the obvious, no need to get so dramatic over something so simple
    hehehe..cheeky and I like it..

  13. #13
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Fally and Mr M nailed it imho.

    All you need in your xml are the entry / exit points for each room. When your game loads read in your xml file and shove all those values into an array where you can get to them easily ( If it's any help I'm having to do exactly the same thing on a current project, which also includes various buttons which do different things on a level, all stored in xml files ).

    For any sort of expandable game xml is the way to go. You just need to code a generic engine and then when entering a new level / room get your data for that level from your array ( See above ) and drop it in there.

    Squize.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center