dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Detecting which sub-array registers a click event

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    29

    Detecting which sub-array registers a click event

    I am trying to ascertain which sub array from the main array received a click event. My approach below makes use of a switch case at the end for this purpose;

    Code:
    var localSegment:Array = [segment1.system_Cab, segment1.programs_Cab, segment1.userFiles_Cab, segment1.rollback_Cab]
    var external_MediaSegment:Array = [segment2.externalHD_Cab, segment2.virtualDisk_Cab]
    var network_LocationSegment:Array = [segment3.homeServer_Cab, segment3.wifiFlashdrive_Cab, segment3.encrivaPlay_Cab]
    var superVolumes:Array = [localSegment, external_MediaSegment, network_LocationSegment]
    
    
    for (var i:Number = 0; i < superVolumes.length; i++ ){
    	for (var j:Number = 0; j< superVolumes[i].length; j++){
    	var fmCabinent = superVolumes[i][j];
    	fmCabinent.addEventListener(MouseEvent.CLICK, openCabinet);
    	}
    }
    
    var arrayContainer:Array; //create a var to reference the clicked item's parent array
    var curIndex:int; //a temporary variable to store the index of clicked object
    var targetCabinent;
    
    function openCabinet (e:MouseEvent):void{
        targetCabinent = e.currentTarget;
    	for (var i:Number = 0; i < superVolumes.length; i++ ){
    	curIndex = superVolumes[i].indexOf(e.currentTarget); //see if the sub array contains the clicked item
    	if(curIndex > -1){
           //if the sub array contains the clicked item (e.currentTarget)
           arrayContainer = superVolumes[i][curIndex];
           break; //stop looping since you found the array
        }
    	if (targetCabinent.currentFrame == 1){
    		targetCabinent.play();
    		
    	}else {
    		fmCabinent.addEventListener(Event.ENTER_FRAME, closeCabinet);
    		
    	}
    	switch(arrayContainer){
        case localSegment:
            trace("You clicked an item from local segment");
            break;
    
        case external_MediaSegment:
            trace("YOu clicked something from media segment");
            break;
    
        case network_LocationSegment:
            trace("You clicked something from network_LocationSegment");
    		break;
    		}
    	}
    }	//stage.addEventListener (MouseEvent.CLICK, outFocusClose);
    
    
    
    function closeCabinet (e:Event):void{
     	if (targetCabinent.currentFrame > 1){
    		targetCabinent.prevFrame();
    	
    	}else{
    		fmCabinent.removeEventListener(Event.ENTER_FRAME, closeCabinet);
    		//stage.removeEventListener (MouseEvent.CLICK, outFocusClose);
    	}
    }
    However I keep getting a type error in the output tab that says:
    TypeError: Error #1034: Type Coercion failed: cannot convert Phoenix_Assetbuilder_fla::Systemfilescabinet_17@5d 1b5661 to Array.

  2. #2
    Senior Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    936
    can you post the fla

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    29
    Attached is the file requested. I tried to strip it down to meet the upload quota for the file type.
    Attached Files Attached Files

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

    Not sure if this is what you meant, mess around with it though.
    Attached Files Attached Files

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,776
    Hi,

    Sorry, the code!
    PHP Code:
    import flash.display.MovieClip;

    var 
    localSegment:Array = [
        
    segment1.system_Cab,
        
    segment1.programs_Cab,
        
    segment1.userFiles_Cab,
        
    segment1.rollback_Cab
    ];

    var 
    external_MediaSegment:Array = [
        
    segment2.externalHD_Cab,
        
    segment2.virtualDisk_Cab
    ];

    var 
    network_LocationSegment:Array = [
        
    segment3.homeServer_Cab,
        
    segment3.wifiFlashdrive_Cab,
        
    segment3.encrivaPlay_Cab
    ];

    var 
    superVolumes:Array = [
        
    localSegment,
        
    external_MediaSegment,
        
    network_LocationSegment
    ];

    var 
    superNames:Array = [
        
    "localSegment",
        
    "external_MediaSegment",
        
    "network_LocationSegment"
    ];

    var 
    i,j:Number;
    var 
    clickState:String;

    for ( 
    0superVolumes.lengthi++ )
    {
        var 
    parentArray superNames[i];
        
    //trace( parentArray );
        
        
    for ( 0superVolumes[i].lengthj++ )
        {
            var 
    fmCabinet superVolumes[i][j];
            
    fmCabinet.arrayName parentArray;
            
    fmCabinet.isClicked 0// declare open or close upon click
            
    fmCabinet.addEventListenerMouseEvent.CLICK openCabinet );
        }
    }

    function 
    openCabinete:MouseEvent ):void
    {
        var 
    targetCabinet e.currentTarget;
        
    //trace( targetCabinet );
        
        
    var targetName e.currentTarget.name;
        
    //trace( targetName );
        
        
    var arrayContainer targetCabinet.arrayName;
        
    //trace( arrayContainer );
        
        
    for ( 0superVolumes.lengthi++ )
        {
            if ( 
    targetCabinet.currentFrame == )
            {
                
    targetCabinet.play();
                
    targetCabinet.isClicked 0;
            }
            else
            {
                
    targetCabinet.isClicked 1;
                
    e.currentTarget.addEventListenerEvent.ENTER_FRAME closeCabinet );
            }
        }
        
        switch ( 
    arrayContainer )
        {
            case 
    arrayContainer :
                
                if ( 
    targetCabinet.isClicked === )
                {
                    
    clickState "opened";
                }
                else
                {
                    
    clickState "closed";
                }
                
                
    trace"You " clickState " '" targetName "' an item from [" arrayContainer "] array" );
                break;
        }
    }

    function 
    closeCabinete:Event ):void
    {
        if ( 
    e.currentTarget.currentFrame )
        {
            
    e.currentTarget.prevFrame();
        }
        else
        {
            
    e.currentTarget.removeEventListenerEvent.ENTER_FRAME closeCabinet );
        }

    Last edited by fruitbeard; 07-15-2018 at 05:26 AM.

  6. #6
    Junior Member
    Join Date
    Jan 2018
    Posts
    29
    Thanks for your input, it would have been smart approach except that the trace logic was just for debugging, to test weather flash could identify what array the clicked object belong to. In essence the idea is that clicking and object from say; externalMedia array will execute a function which handles a chain of movie clip tweens and so the only way to know if it would be able accomplish running the functions made for it is by testing it through the trace statement. Don't know if I am making sense?

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,776
    Hi

    Not really no.
    If you mean importing arrays I am positive that you can create and fill the holding arrays and reference them.
    For output purpose you just swap the traces for some other code or call another function etc etc

  8. #8
    Junior Member
    Join Date
    Jan 2018
    Posts
    29
    okay, I will try swapping out the traces as you advised, and revert back. Thanks for your advice.

  9. #9
    Junior Member
    Join Date
    Jan 2018
    Posts
    29
    thanks so much, it worked, but please I would like to know why my initial approach didn't work

Tags for this Thread

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