Results 1 to 10 of 10

Thread: Flash 8 - using a variable to target a movie clip

  1. #1

    Flash 8 - using a variable to target a movie clip

    I found this tutorial which seems to do exactly what I'm trying to do (the only one I've found to date), except his technique doesn't seem to work for me at all. It doesn't really seem to say anywhere which version of Actionscript he uses, but at least it seems to show that what I'm trying to do is possible for someone, which is a start.

    I'm trying to do exactly what he describes--I have several squares on a grid that are numbered, eg. grid_0101, grid_0102, grid_0103, and so on, for about 100 squares altogether, and I want to be able to target a given square dynamically.

    This is the basis of the code I started with:

    Code:
    var digsquare_x:String = "01";
    var digsquare_y:String = "03";
    var digthis:String = "grid_" + digsquare_x + digsquare_y;
    this[digthis]._alpha = 50;
    I don't need to change the alpha specifically, I was just trying to see if I could get it to affect a given square in any way, and apparently the answer is no. I know that the value of "digthis" is accurate, but even blatantly telling it to refer to _level10[digthis] still gives me no results. Neither does _root.level10[digthis] which I tried just to see if it would work. The tutorial seemed like a good starting point, but no matter what I do nothing happens, and I'm stumped.

    What am I doing wrong? Is there a way to do this in AS2? Being able to use this technique would make a lot of the projects I've been working on much simpler, if I could get it to work. Any help would be greatly appreciated!

  2. #2
    1,391
    posts
    Registered User
    well the code you have above functions just fine concatenating the string, and the syntax looks correct

    i would then have to say that there are not 'instance names' on each of the clips on stage? you may have named them that way in the Lib, but did you also type in the instance names in the properties panel for each block on the grid?

    so often what we'd want to do is to place the instances into an array, this way you can track them by index, find them by lookup, and even get their coordinates using the index - here is a little snippet to demonstrate, unfortunately i wrote it in AS3, but you should be able to create a new as3 doc, plug this in the first frame and run it - it dynamically construct the grid, and assigns a simple listener to remove it from the stage and output a little information about it, when you click any block
    Code:
    var blocks:Array = [];
    var columns:int = 10;
    var rows:int = 10;
    var size:int = 20;
    var color:uint = 0x000000;
    
    for( var i:int=0; i<rows*columns; i++ ) {
    	var b:Sprite = new Sprite();
    	b.x = Math.floor( i % rows ) * size;
    	b.y = Math.floor( i / rows ) * size;
    	b.graphics.beginFill( color+=2.56 );
    	b.graphics.drawRect( 0, 0, size, size );
    	b.graphics.endFill();
    	b.addEventListener( MouseEvent.CLICK, remove, false, 0, true );
    	
    	blocks.push( addChild( b ) );
    }
    
    function remove( e:MouseEvent ):void {
    	var id:int = blocks.indexOf( e.currentTarget );
    	trace( "REMOVED BLOCK: ROW="+Math.floor(id/columns)+" COL="+Math.floor(id%columns)+" ID="+id );
    	e.currentTarget.parent.removeChild(e.currentTarget);
    }

  3. #3
    TheCanadian's Avatar
    10,300
    posts
    Noo doot aboot it, eh?
    AS2 has a function called eval which does what you want, but it also won't work if the path is wrong. Personally, I'd stick with the array access code that you have. Your problem is probably that this.grid0103 doesn't exist.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  4. #4
    1,391
    posts
    Registered User
    agree C - but really? what if your map names change, or r/c size - or you try to point at an object that doesn't exist

  5. #5
    I can't use AS3. My version of Macromedia Flash uses AS2 only, hence some of my concerns--if this MUST use AS3 then I'm sunk.

    Yes, every clip on stage has its own instance name--I went through and named each one by hand, and went back later and double-checked to make sure everything looked the same. I also have some actionscript which targets several of them specifically, and that functions fine, so I know their names are correct and they are "accessible" under mundane circumstances. It's just this particular circumstance that doesn't work.

    If I replace the line:

    Code:
    this[digthis]._alpha = 50;
    ...with this:

    Code:
    _root.grid_0103._alpha = 50;
    ...even just doing that will cause it to work.



    I don't know if it matters that this is in an onEnterFrame function in the main timeline--the tutorial wasn't very specific about where it should be used, but after 24 hours I'm starting to wonder if that could be part of the problem. I tried putting it on a movieclip instead, but that broke it (I'll have to try again after work, when I can really put a decent amount of effort into it) so for the time being it's still on the main timeline. Currently the entire function looks like this:

    Code:
    onEnterFrame = function(){
        //Spacebar = dig
        if(Key.isDown(key_dig)){
            if(_root.active == false){
                _root.active = true;
                var digsquare_x:String = String(onsquare_x);
                var digsquare_y:String = String(onsquare_y);
                if(digsquare_x < 10){digsquare_x = "0" + digsquare_x;}
                if(digsquare_y < 10){digsquare_y = "0" + digsquare_y;}
                var digthis:String = "grid_" + digsquare_x + digsquare_y;
                this[digthis]._alpha = 50;
                trace("dig!");
                _root.char_top.gotoAndPlay("dig");
            }
        }
    }
    The entire function works perfectly, with the exception of that one line.
    Last edited by Ghostfishe; April 16th, 2012 at 11:44 AM.

  6. #6
    TheCanadian's Avatar
    10,300
    posts
    Noo doot aboot it, eh?
    No, as I said, this is possible in AS2. Have you tried _root[digthis]._alpha = 50? Are you sure digthis is "grid_0103"?

    Quote Originally Posted by cbeech View Post
    agree C - but really? what if your map names change, or r/c size - or you try to point at an object that doesn't exist
    I meant instead of eval. What if a meteor lands on your head?
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  7. #7
    Yes, I have traced the variable and know its value is grid_0103.

    Substituting "_root" for "this" seems like a good idea, but gives exactly the same results. Still no error, but no alpha effect either. Hmm...

  8. #8
    TheCanadian's Avatar
    10,300
    posts
    Noo doot aboot it, eh?
    Okay well if you can use _root.grid_0103 to access it then you can also use _root["grid_0103"] to access it. I don't know what else to tell you.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  9. #9
    1,391
    posts
    Registered User
    as C has said, certainly can be done in AS2 - sorry i didn't mean to lead you astray there

    i don't have anything either, there must be something wrong with the path still... or - is the timeline stopped? perhaps it's reinstantiating

    --------------------
    I meant instead of eval. What if a meteor lands on your head?
    LOL i've got a special helmet for that...

  10. #10
    I finally got it working this morning. Thanks for all the help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012