Pregunta

hi guys thank you so much for trying to help

Ok so the question is this. I am trying to move a movieclip automatically with

movieClip.x += xspeed ; 

ofcourse this works but the point is i want this to be triggered with keyboard press ..problem is i couldn't a keyboard event which works as a mouse click..it works as long as space bar is pressed but if i release it ..it stops working..i want it to be like onclick it should start moving automatically.

Any ideas? thanks

hi thanks so much for your reply and sorry for the delay. Your code gave me an idea but I tried to write it without classes . It doesnt throw up any errors however it doesnt work either . I must be doing something stupid , please have a look and let me know . //rope coding

var ropey = MovieClip(this.root).boat_mc.rope_mc.fishyrope_mc.hitbox_mc.y ;
    trace(ropey);
    var ropemove:Boolean;


    stage.addEventListener(Event.ENTER_FRAME,ropeCode);

    function ropeCode(e:Event):void
    {
                //detect keyboard spacebar click
                stage.addEventListener(KeyboardEvent.KEY_UP,onSpacebarUp);

                function onSpacebarUp(e:KeyboardEvent):void
                {
                    if (e.keyCode == Keyboard.SPACE)
                    {
                        ropemove = true;
                    } else if(ropey > 600 ) {
                        ropemove = false;
                    }

                }

                    //drop rope if variable = true

                    function dropRope(e:Event):void 
                    {

                        if(ropemove = true) {
                            MovieClip(this.root).boat_mc.rope_mc.y += xSpeed;
                        } else if (ropemove = false) {
                            MovieClip(this.root).boat_mc.rope_mc.y -= xSpeed;
                        }
                    }



    }
¿Fue útil?

Solución

MyObj extends MovieClip (or Sprite). Basically alls that's happening is you should just toggle a variable when you get the KEY_UP (not KEY_DOWN, as that will repeat if the key is held down). Then, every frame, check this variable, and if's it's good, move

Something like:

private var m_shouldMove:Boolean = false;

// constructor
public function MyObj()
{
    // add our listener for when we're added to the stage as we'll be adding events on it
    this.addEventListener( Event.ADDED_TO_STAGE, this._onAddedToStage );
}

private function _onAddedToStage( e:Event ):void
{
    // NOTE: the keyboard listener goes onto the stage
    // you'll also need to remove the events when your object is removed (e.g. REMOVED_FROM_STAGE)
    this.removeEventListener( Event.ADDED_TO_STAGE, this._onAddedToStage );
    this.addEventListener( Event.ENTER_FRAME, this._onEnterFrame );
    this.stage.addEventListener( KeyboardEvent.KEY_UP, this._onKeyUp );
}

private function _onEnterFrame( e:Event ):void
{
    // every frame, if we should move, do so
    if( this.m_shouldMove )
        this.x += this.speed;
}

private function _onKeyUp( e:KeyboardEvent ):void
{
    if( e.keyCode == Keyboard.SPACE )
        this.m_shouldMove = !this.m_shouldMove; // toggle our var
}

Update

I've reworked your code sample, so it should work now:

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

What I changed:

  • Held your rope as a variable to make it easier to access
  • Removed ropey as it's not needed (for your > 600.0 check, you need to recalculate it anyway
  • The keyboard event is now added with the enter frame event (you were adding a new keyboard event every frame
  • The keyboard event listener just toggles the ropeMove var (there's no point checking for > 600.0 here as it means you only check when any other key is pressed)
  • The enter frame event simply moves the rope y
  • In the enter frame event, if our y is too big, we stop moving

What the code is doing:

  • We set up our vars - rope and ropeMove - ropeMove is used to know if we can move the rope or not
  • We add our event listeners - one for the keybard event, to catch the spacebar key, and one enter frame event, so we can move our rope if necessary
  • In the keyboard event, if our key is the spacebar, we toggle our ropeMove variable
  • In the enter frame event, if ropeMove is true, we move our rope
  • If our rope.y is greater than 600, we clamp it to 600, and set ropeMove to false so we stop moving

Update 2

With the addition of a variable ropeDir, the rope will now continuously move up and down (assuming ropeMove is true)

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

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 * ropeDir;

        // stop moving if we've gone too far
        if( rope.y > 600.0 && ropeDir == 1 )
            ropeDir = -1;
        else if( rope.y < 0.0 && ropeDir == -1 )
            ropeDir = 1;
    }
}

Otros consejos

addEventListener(KeyboardEvent.KEY_DOWN, moveStarter);

function moveStarter():void
{
   addEventListener(Event.ENTER_FRAME, startMove);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top