首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netstream()循环问题

Netstream()循环问题
EN

Stack Overflow用户
提问于 2009-03-30 16:01:42
回答 1查看 1.1K关注 0票数 0

我正在预先加载4个flv影片并隐藏它们,当我滚动videobutton影片剪辑时,我希望flv视频淡入并开始播放。我有这个可以工作的代码,但我觉得它写得很糟糕。

代码语言:javascript
复制
var videos:Array = new Array(
'ltp_video-low1.flv',
'ltp_video-low1.flv',
'ltp_video-low1.flv',
'ltp_video-low1.flv'
);

function videoOver(buttonMC,video,stream) {
    buttonMC.onRollOver = function() {
    stream.pause(false);
    video.attachVideo(stream);
    fadeIn(video);
    };
}

function videoOut(buttonMC,video,stream) {
    buttonMC.onRollOut = function() {
    fadeOut(video);
    stream.pause();
    };
}

for (var i:Number=1; i<=4; i++) {
    this['connection'+i] = new NetConnection();
    this['connection'+i].connect(null);
    this['stream'+i] = new NetStream(this['connection'+i]);
    this['stream'+i].play(videos[i-1]);
    videoOver(this['videobutton'+i],this['video'+i],this['stream'+i]);
    videoOut(this['videobutton'+i],this['video'+i],this['stream'+i]);
}

相反,我想要这样的东西:但这不起作用,我想是attachVideo()出了问题。

代码语言:javascript
复制
for (var i:Number = 1; i<=4; i++) {
    this['connection'+i] = new NetConnection();
    this['connection'+i].connect(null);
    this['stream'+i] = new NetStream(this['connection'+i]);
    this['stream'+i].play('ltp_video-low1.flv');
    this['videobutton'+i].i = i;
    this['videobutton'+i].onRollOver = function() {
        this['stream'+this.i].pause(false);
        this.attachVideo(['stream'+this.i]);
        fadeIn(['video'+this.i]);
    };
    this['videobutton'+i].onRollOut = function() {
        this['stream'+this.i].pause();
        this.attachVideo(['stream'+this.i]);
        fadeOut(['video'+this.i]);
    };
}

下面是所有的代码:

代码语言:javascript
复制
// Import TweenLite
import gs.*;
import gs.easing.*;

// Creates the fade functions
function fadeIn(video) {
    TweenLite.to(video,0.5,{_alpha:100, ease:Regular.easeOut});
}
function fadeOut(video) {
    TweenLite.to(video,0.5,{_alpha:0, ease:Regular.easeOut});
}

// Parses the Flashvars into arrays
var titles:Array = (_level0.titleVars) ? _level0.titleVars.split(',') : [];
var urls:Array = (_level0.urlVars) ? _level0.urlVars.split(',') : [];

// Sets the mouse action
function SetMouseAction(indexNumber, buttonMC, arrowMC, dynamicTF, linkURL):Void {
    buttonMC.colorText = dynamicTF;
    buttonMC.onRollOver = function() {
        TweenLite.to(arrowMC,0.2,{_x:"2", _alpha:80, ease:Back.easeOut, tint:0x7cb0b7});
        this.colorText.textColor = 0x7cb0b7;
        // Fixes the Flash bug with button over each other
        if (indexNumber == 1 || indexNumber == 2 || indexNumber == 3) {
            stream1.pause(false);
            fadeIn(video1);
        }
        if (indexNumber == 4 || indexNumber == 5 || indexNumber == 6) {
            stream2.pause(false);
            fadeIn(video2);
        }
        if (indexNumber == 7 || indexNumber == 8 || indexNumber == 9) {
            stream3.pause(false);
            fadeIn(video3);
        }
        if (indexNumber == 10 || indexNumber == 11 || indexNumber == 12) {
            stream4.pause(false);
            fadeIn(video4);
        }
    };
    buttonMC.onRollOut = function() {
        TweenLite.to(arrowMC,0.2,{_x:37, _alpha:100, ease:Back.easeOut, tint:0xFFFFFF});
        this.colorText.textColor = 0xffffff;
    };
    buttonMC.onRelease = function() {
        if (linkURL) {
            getURL(linkURL);
        }
    };
}

// Loops trough all the MC
for (var i:Number = 1; i<=12; i++) {
    SetMouseAction(i,this["link"+i],this["arrow"+i],this["text"+i],urls[i-1]);
    this["text"+i].text = titles[i-1];
}





var videos:Array = new Array('ltp_video-low1.flv', 'ltp_video-low1.flv', 'ltp_video-low1.flv', 'ltp_video-low1.flv');

function videoOver(buttonMC, video, stream) {
    buttonMC.onRollOver = function() {
        stream.pause(false);
        video.attachVideo(stream);
        fadeIn(video);
    };
}

function videoOut(buttonMC, video, stream) {
    buttonMC.onRollOut = function() {
        fadeOut(video);
        stream.pause();
    };
}

for (var i:Number = 1; i<=4; i++) {
    this['connection'+i] = new NetConnection();
    this['connection'+i].connect(null);
    this['stream'+i] = new NetStream(this['connection'+i]);
    this['stream'+i].play(videos[i-1]);
    videoOver(this['videobutton'+i],this['video'+i],this['stream'+i]);
    videoOut(this['videobutton'+i],this['video'+i],this['stream'+i]);
}



/*
for (var i:Number = 1; i<=4; i++) {
    this['connection'+i] = new NetConnection();
    this['connection'+i].connect(null);
    this['stream'+i] = new NetStream(this['connection'+i]);
    this['stream'+i].play('ltp_video-low1.flv');
    this['videobutton'+i].i = i;
    this['videobutton'+i].onRollOver = function() {
        this['stream'+this.i].pause(false);
        this.attachVideo(['stream'+this.i]);
        fadeIn(['video'+this.i]);
    };
    this['videobutton'+i].onRollOut = function() {
        this['stream'+this.i].pause();
        this.attachVideo(['stream'+this.i]);
        fadeOut(['video'+this.i]);
    };
}
*/
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-03-30 18:11:43

在你的原始代码中,你有:

代码语言:javascript
复制
function videoOver(buttonMC,video,stream) {
  buttonMC.onRollOver = function() {
    stream.pause(false);
    video.attachVideo(stream);
    fadeIn(video);
  };
}

videoOver(this['videobutton'+i],this['video'+i],this['stream'+i]);

在这种情况下,对视频对象调用attachVideo。

我在您的代码中看不到在哪里声明了视频对象,但我将假定它在那里。

在第二段代码中,您将执行以下操作:

代码语言:javascript
复制
this['videobutton'+i].onRollOver = function() {
  this['stream'+this.i].pause(false);
  this.attachVideo(['stream'+this.i]);
  fadeIn(['video'+this.i]);
};

因此,对videobutton对象调用attachVideo,而不是对视频对象调用。

此外,我认为对流的引用不会起作用,因为您的this变量将是videobutton,据我所知,它没有定义该值(函数() {}内的" this“引用与函数外的this引用不同)。

我认为对fadeIn的调用也可能不能正常工作,因为我认为作用域将会变得混乱。

这里有一些可能行得通的方法。我根本没有测试过它,但希望您能明白我在尝试什么。

代码语言:javascript
复制
for (var i:Number = 1; i<=4; i++) {
  this['connection'+i] = new NetConnection();
  this['connection'+i].connect(null);
  this['stream'+i] = new NetStream(this['connection'+i]);
  this['stream'+i].play('ltp_video-low1.flv');
  this['videobutton'+i].i = i;
  setHandlers(i);
}

function onVideoButtonRollOver(i:Number):Void {
  this['stream' + i].pause(false);
  this.attachVideo(this['stream' + i]);
  this.fadeIn(this['video' + i]);
}

function onVideoButtonRollOut(i:Number):Void {
  this['stream' + i].pause();
  fadeOut(this['video' + i]);
}

function setHandlers(i:Number):Function {
  this['videobutton'+i].scope = this;
  // This essentially fixes the scope issue.  The value of i is retained
  // properly due to the property of closures.
  this['videobutton'+i].onRollOver = function() {
    this.scope.onVideoButtonRollOver.apply(this.scope, [i]);
  }
  this['videobutton'+i].onRollOut = function() {
    this.scope.onVideoButtonRollOut.apply(this.scope, [i]);
  }
}

尽管我对此并不是很满意。我认为代码可能应该被重构,这样你的每个组都有一个单一的对象,有像"stream","connection“等成员。我认为这会让事情变得更简洁,因为你不必不断地引用索引。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/697827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档