A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [MX04]changing variable without telling it to

  1. #1
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272

    [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"+numbernumber);
        
    _root["unit"+number]._x base._x;
        
    _root["unit"+number]._y base._y;
        
    _root["unit"+number].onEnterFrame = function(){
            
    Move();
            if(
    rock.hitTest(this._xthis._y+radiustrue))this._y-=power;
            if(
    rock.hitTest(this._xthis._y-radiustrue))this._y+=power;
               if(
    rock.hitTest(this._x-radiusthis._ytrue))this._x+=power;
            if(
    rock.hitTest(this._x+radiusthis._ytrue))this._x-=power;
        }


    Signature Goes Here

  2. #2
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    bump
    Signature Goes Here

  3. #3
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    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"+numbernumber); 
        
    _root["unit"+number]._x base._x
        
    _root["unit"+number]._y base._y
        
       
    _root["unit"+number].onEnterFrame = function(){ 
            if(
    rock.hitTest(this._xthis._y+radiustrue))this._y-=power
            if(
    rock.hitTest(this._xthis._y-radiustrue))this._y+=power
            if(
    rock.hitTest(this._x-radiusthis._ytrue))this._x+=power
            if(
    rock.hitTest(this._x+radiusthis._ytrue))this._x-=power
        } 


    Kostas
    K. Zotos online portfolio: http://www.in3d.eu

  4. #4
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    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
    Signature Goes Here

  5. #5
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    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..
    K. Zotos online portfolio: http://www.in3d.eu

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