0
votes

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;
        }
    }
}
3

3 Answers

0
votes

This should work

var ropemove:Boolean = true;
var xSpeed = 5;
var once:Boolean=false;


stage.addEventListener(Event.ENTER_FRAME,ropeCode);
stage.addEventListener(KeyboardEvent.KEY_UP,onSpacebarUp);
function onSpacebarUp(e:KeyboardEvent):void
{
    if (e.keyCode == 32)
    {
        if (ropemove==true)
        {
            if(once==false)
            {
                ropemove = false;
                once=true
            }
        }
        if(ropemove==false)
        {
            ropemove==true
        }

    }
    if (rope.x >= stage.stageWidth )
    {
        ropemove = false;
    }
    trace(ropemove)

}
function ropeCode(e:Event):void
{

    if (ropemove == true)
    {
        rope.x +=  xSpeed;
    }
}
0
votes

Two problems I can spot in your code:

1.) Everything is inside your Event.ENTER_FRAME event handler. This means every frame, that code is going to be run: including where you're adding a keyboard event listener. After 1 second, (assuming you are running at 30 fps) onSpacebarUp() will fire 30 times when you press space, and keeps increasing. Probably not a good idea, pretty sure you only want to add this once.

2.) The part where the boolean value will cause your movieclip to move is in a method: dropRope(). But this is not called anywhere, so it is actually not doing anything. Also may not need the event argument (the e:event) part, as you're not using it nor is it being called from an event.

BennettLiam's code should do something closer to what you want, I'm just adding this answer as an explanation for why your code isn't working. In their answer, they've fixed the above problems I mentioned: moved the event listener code for the keyboard outside of the event frame handler loop so it is only added once, and changed the enter frame event handler to call dropRope() o every frame, so that it is doing something.

0
votes
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;
        }
    }
}