Link in Zwischenablage kopieren
Kopiert
I want a simply function where I press the Enter-Key and then randomly 1 out of 7 imported videos starts playing.
Every video should be invisible first and should go invisible again after playing.
I tried to create it based on information I found, but I have the feeling it is very wrong and there are even simpler ways to create it
What is the best and simpliest Actionscript code to get this effect?
The name of the videos are video1, video2, video3, video4, video5, video6, video7.
Here is just for reference my code (but I guess there is not that much to take out of it):
video1.visible = false;
video2.visible = false;
video3.visible = false;
video4.visible = false;
var SelectMC:Number = 0;
addEventListener(KeyboardEvent.ENTER);
function fl_ClickToHide(event:KeyboardEvent):void
{
function getMC() :void
{
var SelRam:Number = Math.random ();
SelectMC = Math.round (SelRam*4+1);
trace(SelectMC);
}
getMC();
}
stage.addEventListener (Event.ENTER_FRAME,EntFrame);
function EntFrame(e:Event):void
{
}
if (SelectMC == 1)
{
video2.visible = false;
video3.visible = false;
video4.visible = false;
video1.visible = true;
}
if (SelectMC == 2)
{
video1.visible = false;
video3.visible = false;
video4.visible = false;
video2.visible = true;
}
if (SelectMC == 3)
{
video1.visible = false;
video2.visible = false;
video4.visible = false;
video3.visible = true;
}
if (SelectMC == 4)
{
video1.visible = false;
video2.visible = false;
video3.visible = false;
video4.visible = true;
}
}
Link in Zwischenablage kopieren
Kopiert
by video, do you mean a movieclip?
of if you really mean a video, how are your displaying it? in an flvplayback component, added to a timeline, in
Link in Zwischenablage kopieren
Kopiert
Hello kglad,
yes, they are imported external videos with playback component. Each on a different layer.
(By the way: Instead of the enter-key, the space-bar or a left mouse click would also be working if this would be easier).
Link in Zwischenablage kopieren
Kopiert
don't import the videos to the timeline.
add one flvplayback component to your stage>assign an instance name (eg, flv_pb).
then use:
var videoA:Array=['videos/flv1.flv','videos/vid2.mp4',etc];
and whenever you want to play a video call videoF:
function videoF(possible event, eg, keyboardevent):void{
flv_pb.source=videoA[Math.floor(videoA.length*Math.random())]; // if you want to prevent repeats use a shuffle function
}
Link in Zwischenablage kopieren
Kopiert
Thanks for your help kglad,
I tried it right now and used the code:
var videoA:Array=['AllVideos/video1.mp4','AllVideos/video2.mp4'];
function videoF(event:MouseEvent):void{
flv_pb.source=videoA[Math.floor(videoA.length*Math.random())]; // if you want to prevent repeats use a shuffle function
}
"AllVideos" is a folder in the same directory where the .fla document is. Is that the right way to align that folder with the FLVPlayback Component?
And how does the shuffle function look like? Where do I have to add this?
Maybe these are very basic questions but I'm kind of new to Adobe Animate and Actionscript, sorry for that.
Link in Zwischenablage kopieren
Kopiert
you would use:
var videoA:Array=['AllVideos/video1.mp4','AllVideos/video2.mp4'];
shuffle(videoA);
var index:int=0;
function videoF(possible event, eg, keyboardevent):void{
flv_pb.source=videoA[index];
index++;
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Link in Zwischenablage kopieren
Kopiert
I used your code and tried:
function videoF(event:MouseEvent):void{
I also tried:
function videoF(KeyboardEvent.SPACE):void{
But both versions haven't worked. Is something missing in this code or did I missed something somewhere else?
What I have done in the project right now is:
1. Pulled a FLVPlayback component into my Scene
2. Named it "flv_pb"
3. Added the action to the same layer
4. Changed the KeyboardEvent/MouseEvent
Link in Zwischenablage kopieren
Kopiert
did you assign a keyboard listener?
Link in Zwischenablage kopieren
Kopiert
This is the full code I have right now:
var videoA:Array=['AllVideos/video1.mp4','AllVideos/video2.mp4','AllVideos/video3.mp4','AllVideos/video4.mp4','AllVideos/video5.mp4','AllVideos/video6.mp4','AllVideos/video7.mp4','AllVideos/video8.mp4'];
shuffle(videoA);
var index:int=0;
addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown)
function reportKeyDown(event:KeyboardEvent):void{
flv_pb.source=videoA[index];
index++;
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Link in Zwischenablage kopieren
Kopiert
assuming your stage has focus, use:
var videoA:Array=['AllVideos/video1.mp4','AllVideos/video2.mp4','AllVideos/video3.mp4','AllVideos/video4.mp4','AllVideos/video5.mp4','AllVideos/video6.mp4','AllVideos/video7.mp4','AllVideos/video8.mp4'];
shuffle(videoA);
var index:int=0;
stage.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown)
function reportKeyDown(event:KeyboardEvent):void{
if(index<videoA.length){
flv_pb.source=videoA[index];
index++;
} else {
// do whatever
}
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Link in Zwischenablage kopieren
Kopiert
If you don't mind, I'd like to suggest an alternative using frames to store the different videos.
All you have to do is to:
- Create a container Movie Clip called videos.
- Inside of this container, put each imported FLVPlayback component on each frame, starting from 2 and leaving the first one empty.
- Then add this code to the main timeline:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import fl.video.VideoEvent;
var videosFrames:Array = getRandomFrames(videos.totalFrames - 1);
var count:uint = 0;
var canChange:Boolean = true;
function keyDownHandler(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.ENTER)
if (canChange)
changeVideo();
}
function changeVideo():void
{
var index:uint = 2 + count % (videos.totalFrames - 1);
canChange = false;
if (index == 2)
{
videosFrames.sort(randomSort);
count = 0;
}
videos.gotoAndStop(videosFrames[count]);
videos["video" + (videos.currentFrame - 1)].addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
count++;
}
function completeHandler(e:fl.video.VideoEvent):void
{
e.currentTarget.removeEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
videos.gotoAndStop(1);
canChange = true;
}
function getRandomFrames(total:uint):Array
{
var array:Array = [];
for (var i:uint = 0; i < total; i++)
array = i + 2;
return array;
}
function randomSort(a:*, b:*):Number
{
if (Math.random() < 0.5)
return -1;
else
return 1;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
FLA/SWF/videos download: here.
I hope it helps.
Regards,
JC
Weitere Inspirationen, Events und Ressourcen finden Sie in der neuen Adobe Community
Jetzt ansehen