dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [AS3] Duplication of class

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    2

    [AS3] Duplication of class

    Hi there,

    Im really starting to struggle with this and its getting a bit rediculous because I'm sure it shouldn't be this hard...

    To learn AS3 im currently trying to rewrite a map editor I made in AS3 which goes past 1500 lines.

    The AS2 code is all function and pretty well coded as far as I can see. However I've only got to line 200 and already hit a brick a wall...

    In AS2 I have the following:

    PHP Code:
    function newMenu(menuTitle:StringyScale:NumberbNumber:NumberbText:StringbAction:String) {
    //FUNCTION ELEVEN
    //-- New Menu: Creates and sets up the new menu window using 5 variables Window Name , Window Height , Button Amount , Button Text , Button Command
        
    var thisMenu:MovieClip attachMovie("mcMenuHeader""menu"+menuNumthis.getNextHighestDepth(), {x:12y:40+(menuNum*15)});
        
    thisMenu.num menuNum;
        
    thisMenu.title menuTitle;
        
    thisMenu.boxScale yScale;
        
    thisMenu.buttons bNumber;
        
    thisMenu.buttonText bText;
        
    thisMenu.buttonAction bAction;
        
    thisMenu.btClose.onPress = function() {
            for (var 
    0i<menuNumi++) {
                if (
    _root["menu"+i].y>thisMenu.y) {
                    if (
    thisMenu["box"+num] == null) {
                        
    _root["menu"+i].-= 15;
                    } else {
                        
    _root["menu"+i].-= 15+thisMenu.boxScale-(thisMenu.boxScale/4.57);
                    }
                }
            }
            
    thisMenu.unloadMovie();
        };
        
    thisMenu.btSize.onPress = function() {
            if (
    thisMenu["box"+num] == null) {
                
    thisMenu.attachMovie("mcMenuBox""box"+numthis.getNextHighestDepth(), {x:0y:12yscale:thisMenu.boxScale});
                for (var 
    0i<menuNumi++) {
                    if (
    _root["menu"+i].y>thisMenu.y) {
                        
    _root["menu"+i].+= thisMenu.boxScale-(thisMenu.boxScale/4.57);
                    }
                }
                
    attachButtons(thisMenu);
            } else {
                for (var 
    0i<menuNumi++) {
                    if (
    _root["menu"+i].y>thisMenu.y) {
                        
    _root["menu"+i].-= thisMenu.boxScale-(thisMenu.boxScale/4.57);
                    }
                }
                for (var 
    0i<thisMenu.buttonsi++) {
                    
    thisMenu["button"+i].removeMovieClip();
                }
                
    thisMenu["box"+num].removeMovieClip();
            }
        };
        
    menuNum++;

    Then all I do is make a call like this:

    newMenu("Controls", 100, 3, "Map Movement:Zoom In:Zoom Out", "mControl:zin:zout");


    This creates the new Menu, gives it the title Controls, applies a box scale and attaches 3 buttons with 3 commands that are called via a large case function.



    That all works fine, it might not be the neatest way but I think its a pretty dynamic way because if a new menu is ever required it can be added with 1 line of code and can easily contain previously made commands, the zin, zout etc...


    Now in AS3 the problem is How?
    Iv done a lot of research, and made some simple AS3 programs, infact I have a working multiplayer Snake using a socket server... BUT i just cant work out how to duplicate a class.

    I have created a class which contains the following:

    PHP Code:
    package com{

        
    import flash.events.MouseEvent
        import flash
    .display.MovieClip
        
        
            
    public class menuAdd extends MovieClip{
                
                private var 
    menuOpen:uint =0
                
    private var buttonLib:Array = new Array()
                    
                    
                    public function 
    addNew(menuNum:intmenuTitle:StringyScale:NumberbNumber:NumberbText:StringbAction:String):void {
                        
                        
                        var 
    thisMenuBox:MovieClip = new mcMenuBox();
                        var 
    thisMenu:MovieClip = new mcMenuHeader();
                        
                        
                        
                        
    addChild(thisMenu)
                        
    thisMenu.addChild(thisMenuBox);
                        
                        
    thisMenu.y=25 + (menuNum*16);
                        
    thisMenu.12;
                        
    thisMenuBox.y+=14;
                        
    thisMenuBox.height yScale
                    thisMenu
    .btClose.addEventListener(MouseEvent.MOUSE_UP,function(evt:MouseEvent):void {
                        
    removeChild(thisMenu)
                        }
                    );
            
                    
    thisMenu.btSize.addEventListener(MouseEvent.MOUSE_UP,function(evt:MouseEvent):void {
                        if(
    menuOpen==0){
                            for(var 
    i:inti<buttonLib.lengthi++){
                                
    trace(buttonLib[i])
                            }
                            
    menuOpen=1
                            thisMenu
    .removeChild(thisMenuBox);
                            
                            
                        }else{
                            
    menuOpen=0
                            thisMenu
    .addChild(thisMenuBox);
                            
                        }
                    }
                    );
            
                for (var 
    i:inti<bNumberi++) {
                    var 
    thisButton:MovieClip = new btMenuOpt();
                        
    thisButton.name "thisButton"+i;
                        
    thisMenu.addChild(thisButton)
                        
    buttonLib.push("thisButton"+i)
                        
    thisButton.stop()
                        
    thisButton.26+ (i*20);
                        
    thisButton.12
                
    }
            }
        }

    And in my fla I have:
    PHP Code:
    var newMenu:menuAdd = new menuAdd()

    function 
    loadMenu():void {
            
    addChild(newMenu)
        
    newMenu.addNew(1,"Menu"1404"New Map:Load:Save:Generate""nMap:load:save:gen");
            
    addChild(newMenu)
        
    newMenu.addNew(2,"Controls"1003"Map Movement:Zoom In:Zoom Out""mControl:zin:zout"); 
    THis appears to work but when i do the trace for the button libary check it returns the values of every button on every menu which means its not creating a new isntance but instead creating a new menu within the same object?


    So my question is... whats the easiest way of creating duplicates in flash, i've struggled with this for a few days now so any help is much appreciated...

    thanks

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    It sounds like you're trying to use the class as a function, rather than an instance.

    var newMenu:menuAdd = new menuAdd()

    shouldn't it be something more like

    var newMenu:Menu = new Menu()

    menuAdd sounds like an operation.

    Code:
    var newMenu:menuAdd = new menuAdd()
    
    var controlsMenu:menuAdd = new menuAdd();
    
    addChild(newMenu);
    addChild(controlsMenu);
    
    function loadMenu():void {
    
        newMenu.addNew(1,"Menu", 140, 4, "New Map:Load:Save:Generate", nMap:load:save:gen");
    
        controlsMenu.addNew(2,"Controls", 100, 3, "Map Movement:Zoom In:Zoom Out", "mControl:zin:zout");
    }
    maybe something like that. I'm not really sure exactly what the problem is.
    lather yourself up with soap - soap arcade

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Posts
    2
    Ah great thanks mr_malee,

    I didnt have email notifications turned on so never got the reply!

    did some more research however and now have it all worked out (I think)

    http://www.beakable.com/myuploads/app.html

    For some reason the XML doesnt load however but its starting to come together again... No where near finished however.



    I have another question though. I Have searched for this but cant seem to find a deffinite answer anywhere..

    Basically the maps are not going to be graphical. All solid colours. My AS2 version which is about 90% complete used a Movieclip with 4 frames, each with different coloured squares.

    In AS3 I have made it use the drawRectangle method and I tried using the Bitmap map but could not see any real difference in the two...

    Is there any opinion on what to use for just solid coloured tiles? even if its just a slight speed increase... ?

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    I would use bitmap and fillRect method for best speed:

    myBitmapData.fillRect(rectangle, color);

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