1

I'm here again for your help :{ I have a question which I tried to google it but can't find the answer. Well,..May be there is an answer but I just can't make it works? I'm in a process of learning AS3 so let's say I'm still new here.

What I'm doing is making a keyboatd to respond with the vdo files I have. It is a very simple idea as press-n-play. Each keys has their vdos to play and if you press another button while the first one is still pressing, it'll play another vdo of its key. I have make this as boolean with function of keydown and keyup like this:

import flash.events.Event;
 import flash.events.KeyboardEvent;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;

var isLeft:Boolean = false;
var isRight:Boolean = false;
    var video;
var nc;
var ns;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUP);
this.addEventListener(Event.ENTER_FRAME,playVid);

                nc = new NetConnection();
                nc.connect(null);
                ns = new NetStream(nc);
                ns.client = this;
                video = new Video(550,400);
                addChild(video);
                video.attachNetStream(ns);


function onDown(e:KeyboardEvent):void
{
      switch (e.keyCode)
      {
                case 37 :
                          //ns.play(TomAndJerry.flv);
                          isLeft=true;
                          break;
                case 39 :
                          //ns.play(westler.flv);
                          isRight = true;
                          break;
      }

}

function onUP(e:KeyboardEvent):void
{
      switch (e.keyCode)
      {
                case 37 :
                          isLeft = false;
                          break;
                case 39 :
                          isRight = false;
                          break;
      }
}

 function playVid(e:Event):void
{
      if (isLeft)
      {
                trace(kk);
                ns.play(westler.flv);
                isLeft = false;
      }
      else if (isRight)
      {
                trace(PP);
                ns.play(TomAndJerry.flv);
                //isRight = false;
      }

}

I have tried making a keydown function without using any boolean or those true of false things to just play a vdo. It worked but, I still have the same problem that I can't find a solution which is ....

When you hold down the keyboard button the vdo will keep start at the beginning.

All I want is to play the vdo even the key is press down. If the vdo ends then play again as loop but if the key is up the vdo will play until it ends. And if there are more than one button are holding down just play the vdo of the lastest pressed button.

T-T" Thanks.

Ps. I have tried removeEventListener but, it made every buttons' function gone.

TeeTee
  • 65
  • 2
  • 11

1 Answers1

0

your playvid function is called each frame so i think it's normal your video don't start,
I think you could try to change your code as follow:

// add net status handler event to check the end of the video
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// remove this line    
//this.addEventListener(Event.ENTER_FRAME,playVid);

/** a key is pressed **/
function onDown(e:KeyboardEvent):void
{
    switch (e.keyCode)
    {
            case Keyboard.LEFT:
                      // start the video just if the video don't play
                      if(!isLeft) ns.play("TomAndJerry.flv");
                      // video left is playing
                      isLeft = true;
                      // video right isn't playing
                      isRight = false;
                      break;

            case Keyboard.RIGHT:
                      // start the video just if the video don't play
                      if(!isRight) ns.play("westler.flv");
                      // video rightis playing
                      isRight = true;
                      // video left isn't playing
                      isLeft = false;
                      break;
    }
}

/** a key is released **/
function onUP(e:KeyboardEvent):void
{
    switch (e.keyCode)
    {
            case Keyboard.LEFT:
                      isLeft = false;
                      break;
            case Keyboard.RIGHT:
                      isRight = false;
                      break;
    }
}

/** net status change, verify if we reach the end of the video **/
function netStatusHandler(e:NetStatusEvent):void
{
    // when netStatus code is NetStream.Play.Stop the video is complete
    if (e.info.code == "NetStream.Play.Stop") 
    {
        // right key is still pressed we loop the video
        if( isRight )      ns.play("westler.flv");
        // left key is still pressed we loop the video
        else if( isLeft )  ns.play("TomAndJerry.flv");
    }
}

I hope this will help you :)

Benjamin BOUFFIER
  • 946
  • 1
  • 5
  • 7