-
14yr old Member
[MX04]changing variable without telling it to
i have an mc (base) that when clicked on generates another mc(unit) and you can control that newly generated mc with the arrow keys. but everytime i click the base mc the next unit that comes out moves faster.. any ideas why?
PHP Code:
radius=10;
var number = 1;
power = 2;
function Move(){
if(Key.isDown(Key.LEFT)){
_root["unit"+number]._x-=power;
_root["unit"+number].gotoAndStop(2);
}
else
if(Key.isDown(Key.RIGHT)){
_root["unit"+number]._x+=power;
_root["unit"+number].gotoAndStop(4);
}
else
if(Key.isDown(Key.UP)){
_root["unit"+number]._y-=power;
_root["unit"+number].gotoAndStop(1);
}
else
if(Key.isDown(Key.DOWN)){
_root["unit"+number]._y+=power;
_root["unit"+number].gotoAndStop(5);
}
}
base.onPress=function(){
number++;
MainT.duplicateMovieClip("unit"+number, number);
_root["unit"+number]._x = base._x;
_root["unit"+number]._y = base._y;
_root["unit"+number].onEnterFrame = function(){
Move();
if(rock.hitTest(this._x, this._y+radius, true))this._y-=power;
if(rock.hitTest(this._x, this._y-radius, true))this._y+=power;
if(rock.hitTest(this._x-radius, this._y, true))this._x+=power;
if(rock.hitTest(this._x+radius, this._y, true))this._x-=power;
}
}
-
14yr old Member
-
http://www.in3d.eu
Hi,
I think is more reliable to check for keyboard events using a keyListener instead of checking in the "onEnterFrame" event..
The alternative code (just uses a keyListener to check for key events) is:
PHP Code:
radius=10;
var number = 1;
power = 2;
//------------------- KEY CHECKING ----------------------------
var keyListener=new Object() // Create a new Listener object
keyListener.onKeyDown=function (){
if(Key.isDown(Key.LEFT)){
_root["unit"+number]._x-=power;
_root["unit"+number].gotoAndStop(2);
}
if(Key.isDown(Key.RIGHT)){
_root["unit"+number]._x+=power;
_root["unit"+number].gotoAndStop(4);
}
if(Key.isDown(Key.UP)){
_root["unit"+number]._y-=power;
_root["unit"+number].gotoAndStop(1);
}
if(Key.isDown(Key.DOWN)){
_root["unit"+number]._y+=power;
_root["unit"+number].gotoAndStop(5);
}
}
Key.addListener(keyListener)
//------------------- KEY CHECKING (END) ------------------------
base.onPress=function(){
number++;
MainT.duplicateMovieClip("unit"+number, number);
_root["unit"+number]._x = base._x;
_root["unit"+number]._y = base._y;
_root["unit"+number].onEnterFrame = function(){
if(rock.hitTest(this._x, this._y+radius, true))this._y-=power;
if(rock.hitTest(this._x, this._y-radius, true))this._y+=power;
if(rock.hitTest(this._x-radius, this._y, true))this._x+=power;
if(rock.hitTest(this._x+radius, this._y, true))this._x-=power;
}
}
Kostas
-
14yr old Member
thanks, that makes the speed stay at a constant but the unit mc won't move when the key is held, only when the key is pressed
-
http://www.in3d.eu
Hello,
don't mention it
..but the unit mc won't move when the key is held, only when the key is pressed
(When say "held" mean to press the key and keep it i suppose..)
Strange.. I use the above code with simple movies (in order to produce similar conditions) and seems to work.. I mean that while the key is pressed (is down) the "unit" duplicated files are moving..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|