如何在sprite中更改movieclip的宽度

时间:2014-06-12 16:57:25

标签: actionscript-3

我真的需要你的帮助......下面是我的mp3播放器代码。令人讨厌的问题是在updateTime函数内部,test.width并不想从0改变它的值,这种非常奇怪的行为让我无法理解为什么......

import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.SoundLoaderContext;
import flash.display.Sprite;
import flash.events.ProgressEvent;



var playlist:XML = new XML();                    
var loader:URLLoader = new URLLoader();
var songNamok:String = new String();
var songPathok:String = new String();

var arrayUrl:Array = new Array(); 
var arrayNames:Array = new Array();
var track:Number = 0;

var myMusic:Sound = new Sound();
var soundFile:URLRequest = new URLRequest();
var channel:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
var pressedButton:Object = new Object();
var tf:TextFormat = new TextFormat(); 
var IsPausePressed:Number = 0;


tf.color = 0xFF0000;
txtLengthOfSong.setStyle("textFormat", tf);

var tfforSong:TextFormat = new TextFormat(); 

tfforSong.font = "Arial"; 
tfforSong.size = 16; 
tfforSong.bold = true;
tfforSong.color = 0xFFffff;




movingString.setStyle("textFormat", tfforSong);










//Create test bar (progressBar) in this place, all childs are drawn objects, 
//they have width and height

var container:Sprite = new Sprite();
addChild(container);
container.addChild(emptyProgress);
container.addChild(loadedProgress);
container.addChild(test);
container.addChild(transparentProgress);




container.setChildIndex(emptyProgress,0);
container.setChildIndex(loadedProgress,1);
container.setChildIndex(test, 2);
container.setChildIndex(transparentProgress,3);





var secondContainer:Sprite = new Sprite();
addChild(secondContainer);
secondContainer.addChild(emptyVolume);
secondContainer.addChild(filledVolume);
secondContainer.addChild(transparentVolume);





//In accordance to this timer test (progress bar) and
// remaining time will be changed
var myTimer:Timer = new Timer(50);

var myTimerForTremble = new Timer(5);
var songPosition:Number = 0;
var myContext:SoundLoaderContext = new SoundLoaderContext(5000);




var firstTime = 0;


buttonPlay.addEventListener (MouseEvent.CLICK, playMusic);
buttonStop.addEventListener (MouseEvent.CLICK, stopMusic);
buttonPause.addEventListener (MouseEvent.CLICK, pauseMusic);
buttonPlay.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
buttonStop.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
buttonPause.addEventListener (MouseEvent.MOUSE_OVER, startTimer);
transparentProgress.addEventListener(MouseEvent.CLICK, changePosition);
transparentVolume.addEventListener(MouseEvent.CLICK, changeVolume);



myTimer.addEventListener(TimerEvent.TIMER, updateTime);


myTimerForTremble.addEventListener (TimerEvent.TIMER, tremble);
buttonPlay.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);
buttonStop.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);
buttonPause.addEventListener (MouseEvent.MOUSE_OUT, stopTimer);








filledVolume.width = (channel.soundTransform.volume * 10)*0.98;





function progressOfDownloading (event:ProgressEvent):void{

     var loadedMp3:Number = Math.round(100 * (event.bytesLoaded / event.bytesTotal)); 

    loadedProgress.width = 35;




}


function songIsOver (evt:Event):void{

    evt.currentTarget.removeEventListener(evt.type, songIsOver);



    myTimer.stop();


firstTime = 0;


songPosition = 0;


try {                                             
         channel.stop();
         myMusic.close();

    } catch(e:Error){
        channel.stop();      

    }



ExternalInterface.call("PlayNextSong","");


}


function playTheMusic ():void{

    myTimer.stop();


firstTime = 0;


if (IsPausePressed == 0)
songPosition = 0;


try {                                            
         channel.stop();
         myMusic.close();


    } catch(e:Error){
        channel.stop();  

    }




playMusic();




}


function changeVolume(evt:MouseEvent):void{
    var x:Number = new Number();

    x = evt.stageX;

    var percentOfVolume:Number = new Number();


    percentOfVolume = Math.floor((x-40)/0.98);

    percentOfVolume = percentOfVolume / 10;

    sTransform.volume = percentOfVolume;



    channel.soundTransform = sTransform;



    filledVolume.width = x - 40;


}


function changePosition (evt:MouseEvent):void{

    if (firstTime == 1)
    {

    var x:Number = new Number();
    x = evt.stageX;
    var percent:Number = new Number();

 var totalSeconds:Number = myMusic.length;
 percent =  myMusic.length/100 * ((x-22)/1.56);
    songPosition = percent;

    channel.stop();

    channel =  myMusic.play(songPosition);

    channel.addEventListener(Event.SOUND_COMPLETE, songIsOver);

    }
}

//This function will launch every 50 milliseconds, because of myTimer


function updateTime(evt:TimerEvent):void{



        var progressok:String = new String(channel.position / myMusic.length);

        var progressPercent:Number = new Number (channel.position / myMusic.length);




       progressPercent =  progressPercent * 100;

    //this is the main problem: test.width doesn't want to change at all, however
    // if I will write solid value like: 10, 12, 13 it works, and also when I write
    // test.width = Math.random() * 100; it also works...
       test.width =  Math.round(progressPercent * 1.56);












    var currentPosition:Number = new Number();
    currentPosition = channel.position;

    var timeRemain:Number = new Number();
    timeRemain = myMusic.length - channel.position;

    var totalSeconds:Number = timeRemain/1000;

    var minutes:Number = Math.floor(totalSeconds/60);
    var seconds = Math.floor(totalSeconds)%60;
    if (seconds<10) {
    seconds = "0"+seconds;
    }


 txtLengthOfSong.text = minutes+":"+seconds;

    var randomHeight:Number = new Number();

    randomHeight = Math.random()*100;



    if (randomHeight > 55)
    randomHeight = 55;
    if (randomHeight < 0)
    randomHeight = 0;

    column1.height = randomHeight;
    column2.height = randomHeight - (Math.random()*100);
    column3.height = randomHeight / 3 * 2;
    column4.height = randomHeight - (Math.random()*100);
    column5.height = randomHeight - (Math.random()*100);
    column6.height = randomHeight - (Math.random()*100);
    column7.height = randomHeight - (Math.random()*100);
    column8.height = randomHeight - (Math.random()*100);
    column9.height = randomHeight - (Math.random()*100);
    column10.height = randomHeight - (Math.random()*100);



if (movingString.textWidth > 150)
{
    var deletedChar:String = new String();
    var movingText:String = new String();


    movingText = movingString.text;

    deletedChar = movingText.charAt(0);

    movingText = movingText.substr(1) + deletedChar;

    movingString.text = movingText;




}




}







function stopTimer(evt:MouseEvent):void{

    pressedButton.y = 145;
    myTimerForTremble.stop();

}

function tremble(evt:TimerEvent):void{



        pressedButton.y = 145 + ((Math.random()*10) - 5);







}

function startTimer (evt:MouseEvent):void{

    if (!myTimerForTremble.running)

    pressedButton = evt.target;

    myTimerForTremble.start();
}


function pauseMusic(evt:MouseEvent = null):void{
IsPausePressed = 1;
    songPosition = channel.position;

    pressedButton.y = 145;
    myTimerForTremble.stop();
    channel.stop();
    firstTime = 0;
   myTimer.stop();


}

ExternalInterface.addCallback('pauseTheMusic', pauseMusic);
ExternalInterface.addCallback('resetSongPosition', resetSongPosition);

function resetSongPosition ():void{

    songPosition = 0;


}


ExternalInterface.addCallback('select_track', SelectTrack);
function SelectTrack(songPath:String, songName:String):void{

    songPathok = songPath;
    songNamok = songName;

     playTheMusic();

}




function playMusic (evt:MouseEvent = null):void{
    if (firstTime == 0)
    firstTime = 1;
    else
    return;

    soundFile.url = songPathok;

    movingString.text = songNamok + "       ";



     var nextTitle:Sound = new Sound(soundFile);
    myMusic = nextTitle;



    channel =  myMusic.play(songPosition);

    channel.addEventListener(Event.SOUND_COMPLETE, songIsOver);

    pressedButton.y = 145;
    myTimerForTremble.stop();




    myTimer.start();


    var percentOfVolume:Number = new Number();


    percentOfVolume = Math.floor(filledVolume.width/0.98);

    percentOfVolume = percentOfVolume / 10;

    sTransform.volume = percentOfVolume;



    channel.soundTransform = sTransform;








}

function stopMusic (evt:MouseEvent):void{
    firstTime = 0;


    pressedButton.y = 145;
    myTimerForTremble.stop();
    channel.stop();
    songPosition = 0;
myTimer.stop();


}

0 个答案:

没有答案