A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Changing a Variable name

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Posts
    4

    Changing a Variable name

    var fish:Number = 2;
    for (i=1; i<=fish; i++) {
    var xdir:Number = Math.random()*100;
    var ydir:Number = Math.random()*100;
    if (xdir<50) {
    _root.fish_(i.valueOf).xspeed=5

    } else {
    _root.fish_(i.valueOf).xspeed=5
    }
    if (ydir<50) {
    (fish_(i.valueOf())).yspeed = -5;
    } else {
    (fish_(i.valueOf())).yspeed = 5;
    }

    }

    The part that isnt working is the value of part, im trying to make it so that the loop adresses both fish by changing the name of the variable.
    So there is fish_1 and fish_2 and i want to loop twice and pick either negative 5 or positive for x and y speed but how do i change the name of the variabel fish_...???
    I am so lazy that i can't type a signature that would in-turn be shorter than this explination

  2. #2
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Try using eval() to create a variable that will point to each fish. In the example below, the variable "wf" is assigned to point to either "fish1" MC or "fish2" MC on the stage. Then each MC is given an "xspeed", "yspeed", and onEnterFrame function, and has it's x scale changed to point the way it is going to move, by using the "wf" variable to point to it.

    Code:
    var fish:Number = 2;
    for (i=1; i<=fish; i++) {
    	var wf:MovieClip = eval("fish"+i);
    	wf.xspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    	wf.yspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    	wf._xscale = 20*wf.xspeed;
    	wf.onEnterFrame = function() {
    		this._x += this.xspeed;
    		this._y += this.yspeed;
    	};
    }

  3. #3
    Junior Member
    Join Date
    Oct 2004
    Posts
    4
    neato! Thanks

    Ehhh...Not so neato, that totally screwed my collision affect where i have the3 two fish "bounce" off each other, now its like an orbital sling shot lol
    Last edited by Fragtagonal; 04-30-2007 at 10:06 PM.
    I am so lazy that i can't type a signature that would in-turn be shorter than this explination

  4. #4
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    you can keep your old code the same with:
    Code:
    var fish:Number = 2;
    for (i=1; i<=fish; i++) {
    	var myFish:MovieClip = ["fish"+i];
    	var xdir:Number = Math.random()*100;
    	var ydir:Number = Math.random()*100;
    	if (xdir<50) {
    		myFish.xspeed = -5;
    	} else {
    		myFish.xspeed = 5;
    	}
    	if (ydir<50) {
    		myFish.yspeed = -5;
    	} else {
    		myFish.yspeed = 5;
    	}
    }
    Although Quixx's use of :
    wf.xspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    wf.yspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    shortens up the code you have used considerably

    so....
    Code:
    var fish:Number = 2;
    for (i=1; i<=fish; i++) {
    	var myFish:MovieClip = ["fish"+i];
    	myFish.xspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    	myFish.yspeed = random(2)%2 == 0 ? dir=-5 : dir=5;
    	//myFish._xscale = 20*myFish.xspeed;
    	//myFish.onEnterFrame = function() {
    		//this is where your fish were speeding up -it's adding the new speeds
    		//to their current position on every frame - so they get faster!!!!
    		//this._x += this.xspeed;
    		//this._y += this.yspeed;
    	//};
    }
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  5. #5
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Thanks for assist Boris the Frog. My apologies Fragtagonal. I should've explained that the code I posted was only an example of how you would use the variable created using eval(["MC"+i) (or in Boris the Frog's example, ["MC"+i]). I didn't intend for you to be able to drop everything there into your clip.

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