A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] evt.target problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    13

    resolved [RESOLVED] evt.target problem

    I had this really wierd unexpected problem recently. I had a movieclip NormalBlock with it's own NormalBlock.as and I am instantiating about 400 of those at runtime. The movieclip had initially 5 frames with different color squares in them so I could use gotoAndStop(1-5) whenever user clicked on it. On my main timeline on 5th frame I created a container mc, for those 400 instances of NormalBlock, called grid & attached an event listener to it. NormalBlock.as also has an eventlistener inside it.
    Now previously I used evt.target.someFunc() inside the function onClicking() registered with the eventlistener attached to grid mc to call someFunc() inside NormalBlock.as & it worked.

    But recently I updated the movieclip. Basically I copy pasted the new movieclip from another fla to my fla, deleted the old movieclip, gave the same name & class to the new movieclip(I now think I should have used the swap thingy..). But this time evt.target.someFunc() is not calling the function at all . I also tried evt.currentTarget.someFunc() but to no avail. The only change in the new movieclip is that those frames inside have a movieclip each in them instead of just colored square graphics.
    The eventlistener inside NormalBlock.as is working perfectly. Also using references I can access the functions inside NormalBlock.as. But somehow evt.target is screwed up. What could be wrong?
    Did I mess up while changing movieclips? The source section inside properties for the movieclip also shows the other fla's path. But that fla I will be deleting later. Will the mc in my current fla go blank or something if I delete that other fla?

    NormalBlock.as

    Code:
    package {
    
    	import flash.display.*;
    	import flash.events.*;
    
    	public class NormalBlock extends MovieClip {
    		private var index:int;
    		private var overTurned:Boolean;//this is to see if the user has clicked or not
    		private var nextFr:int;//the next color underneath
    		private var blockType:int;
    		public var isPartOfShape:Boolean;
    
    		public function NormalBlock(_index:int,_nextFr:int,_isPartOfShape:Boolean = false) {
    			index=_index;
    			nextFr=_nextFr;
    			isPartOfShape = _isPartOfShape;
    			overTurned=false;
    			buttonMode=true;
    			useHandCursor=true;
    
    			addEventListener(MouseEvent.CLICK,onClick,false,0,true);
    			blockType = 0;
    
    		}
    
    		private function onClick(evt:MouseEvent) {
    
    			if (overTurned) {
    				gotoAndStop(nextFr);
    				overTurned=false;
    				nextFr-=11;
    			} else {
    				gotoAndStop(nextFr);
    				overTurned=true;
    				nextFr+=11;
    			}
    		}
                    public function test():Boolean{
                            return true;
                    }
    		public function get getIndex():int {
    			return index;
    		}
    		public function get getType():int {
    			return blockType;
    		}
    		public function get getColor():int {
    			return nextFr;
    		}
    		public function get isOverTurned():Boolean {
    			return overTurned;
    		}
    		public function get isShape():Boolean {
    			trace("ispartofshape");
    			return isPartOfShape;
    		}
    		public function set setIsShape(itis:Boolean) {
    			trace("settingispartofshape");
    			isPartOfShape=itis;
    		}
    		public function set setColor(_color:int) {
    			nextFr=_color;
    		}
    		public function set setType(type:int) {
    			blockType=type;
    		}
    		public function resetType(_shapeColor:int) {
    			if (isPartOfShape||nextFr==_shapeColor) {
    				blockType=1;
    			}
    		}
    		public function stopListener() {
    			buttonMode=false;
    			useHandCursor=false;
    			removeEventListener(MouseEvent.CLICK,onClick);
    
    		}
    	}
    
    }


    The code in main timeline


    Code:
    function init()
    {
    ....
    	grid = new MovieClip(); //main container holding all the blocks
    	addChild(grid);
    	grid.addEventListener(MouseEvent.CLICK,onClicking,false,0,true);
    ...
    }
    function startGame()
    {
    ...
    	for(var i:int = 0;i < TOTAL_BLOCKS;i++)
    	{
    		var nBlock:MovieClip = new NormalBlock(0,2);
    		nBlock.x = GRID_X + (i%20) * 20;
    		nBlock.y = GRID_Y + (Math.floor(i/20)) * 20;
    		grid.addChild(nBlock);
    	}
    ....
    
    }
    
    function onClicking(evt:MouseEvent)
    {
    ...
    	trace("CURRENT: "+evt.currentTarget);
            trace("USUAL "+evt.target);
            if(evt.target.test())
            {
                    trace("COMING HERE"); 
            }
    	if(evt.target.isPartOfShape) 
    	{
    		trace("WOOOOOOOOOOOOT");
                    lots of code....;
    	}
    ...
    }
    Last edited by lastMohican; 10-31-2010 at 01:21 PM. Reason: Added code

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    As far as your code shows, and assuming that bolded line is your "source of error", you're not calling any functions of the movieclip. You're just checking if the isPartOfShape property is true or not, and when you created those movieclips you never specified a value for that property so it defaults as false. So you don't get a trace.

    That's just from looking at your code, I don't see any reason why you won't be able to call a function from the movieclip. Maybe I'm misread something.

    If you already copied the movieclip from the old FLA to the new FLA, it won't disappear when you delete the old FLA.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Ah sry about that. I was trying various things to resolve the issue so kinda copypasted the modified code. Rectified that now & also put in a new function just for testing evt.target

    I have kinda narrowed down the problem though. It's something to do with event propagation I think. The movieclip I am using contains nested movieclips in each frame. I tested with another movieclip containing only basic shapes in every frame & it worked properly.
    I have attached a test mc which will make this clear. For the 3rd frame in that mc if I use evt.target.parent.test() then it would work. But that code now won't work for the first 2 frames!
    Attached Files Attached Files
    Last edited by lastMohican; 10-31-2010 at 03:09 PM. Reason: Added testmovie.as as txt file.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    I don't understand what you're trying to do in the attached files, how do I put them together?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    13

    resolved

    Ah I got it! Removed the grid listener & added the individual block listeners in the main timeline instead of the as file. Now evt.currentTarget always gives me [object NormalBlock]

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