A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Noob Question: What is "this" for?

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    12

    Question Noob Question: What is "this" for?

    When do i need to use "this" code? What is it for?
    e.g. this.createEmptyMovieClip and createEmptyMovieClip works the same?

  2. #2
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    You don't need to use 'this'.
    You can use it if you want to disambiguate a situation -- make clear your intentions.
    Most people don't use 'this'.

    Maybe 'this' is legacy from Flash 5/6 era?

  3. #3
    Junior Member
    Join Date
    Oct 2008
    Posts
    12
    http://board.flashkit.com/board/showthread.php?t=779885

    like in his question code? he is using this.

  4. #4
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    That code still works when you remove the 'this.' from it.
    (come on, you could have tested that yourself...)

  5. #5
    Junior Member
    Join Date
    Oct 2008
    Posts
    12
    ok ok sorry for being too lazy..:P

    but why do programmers have to still put the 'this' on their code?

  6. #6
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831
    You guys are quite correct regarding usage of keyword 'this'. It's of no use in the above pieces of code, but there are a few instances where I need to mention you need to use 'this':

    1. Ideally 'this' is used with drag and dropping objects. I dont think you can manage without 'this' in drag n drops.
    2. When some one has nested movieclips within his particular FLA and if he needs to maintain the pathof any single movie clip/object, then a variable is created and that variable is assigned with 'this' and once theis is done then we can access that variable to access that object.
    3. this["variable_name"] allows you to dynamically create variables, arrays etc.

    hope this helps!
    As ever,
    Vinayak Kadam

  7. #7
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    Hmm,

    Just did a quick test, and you can use dragging without using 'this'

    AS2 - need an mc with instance name of mrDrag on the stage.
    This code goes on the timeline
    Code:
    mrDrag.onPress = dragMe;
    mrDrag.onRelease = releaseMe;
    
    function dragMe():Void
    {
    	startDrag(mrDrag);
    }
    
    function releaseMe():Void
    {
    	mrDrag.stopDrag();
    }
    AS3 - again, need an mc with instance name of mrDrag on the stage.
    This code goes on the timeline.
    Code:
    mrDrag.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
    mrDrag.addEventListener(MouseEvent.MOUSE_UP, releaseMe);
    
    mrDrag.buttonMode = true;
    
    function dragMe(event:MouseEvent):void
    {
    	mrDrag.startDrag();
    }
    
    function releaseMe(event:MouseEvent):void
    {
    	mrDrag.stopDrag();
    }

  8. #8
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831
    I was really wondering about this one. You are quite correct, but actually the case I was saying about is was when u had too many drags and drops.... in that situation I think you force fully have to use this to make the code more dynamic.

    What do you think ?
    As ever,
    Vinayak Kadam

  9. #9
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    I'm no expert, so someone else will probably come across and correct me :lol:

    Ok, for this code, set up a MovieClip with the instance name 'myMovieclips' and place as many MovieClips inside it as you want, they don't need instance names or anything special, just artwork to click on (I did red boxes).

    AS3
    Code:
    myMovieclips.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
    myMovieclips.addEventListener(MouseEvent.MOUSE_UP, releaseMe);
    
    myMovieclips.buttonMode = true;
    
    function dragMe(event:MouseEvent):void
    {
    	event.target.startDrag();
    }
    
    function releaseMe(event:MouseEvent):void
    {
    	event.target.stopDrag();
    }

    This is using event propagation, check this article for an in depth explanation (I haven't read it all yet): http://www.adobe.com/devnet/actionsc...dling_as3.html

    I'm not sure the best way to do this in AS2, one case where the AS3 solution is quite elegant.

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