PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Optimization needed
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Optimization needed

  1. #1
    Member
    Join Date
    May 2011
    Posts
    55

    Optimization needed

    I got several things in my code that is repetetive, but they do have some stuff in them that is different compared to the other equal looking parts, so I don't know how to shorten the code.

    Anyone care to take a look at this code and try to optimize it, please?

    Especially the parts in bold.

    Code:
    stage.quality="low";
    var ranNumberX:Number;
    var ranNumberY:Number;
    var currentObject=ff;
    var collide:Boolean=false;
    var objectWidthHeight:Number=61;
    var playedPutDownSound:Boolean=false;
    //-------------------------------------------------------
    var pinkBack:MovieClip = new PinkBack();
    pinkBack.cacheAsBitmap=true;
    addChild(pinkBack);
    var pinkGrid:MovieClip = new PinkGrid();
    pinkGrid.cacheAsBitmap=true;
    addChild(pinkGrid);
    var boxesMC:MovieClip = new BoxesMC();
    boxesMC.cacheAsBitmap=true;
    addChild(boxesMC);
    var lastBox=boxesMC.box0;
    //-------------------------------------------------------
    var object10:MovieClip = new object();
    object10.cacheAsBitmap=true;
    addChild(object10);
    object10.gotoAndStop("BottomLeft");
    ranPlacement();
    object10.x=ranNumberX;
    object10.y=ranNumberY;
    
    var object9:MovieClip = new object();
    object9.cacheAsBitmap=true;
    addChild(object9);
    object9.gotoAndStop("BottomLeft");
    ranPlacement();
    object9.x=ranNumberX;
    object9.y=ranNumberY;
    
    var object8:MovieClip = new object();
    object8.cacheAsBitmap=true;
    addChild(object8);
    object8.gotoAndStop("BottomLeft");
    ranPlacement();
    object8.x=ranNumberX;
    object8.y=ranNumberY;
    
    var object7:MovieClip = new object();
    object7.cacheAsBitmap=true;
    addChild(object7);
    object7.gotoAndStop("BottomLeft");
    ranPlacement();
    object7.x=ranNumberX;
    object7.y=ranNumberY;
    
    var object6:MovieClip = new object();
    object6.cacheAsBitmap=true;
    addChild(object6);
    object6.gotoAndStop("BottomLeft");
    ranPlacement();
    object6.x=ranNumberX;
    object6.y=ranNumberY;
    
    var object5:MovieClip = new object();
    object5.cacheAsBitmap=true;
    addChild(object5);
    object5.gotoAndStop("BottomLeft");
    ranPlacement();
    object5.x=ranNumberX;
    object5.y=ranNumberY;
    
    var object4:MovieClip = new object();
    object4.cacheAsBitmap=true;
    addChild(object4);
    object4.gotoAndStop("BottomLeft");
    ranPlacement();
    object4.x=ranNumberX;
    object4.y=ranNumberY;
    
    var object3:MovieClip = new object();
    object3.cacheAsBitmap=true;
    addChild(object3);
    object3.gotoAndStop("BottomRight");
    ranPlacement();
    object3.x=ranNumberX;
    object3.y=ranNumberY;
    
    var object2:MovieClip = new object();
    object2.cacheAsBitmap=true;
    addChild(object2);
    object2.gotoAndStop("TopRight");
    ranPlacement();
    object2.x=ranNumberX;
    object2.y=ranNumberY;
    
    var object1:MovieClip = new object();
    object1.cacheAsBitmap=true;
    addChild(object1);
    object1.gotoAndStop("TopLeft");
    ranPlacement();
    object1.x=ranNumberX;
    object1.y=ranNumberY;
    //-------------------------------------------------------
    var boxes:Array=[boxesMC.box0,boxesMC.box1,boxesMC.box2,boxesMC.box3,boxesMC.box4,boxesMC.box5,boxesMC.box6,boxesMC.box7,boxesMC.box8,boxesMC.box9,boxesMC.box10,boxesMC.box11,boxesMC.box12,boxesMC.box13,boxesMC.box14,boxesMC.box15,boxesMC.box16,boxesMC.box17,boxesMC.box18,boxesMC.box19,boxesMC.box20,boxesMC.box21,boxesMC.box22,boxesMC.box23,boxesMC.box24,boxesMC.box25,boxesMC.box26,boxesMC.box27,boxesMC.box28,boxesMC.box29,boxesMC.box30,boxesMC.box31,boxesMC.box32,boxesMC.box33,boxesMC.box34,boxesMC.box35];
    for (var i:int = 0; i < boxes.length; i++) {
    	boxes[i].visible=false;
    }
    var objects:Array=[object1,object2,object3,object4,object5,object6,object7,object8,object9,object10];
    //-------------------------------------------------------
    var outline:GlowFilter=new GlowFilter(0x000000,1.0,2.0,2.0,10);
    outline.quality=BitmapFilterQuality.MEDIUM;
    var myFont:Font = new Font1();
    var myFormat:TextFormat = new TextFormat();
    myFormat.font=myFont.fontName;
    myFormat.size=14;
    var FPS:TextField = new TextField();
    var SCORE:TextField = new TextField();
    var LEVEL:TextField = new TextField();
    FPS.autoSize=TextFieldAutoSize.LEFT;
    FPS.defaultTextFormat=myFormat;
    FPS.embedFonts=true;
    FPS.textColor=0xFFFFFF;
    FPS.antiAliasType=AntiAliasType.NORMAL;
    FPS.filters=[outline];
    SCORE.autoSize=TextFieldAutoSize.LEFT;
    SCORE.defaultTextFormat=myFormat;
    SCORE.embedFonts=true;
    SCORE.textColor=0xFFFFFF;
    SCORE.antiAliasType=AntiAliasType.NORMAL;
    SCORE.filters=[outline];
    LEVEL.autoSize=TextFieldAutoSize.LEFT;
    LEVEL.defaultTextFormat=myFormat;
    LEVEL.embedFonts=true;
    LEVEL.textColor=0xFFFFFF;
    LEVEL.antiAliasType=AntiAliasType.NORMAL;
    LEVEL.filters=[outline];
    FPS.cacheAsBitmap=true;
    SCORE.cacheAsBitmap=true;
    LEVEL.cacheAsBitmap=true;
    addChild(FPS);
    addChild(SCORE);
    addChild(LEVEL);
    FPS.x=3;
    FPS.y=1;
    SCORE.x=125;
    SCORE.y=1;
    LEVEL.x=270;
    LEVEL.y=1;
    FPS.text="FPS: NAN";
    SCORE.text="SCORE: 00000";
    LEVEL.text="LEVEL: 01";
    //-------------------------------------------------------
    function pickUpFunction():void {
    	var pickUpSound:Sound = new PickUp();
    	var sc:SoundChannel=pickUpSound.play();
    	var st:SoundTransform=sc.soundTransform;
    	st.volume=.5;
    	sc.soundTransform=st;
    }
    function putDownFunction():void {
    	var putDownSound:Sound = new PutDown();
    	var sc:SoundChannel=putDownSound.play();
    	var st:SoundTransform=sc.soundTransform;
    	st.volume=.2;
    	sc.soundTransform=st;
    }
    function updateListeners():void {
    	object1.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    	object2.removeEventListener(MouseEvent.MOUSE_DOWN, mmouseDownHandler);
    	object1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    	object2.addEventListener(MouseEvent.MOUSE_DOWN, mmouseDownHandler);
    	object4.removeEventListener(MouseEvent.MOUSE_DOWN, mmmmouseDownHandler);
    	object3.removeEventListener(MouseEvent.MOUSE_DOWN, mmmouseDownHandler);
    	object4.addEventListener(MouseEvent.MOUSE_DOWN, mmmmouseDownHandler);
    	object3.addEventListener(MouseEvent.MOUSE_DOWN, mmmouseDownHandler);
    	object5.removeEventListener(MouseEvent.MOUSE_DOWN, fivemouseDownHandler);
    	object6.removeEventListener(MouseEvent.MOUSE_DOWN, sixmouseDownHandler);
    	object5.addEventListener(MouseEvent.MOUSE_DOWN, fivemouseDownHandler);
    	object6.addEventListener(MouseEvent.MOUSE_DOWN, sixmouseDownHandler);
    	object7.removeEventListener(MouseEvent.MOUSE_DOWN, sevenmouseDownHandler);
    	object8.removeEventListener(MouseEvent.MOUSE_DOWN, eightmouseDownHandler);
    	object7.addEventListener(MouseEvent.MOUSE_DOWN, sevenmouseDownHandler);
    	object8.addEventListener(MouseEvent.MOUSE_DOWN, eightmouseDownHandler);
    	object9.removeEventListener(MouseEvent.MOUSE_DOWN, ninemouseDownHandler);
    	object10.removeEventListener(MouseEvent.MOUSE_DOWN, tenmouseDownHandler);
    	object9.addEventListener(MouseEvent.MOUSE_DOWN, ninemouseDownHandler);
    	object10.addEventListener(MouseEvent.MOUSE_DOWN, tenmouseDownHandler);
    	currentObject.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    	currentObject.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    	currentObject.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    	currentObject.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    }
    function ranPlacement():void {
    	ranNumberX=Math.round(Math.random()*80-objectWidthHeight)+350+objectWidthHeight;
    	ranNumberY=Math.round(Math.random()*80-objectWidthHeight)+190+objectWidthHeight;
    }
    //-------------------------------------------------------
    object1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object1;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object1;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object2.addEventListener(MouseEvent.MOUSE_DOWN, mmouseDownHandler);
    function mmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object2;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object2;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object3.addEventListener(MouseEvent.MOUSE_DOWN, mmmouseDownHandler);
    function mmmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object3;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object3;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object4.addEventListener(MouseEvent.MOUSE_DOWN, mmmmouseDownHandler);
    function mmmmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object4;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object4;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object5.addEventListener(MouseEvent.MOUSE_DOWN, fivemouseDownHandler);
    function fivemouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object5;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object5;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object6.addEventListener(MouseEvent.MOUSE_DOWN, sixmouseDownHandler);
    function sixmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object6;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object6;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object7.addEventListener(MouseEvent.MOUSE_DOWN, sevenmouseDownHandler);
    function sevenmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object7;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object7;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object8.addEventListener(MouseEvent.MOUSE_DOWN, eightmouseDownHandler);
    function eightmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object8;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object8;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object9.addEventListener(MouseEvent.MOUSE_DOWN, ninemouseDownHandler);
    function ninemouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object9;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object9;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    object10.addEventListener(MouseEvent.MOUSE_DOWN, tenmouseDownHandler);
    function tenmouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=object10;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=object10;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }
    //-------------------------------------------------------
    function mouseMoveHandler(evt:MouseEvent):void {
    	for (var i:int = 0; i < boxes.length; i++) {
    		if (currentObject!==ff) {
    			if (boxes[i].box.hitTestObject(currentObject.box)) {
    				boxes[i].visible=true;
    				lastBox=boxes[i];
    				collide=true;
    			} else if (!lastBox.box.hitTestObject(currentObject.box)) {
    				lastBox.visible=false;
    				collide=false;
    			}
    		}
    	}
    }
    function mouseUpHandler(evt:MouseEvent):void {
    	for (var ii:int = 0; ii < objects.length; ii++) {
    		if (objects[ii].box.hitTestObject(ff.box)) {
    			trace("one is hitting");
    		} else {
    			currentObject.stopDrag();
    			if (playedPutDownSound==false) {
    				trace("ooo");
    				putDownFunction();
    				playedPutDownSound=true;
    			}
    		}
    	}
    	if (collide==true) {
    		currentObject.x=lastBox.x+0;
    		currentObject.y=lastBox.y+0;
    		lastBox.visible=false;
    		currentObject=ff;
    	} else {
    		collide=false;
    
    	}
    
    }
    I read the forum guildlines than no one likes it when people paste their whole code, but this one is pretty straight forward I guess, nothing too complex.

  2. #2
    Senior Member Draxus's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    123
    I didn't read all of it, but for your event listeners, you can just use one function using the event's current target:

    Code:
    object7.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    object8.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    //etc
    function mouseDownHandler(evt:MouseEvent):void {
    	playedPutDownSound=false;
    	currentObject=evt.currentTarget;
    	updateListeners();
    	if (currentObject==ff) {
    		currentObject=evt.currentTarget;
    		currentObject.startDrag();
    	} else {
    		currentObject.startDrag();
    		pickUpFunction();
    	}
    }

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    I think no one have that must of time to make a working application with all your declared assets on your code and test whether the modifications works or not. You might missed to get a perfect solutions of your code structure. I recommend you to post your source as a sample to get quick help.

    BTW you can do this:

    ActionScript Code:
    var ranNumberX:Number;
    var ranNumberY:Number;

    for (var i:int=0; i<=10; i++) {
        var obj:String="object"+(i+1);
        this[obj] = new object();
        this[obj].cacheAsBitmap=true;
        addChild(this[obj]);
        this[obj].gotoAndStop("TopLeft");
        ranPlacement(this[obj]);
        this[obj].x=ranNumberX;
        this[obj].y=ranNumberY;
        this[obj].buttonMode=true;
        this[obj].addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

    }
    function updateListeners(object):void {
        object.buttonMode=false;
        object.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    }
    function mouseDownHandler(evt:MouseEvent):void {
        updateListeners(evt.target);
    }

    function ranPlacement(object):void {
        ranNumberX=Math.round(Math.random()*80-object.width)+350+object.width;
        ranNumberY=Math.round(Math.random()*80-object.height)+190+object.height;
    }

    arkitx
    Last edited by arkitx; 07-12-2011 at 03:00 PM.

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