A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: AS3 accesing Library item form an exterior function

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3

    AS3 accesing Library item form an exterior function

    Hi, so here is the idea:

    I want to create a simple game that creates blocks

    package
    {
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;
    import Functions.LD_moveToPoint; // custom function
    import Functions.makeBlocks; // custom function

    public class Main extends MovieClip
    {

    public function Main()
    {
    var GameCTNR: Game_CTNR = new Game_CTNR();
    addChild(GameCTNR);

    //makeBlocks(blok10,Game_CTNR,15,3);
    // blok10 is a library item to copy numerous times, Game_CTNR is a mc that holds all other mc's, 15 is the horizontal row count of blocks and 3 is the vertical count.

    }
    }
    }
    --------------------------------------------------------
    and now the makeBlocks() function:

    package Functions
    {
    import flash.geom.*
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;

    public function makeBlocks(LibraryItem:Object, Target:Object, HorizontalCount:Number = 10, VerticalCount:Number = 3 ):Array
    {
    var h = 0;
    var v = 0;
    var Yoffset = 20
    var StageWidth = 800
    var blocksOffsetH = 1
    var blocksOffsetV = 1
    var BlockArray:Array = new Array();

    for(h=0;h<=HorizontalCount-1;h++)
    {
    for(v=0;v<=VerticalCount-1;v++)
    {
    var BLOK: LibraryItem = new LibraryItem(); //pick an item from library and use it as a source for copying
    BLOK.x=h*(BLOK.width+blocksOffsetH)+(StageWidth/2- ((BLOK.width+blocksOffsetH)*HorizontalCount/2))
    BLOK.y=v*(BLOK.height+blocksOffsetV) + Yoffset
    BlockArray.push(BLOK)
    Target.addChild(BLOK) // add blocks to a object present in the scene
    }
    }
    return BlockArray;
    }

    }

    the problem is that It doesn't work, I can't use the LibraryItem variable as it throws an error.

  2. #2
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Come on please, nobody knows how to do it? Or perhaps do it differently as long as I can write a function to an external .as file an use the existing items in the library to save script space in my Main.as file and create a system with changable variables on demand without having to write everything all over again.

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    var className:String = flash.utils.getQualifiedClassName(LibraryItem);
    var myClass:Class = flash.utils.getDefinitionByName(className) as Class;
    var BLOK = new myClass();

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Perhaps this might help too

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Hi, thx for your replies, this is how i did it myself before I got yuor replies:

    var ObjToCopy:String = LibraryItem as String;
    var ClassReference:Class = getDefinitionByName(ObjToCopy) as Class;
    var instance:MovieClip = new ClassReference();

    However I think that fruitbeard's idea is much berrer.
    I just changed a couple of things:
    1. changed all variables in your Functions.as to public static var
    2.imported Functions.as in Main.as to be able to gain access to those variables especially the BlockArray which is needed in collision detection.

    thx

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Glad it helped, however I only mimicked your functions and tried to change as little as possible in YOUR Functions.as

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