Search code examples
actionscript-3flashactionscriptflash-cs5flash-cs4

as3 space bar function not workingf


I have written a code to move a MovieClip on pressing space bar. So if someone presses space bar ..it activates a boolean variable from false to true and if its true the object moves ..but its not working. can some one please help. Thank you

     var rope = MovieClip(this.root).boat_mc.rope_mc.fishyrope_mc.hitbox_mc;
var ropeMove:Boolean = false;

stage.addEventListener(Event.ENTER_FRAME, ropeCode);
stage.addEventListener(KeyboardEvent.KEY_UP, onSpacebarUp);

function onSpacebarUp(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.SPACE)
        ropeMove = !ropeMove; // toggles ropeMove (i.e. if it's true, sets it to false, and vice versa)
}

function ropeCode(e:Event):void
{
    // move the rope
    if( ropeMove )
    {
        rope.y += xSpeed;

        // stop moving if we've gone too far
        if( rope.y > 600.0 )
        {
            rope.y = 600.0;
            ropeMove = false;
        }
    }
}

Solution

  • var rope = MovieClip(this.root).boat_mc.rope_mc.fishyrope_mc.hitbox_mc;
    var ropeMove:Boolean = false;
    
    stage.addEventListener(Event.ENTER_FRAME, ropeCode);
    stage.addEventListener(KeyboardEvent.KEY_UP, onSpacebarUp);
    
    function onSpacebarUp(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.SPACE)
            ropeMove = !ropeMove; // toggles ropeMove (i.e. if it's true, sets it to false, and vice versa)
    }
    
    function ropeCode(e:Event):void
    {
        // move the rope
        if( ropeMove )
        {
            rope.y += xSpeed;
    
            // stop moving if we've gone too far
            if( rope.y > 600.0 )
            {
                rope.y = 600.0;
                ropeMove = false;
            }
        }
    }