Assigning different variable values
- aerosmith1
-
aerosmith1
- Member since: Dec. 13, 2005
- Offline.
-
- Forum Stats
- Member
- Level 16
- Gamer
I've been trying to assign different values for a variable for different movieclips. I'm a noob and for I can't figure out why it isn't working. Could you help?
var speed:Number = 1;
box_mc.speed = 5;
ball_mc.speed = 3;
box_mc.onEnterFrame = moveStuff;
ball_mc.onEnterFrame = moveStuff;
Everything moves at the speed of one. This obviously doesn't show the function but I don't think you will really need that.
- CyanSandwich
-
CyanSandwich
- Member since: Aug. 14, 2009
- Offline.
-
- Forum Stats
- Member
- Level 41
- Programmer
You can't have different values for the same exact variable at the same time. If it's only two movieclips then just have two different variable and functions.
speed = 5
speed2 = 3
box_mc.onEnterFrame = moveStuff;
ball_mc.onEnterFrame = moveStuff2;
- milchreis
-
milchreis
- Member since: Jan. 11, 2008
- Offline.
-
- Forum Stats
- Member
- Level 26
- Programmer
Why do you define "var speed" to be one?
Could you please post the function? thanks.
- aerosmith1
-
aerosmith1
- Member since: Dec. 13, 2005
- Offline.
-
- Forum Stats
- Member
- Level 16
- Gamer
I saw sample code somewhere else where it let you assign different values for different movieclips and none of the comments below seemed to have problems with it so I wasn't sure what the deal was.
function moveStuff(){
if(Key.isDown(Key.LEFT)){
this._x -= speed;
}
if(Key.isDown(Key.RIGHT)){
this._x += speed;
}
if(Key.isDown(Key.UP)){
this._y -= speed;
}
if(Key.isDown(Key.DOWN)){
this._y += speed;
}
if(this._x>Stage.width+(this._width/2)){
this._x=0-(this.width/2)
}
if(this._x<0-(this._width/2)){
this._x=Stage.width+(this._width/2)
}
if(this._y>Stage.height+(this._height/2)){
this._y=0-(this._width/2)
}
if(this._y<0-(this._width/2)){
this._y=Stage.height+(this._height/2)
}
}
box_mc.speed = 5;
ball_mc.speed = 3;
box_mc.onEnterFrame = moveStuff;
ball_mc.onEnterFrame = moveStuff; - milchreis
-
milchreis
- Member since: Jan. 11, 2008
- Offline.
-
- Forum Stats
- Member
- Level 26
- Programmer
You are moving the whole world, with a ball and box both standing still on it.
If you want to move an object, change its x/y coordinates.
- aerosmith1
-
aerosmith1
- Member since: Dec. 13, 2005
- Offline.
-
- Forum Stats
- Member
- Level 16
- Gamer
No it's moving the ball and box because I'm making the function run through the movie clips right?
- Jaface
-
Jaface
- Member since: Aug. 26, 2010
- Offline.
-
- Forum Stats
- Member
- Level 09
- Blank Slate
At 12/15/10 07:09 PM, aerosmith1 wrote: No it's moving the ball and box because I'm making the function run through the movie clips right?
If everything is moving at the speed of 1, my guess is that the global speed variable is being used instead of the mc speed variable. Try changing all your speed calls to "this.speed".

