A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Move a clip by referencing arrays??

  1. #1
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714

    Move a clip by referencing arrays??

    Hi

    My problem is this:

    I have clips on stage named: bean_1,bean2, bean3

    I have an array to reference the clips: moveBeansArray[bean_1,bean_2,bean_3]

    I have an array that stores _x co-ordinates: xPosArray[200,250,300]

    I then have a function to move a clip via a flag variable: movebeans

    Code:
    onEnterFrame = function() {
    
    if (movebeans == true) {
    moveBeansArray[0]._x += (xPosArray[0]-moveBeansArray[0]._x)/10;
    }
    }
    But no luck!
    It only works if I use the actual
    instance name of the clip i.e.(bean_1) that i want to move,

    Code:
    onEnterFrame = function() {
    if (movebeans == true) {
    bean_1._x += (xPosArray[0]-bean_1._x)/10;
    }
    		}
    }
    Any help is much appreciated, tried many variations but can't find a solution!!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    How are you setting up your arrays?
    Code:
    var movebeans = true;
    var moveBeansArray = new Array(bean_1, bean_2, bean_3);
    var xPosArray = new Array(200, 250, 300);
    
    onEnterFrame = function () {
    	if (movebeans == true) {
    		moveBeansArray[0]._x += (xPosArray[0]-moveBeansArray[0]._x)/10;
    	}
    };

  3. #3
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Hi
    Thanks for reply - like this:
    A btn_mc is hitTesting the clips:

    Code:
    var xPosArray:Array = new Array();
    xPosArray = [200, 250, 300]
    //
    var moveBeansArray:Array = new Array();
    //
    btn_mc.onRelease = function() {
    for (var k:Number = 1; k<=4; k++) {
    			if (this.hitTest(_root["bean_"+k])) {
    				
    				moveBeansArray.push([_root["bean_"+k]]);
    				
    }
    //trace("moveBeansArray="+moveBeansArray)
    //trace("xPosArray="+xPosArray)
    }
    Any ideas?

  4. #4
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    try adding a trace function to it:
    if (movebeans == true) {
    moveBeansArray[0]._x += (xPosArray[0]-moveBeansArray[0]._x)/10;
    trace(moveBeansArray[0]._name);
    }
    If it comes out undefined, make sure you don't have spelling inconsistencies between the clip names and what is in the array. Also, make sure that the array you created and the array that you are referencing are the same array.
    You could also try this:
    if (movebeans == true) {
    moveBeansArray[0]._x += 5;
    }
    To make sure that it is the moveBeansArray that has the problem, and not the xPosArray.
    Syntax Error

  5. #5
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Thanks for reply,
    Have checked that....
    Here is a fla. with the actual working code showing what i am trying to do - it works when i ref the actual clip name (bean_1)

    Then i've repeated the same code underneath but used the moveBeansArray[0] instead of bean_1 and then it doesn't work! i need it to work using the array?

    Thanks again
    Attached Files Attached Files

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    btn_mc0.onPress = function() {
    	movebeans = true;
    	for (var k:Number = 1; k<=4; k++) {
    		if (this.hitTest(_root["bean_"+k])) {
    			moveBeansArray.push(_root["bean_"+k]);
    		}
    		onEnterFrame = function () {
    			if (movebeans == true) {
    				moveBeansArray[0]._x += (xPosArray[0]-moveBeansArray[0]._x)/10;
    			}
    		};
    	}
    	trace("moveBeansArray="+moveBeansArray);
    	trace("xPosArray="+xPosArray);
    };

  7. #7
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Thanks dawsonk,

    Can't believe I missed that!!!!!

    Nice one

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