Skip to main content
Known Participant
November 9, 2009
Answered

Random swf/f4v load

  • November 9, 2009
  • 1 reply
  • 1255 views

Hey I have this problem - with help from Kglad in a previous post plus a look on another post I have the underneath action script.

What I really need it to do, is to load a random movie from my array, and when it replay the movie it has to be the same movie as which was randomly loaded... So far I have this code:

var movies:Array =["film/film1.f4v", "film/film2.f4v"]

function shuffle(a:Array) {

for (var ivar = a.length-1; ivar>=0; ivar--) {

var p = Math.floor(Math.random())(ivar+1);

var t = a[ivar];

a[ivar] = a

;

a

= t;

  }

};

var timedelay:Number = 10;  // seconds delay in replay

var video;

var nc:NetConnection;

var ns:NetStream;

nc = new NetConnection();

nc.connect(null);

ns = new NetStream(nc);

ns.client = this;

ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusf);

function netStatusf(e:NetStatusEvent) {

    if (e.info.code == "NetStream.Play.Stop" && Math.abs(durationNum-ns.time)<.1) {

setTimeout(replayF,timedelay*1000);

    }

}

function replayF(){

ns.play(shuffle(movies));

}

var durationNum:Number;

function onMetaData(iObj:Object):void {

    durationNum = iObj.duration;

}

video = new Video(287,263);

video.x = 231.1;

video.y = 140.5;

addChild(video);

video.attachNetStream(ns);

ns.play(shuffle(movies));

but doesn't seem to work it comes with this error:

TypeError: Error #1006: value is not a function.

at cv/shuffle()

at cv/frame3()

at flash.display::MovieClip/gotoAndStop()

at cv/index()

Hope someone can help...

This topic has been closed for replies.
Correct answer kglad
use:

var moviesA:Array =["film/film1.f4v", "film/film2.f4v"];

var movie:String = moviesA[Math.floor(moviesA.length)];

var timedelay:Number = 10;  // seconds delay in replay

var video;

var nc:NetConnection;

var ns:NetStream;

nc = new NetConnection();

nc.connect(null);

ns = new NetStream(nc);

ns.client = this;

ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusf);

function netStatusf(e:NetStatusEvent) {

    if (e.info.code == "NetStream.Play.Stop" && Math.abs(durationNum-ns.time)<.1) {

setTimeout(replayF,timedelay*1000);

    }

}

function replayF(){

ns.play(movie);

}

var durationNum:Number;

function onMetaData(iObj:Object):void {

    durationNum = iObj.duration;

}

video = new Video(287,263);

video.x = 231.1;

video.y = 140.5;

addChild(video);

video.attachNetStream(ns);

ns.play(movie);

p.s.  when you do want to randomize an array and loop through it without repeats, the shuffle function should  used once.  then iterate your "shuffled" arrays elements from index 0 to the last element.


oops.  my error:

use:

var moviesA:Array =["film/film1.f4v", "film/film2.f4v"];

var movie:String = moviesA[Math.floor(Math.random()*moviesA.length)];

var timedelay:Number = 10;  // seconds delay in replay

var video;

var nc:NetConnection;

var ns:NetStream;

nc = new NetConnection();

nc.connect(null);

ns = new NetStream(nc);

ns.client = this;

ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusf);

function netStatusf(e:NetStatusEvent) {

    if (e.info.code == "NetStream.Play.Stop" && Math.abs(durationNum-ns.time)<.1) {

setTimeout(replayF,timedelay*1000);

    }

}

function replayF(){

ns.play(movie);

}

var durationNum:Number;

function onMetaData(iObj:Object):void {

    durationNum = iObj.duration;

}

video = new Video(287,263);

video.x = 231.1;

video.y = 140.5;

addChild(video);

video.attachNetStream(ns);

ns.play(movie);

p.s.  when you do want to randomize an array and loop through it without repeats, the shuffle function should  used once.  then iterate your "shuffled" arrays elements from index 0 to the last element.

1 reply

Inspiring
November 10, 2009

var p = Math.floor(Math.random())(ivar+1);

should be
var p = Math.floor(Math.random())*(ivar+1);
or something.
Math.random() is a number, (ivar+1) is a number...
think var p = Math.floor(5)(5);
what're the 2 5's supposed to do?
HTH,
-Ted

PS, i think the next gotchya is going to be that the function doesn't return a URL, but im not sure...

function shuffle(a:Array):String{

     for (var ivar = a.length-1; ivar>=0; ivar--) {

          var p = Math.floor(Math.random())*(ivar+1);

          var t = a[ivar];

          a[ivar] = a

;

          a

= t;

          var url = "url" // couldnt tell which one of your vars is setting the curent url

     }

     return url;

}                             

that way, play(shuffle(movies)) translates to play(url);

kimtragerAuthor
Known Participant
November 10, 2009

Nothing really happens - dosen't come with any error message, but doesn't play any movies...

So don't know if that means that it dosen't return any URL..?

var movies:Array =["film/film1.f4v", "film/film2.f4v"]

function shuffle(a:Array):String{

     for (var ivar = a.length-1; ivar>=0; ivar--) {

          var p = Math.floor(Math.random())*(ivar+1);

          var t = a[ivar];

          a[ivar] = a

;

          a

= t;

          var url = "url"

     }

     return url;

}                            

var timedelay:Number = 10;  // seconds delay in replay

var video;

var nc:NetConnection;

var ns:NetStream;

nc = new NetConnection();

nc.connect(null);

ns = new NetStream(nc);

ns.client = this;

ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusf);

function netStatusf(e:NetStatusEvent) {

    if (e.info.code == "NetStream.Play.Stop" && Math.abs(durationNum-ns.time)<.1) {

setTimeout(replayF,timedelay*1000);

    }

}

function replayF(){

ns.play(shuffle(movies));

}

var durationNum:Number;

function onMetaData(iObj:Object):void {

    durationNum = iObj.duration;

}

video = new Video(287,263);

video.x = 231.1;

video.y = 140.5;

addChild(video);

video.attachNetStream(ns);

ns.play(shuffle(movies));