Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

package { import flash.display.Sprite; import flash.display.Shape; import…

  1. User

    package { import flash.display.Sprite; import flash.display.Shape; import flash.events.Event; import zip.ZIPResourceLoader; import flash.events.ProgressEvent;

    public class ProgressBar extends Sprite
    {
        private var progressBar:Shape;
        private var backgroundBar:Shape;
        public var progress:Number;
        private var maxWidth:Number;
        private var maxHeight:Number;
        public var resourceLoader:ZIPResourceLoader;
        private var bytesLoaded:Number;
        private var bytesTotal:Number;
    
        public function ProgressBar()
        {
            init();
        }
    
        public function setResourceLoader(resourceLoader:ZIPResourceLoader):void 
    	{
            resourceLoader.addEventListener(ProgressEvent.PROGRESS, onProgressUpdate);
        }
    
        public function onProgressUpdate(event:ProgressEvent):void 
    	{
            progress = event.bytesLoaded / event.bytesTotal;
            updateProgressBar();
        }
    
        private function init():void
        {
            resourceLoader = new ZIPResourceLoader(this);
            maxWidth = 700;
            maxHeight = 28;
            progress = 0;
    
            backgroundBar = new Shape();
            backgroundBar.graphics.beginFill(0x014ca3);
    		 var cornerRadius:Number = 10;
            backgroundBar.graphics.drawRoundRect(0, 0, maxWidth, maxHeight, cornerRadius);
    		        
            backgroundBar.graphics.endFill();
    
            progressBar = new Shape();
    
            addChild(backgroundBar);
            addChild(progressBar);
    
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }
    
        private function onAddedToStage(event:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(Event.ENTER_FRAME, onUpdate);
        }
    
        private function onRemovedFromStage(event:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
            removeEventListener(Event.ENTER_FRAME, onUpdate);
        }
    
        private function onUpdate(event:Event):void
        {
            progress += 0.005; // Увеличьте прогресс на 0.01 каждый фрейм
            if (progress > 1) progress = 1; // Убедитесь, что прогресс не превышает 1
            updateProgressBar();
        }
    
    
        public function updateProgressBar():void
        {
            progressBar.graphics.clear();
            progressBar.graphics.beginFill(0x348aee);
            var cornerRadius:Number = 3;
            var yOffset:Number = -2; // смещение по вертикали на 2 пикселя
            progressBar.graphics.drawRoundRect(0, yOffset, maxWidth * progress, maxHeight + 4, cornerRadius);
    
            progressBar.graphics.endFill();
        }
    
        public function setProgress(value:Number, maxValue:Number = 1):void 
    	{
            progress = value / maxValue;
            if (progress < 0) 
    		{
                progress = 0;
            } 
    		else if (progress > 1) 
    		{
                progress = 1;
            }
            updateProgressBar();
        }
    }
    

    } как сделать чтобы когда все version.txt локальный и на хосте одинаковые, то полоса прогресс бара скрывалась, вот код загрузчика package zip { import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.EventDispatcher; import flash.events.ProgressEvent; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.net.URLRequest; import flash.net.URLLoaderDataFormat; import flash.net.URLRequestMethod; import flash.net.URLLoader; import flash.net.URLStream; import flash.net.URLVariables; import flash.utils.ByteArray; import deng.fzip.FZip; import deng.fzip.FZipFile;

    public class ZIPResourceLoader extends EventDispatcher
    {
        public var resourcesURL:String = "https://redagereborn.ru/resources.zip";
        public var versionURL:String = "https://redagereborn.ru/version.txt";
        public var localFilePath:String = File.applicationStorageDirectory.nativePath + File.separator + "resources.zip";
        public var versionFile:File = new File(File.applicationStorageDirectory.nativePath + File.separator + "version.txt");
        public var zipLoader:URLLoader = new URLLoader();
        private var progressBar:ProgressBar;
    	public static const PROGRESS_UPDATE:String = "progressUpdate";
    	
        public function ZIPResourceLoader(progressBar:ProgressBar)
        {
    		this.progressBar = progressBar;
            zipLoader.dataFormat = URLLoaderDataFormat.TEXT;
            zipLoader.addEventListener(Event.COMPLETE, onVersionLoaded);
            zipLoader.addEventListener(IOErrorEvent.IO_ERROR, onVersionLoadError);
            zipLoader.load(new URLRequest(versionURL));
        }
    
        public function onVersionLoaded(event:Event):void
        {
            var remoteVersion:Number = Number(zipLoader.data);
    
            var versionLoader:URLLoader = new URLLoader();
            versionLoader.dataFormat = URLLoaderDataFormat.TEXT;
            versionLoader.addEventListener(Event.COMPLETE, onLocalVersionLoaded);
            versionLoader.addEventListener(IOErrorEvent.IO_ERROR, onLocalVersionLoadError);
            versionLoader.load(new URLRequest(versionFile.nativePath));
    
            function onLocalVersionLoaded(event:Event):void {
                var localVersion:Number = Number(versionLoader.data);
                if (localVersion != remoteVersion) {
    		        startDownloadProcess();
                } else {
                    Alert.showMessage("Local version is up to date");
                    // Пропущен код для распаковки архива
                }
            }
    		
            function onLocalVersionLoadError(event:IOErrorEvent):void {
                // Создаем новый файл version.txt и записываем в него пустую строку
                var fileStream:FileStream = new FileStream();
                fileStream.open(versionFile, FileMode.WRITE);
                fileStream.writeUTFBytes("");
                fileStream.close();
                // Запускаем процесс загрузки и распаковки архива
                startDownloadProcess();
            }
        }
    	
    	public function onDownloadProgress(event:ProgressEvent):void 
    	{
            progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
        } 
    
        private function startDownloadProcess():void 
    	{
            Alert.showMessage("Downloading resources.zip");
            var downloadStream:URLStream = new URLStream();
    		downloadStream.addEventListener(ProgressEvent.PROGRESS, onDownloadProgress);
            downloadStream.addEventListener(Event.COMPLETE, onDownloadComplete);
            downloadStream.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
            downloadStream.load(new URLRequest(resourcesURL));
        }
    	
        public function onProgressUpdate(event:ProgressEvent):void 
    	{
            progressBar.progress = event.bytesLoaded / event.bytesTotal;
            progressBar.updateProgressBar();
        }
    
        public function onVersionLoadError(event:IOErrorEvent):void
        {
            Alert.showMessage("Failed to load version.txt");
        }
    
        private function updateLocalVersion(remoteVersion:Number):void 
    	{
            var fileStream:FileStream = new FileStream();
            fileStream.open(versionFile, FileMode.WRITE);
            fileStream.writeUTFBytes(remoteVersion.toString());
            fileStream.close();
        }
    	
        public function onDownloadComplete(event:Event):void
        {
    
          //var percent:Number = events.bytesLoaded / events.bytesTotal;
          //dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, events.bytesLoaded, events.bytesTotal));
            var downloadStream:URLStream = event.target as URLStream;
            var fileBytes:ByteArray = new ByteArray();
            downloadStream.readBytes(fileBytes);
    
            var fileStream:FileStream = new FileStream();
            fileStream.open(new File(localFilePath), FileMode.WRITE);
            fileStream.writeBytes(fileBytes, 0, fileBytes.length);
            fileStream.close();
    
            //Alert.showMessage("Downloaded resources.zip");
            var remoteVersion:Number = Number(zipLoader.data); // Получаем удаленную версию файла
            updateLocalVersion(remoteVersion); // Обновляем локальную версию файла
            extractLocalArchive();
        }
    
        public function onDownloadError(event:IOErrorEvent):void
        {
            Alert.showMessage("Failed to download resources.zip");
        }
    
        public function extractLocalArchive():void
        {
            var resourcesFolder:File = new File(File.applicationStorageDirectory.nativePath + File.separator + "cache/resources");
            if (resourcesFolder.exists && resourcesFolder.isDirectory)
            {
                resourcesFolder.deleteDirectory(true); // Удаление папки “resources” с ее содержимым
            }
    
            var zipFile:FZip = new FZip();
            zipFile.addEventListener(Event.COMPLETE, onZipExtracted);
            zipFile.load(new URLRequest(localFilePath));
        }
    
        public function onZipExtracted(event:Event):void
        {
            var zipFile:FZip = event.target as FZip;
    
            try {
                for (var i:int = 0; i < zipFile.getFileCount(); i++)
                {
                    var zipEntry:FZipFile = zipFile.getFileAt(i);
                    var targetFilePath:String = File.applicationStorageDirectory.nativePath + File.separator + "cache/resources" + File.separator + zipEntry.filename;
                    var targetFile:File = new File(targetFilePath);
    
                    if (zipEntry.filename.charAt(zipEntry.filename.length - 1) == "/") {
                        targetFile.createDirectory();
                    } else {
                        var targetFileStream:FileStream = new FileStream();
                        targetFileStream.open(targetFile, FileMode.WRITE);
                        targetFileStream.writeBytes(zipEntry.content);
                        targetFileStream.close();
                    }
                }
                // Закрываем архив
                zipFile.close();
                // Удаляем архив
                var file:File = new File(localFilePath);
                file.deleteFile();
    			
                Alert.showMessage("Extracted successfully!");
                } catch (error:Error) {
                    Alert.showMessage("Failed to extract resources.zip: " + error.message + " (" + error.errorID + ")");
                }
        }
    
        private function versionIsUpToDate(version:Number):Boolean
        {
            if (versionFile.exists) {
                var fileStream:FileStream = new FileStream();
                fileStream.open(versionFile, FileMode.READ);
                var localVersion:Number = Number(fileStream.readUTFBytes(fileStream.bytesAvailable));
                fileStream.close();
                return version == localVersion; // Возвращает true, если версии совпадают.
            }
            return false;
        }
    }
    

    }

  2. Assistant

    To hide the progress bar when the local and remote version.txt files are the same, you can add a conditional statement in the onLocalVersionLoaded function in the ZIPResourceLoader class. Here's an updated code for that function:

    function onLocalVersionLoaded(event:Event):void {
        var localVersion:Number = Number(versionLoader.data);
        if (localVersion != remoteVersion) {
            startDownloadProcess();
        } else {
            Alert.showMessage("Local version is up to date");
            progressBar.visible = false; // Hide the progress bar
            // Пропущен код для распаковки архива
        }
    }
    

    With this code, if the local version and remote version are the same, the progress bar will be hidden by setting its visible property to false.

  3. User

    мне нужно чтобы скрылся чисто progressBar а не backgroundBar

  4. Assistant

    To hide only the progressBar instead of the backgroundBar, you can modify the updateProgressBar method in the ProgressBar class. Here's an updated code for that method:

    public function updateProgressBar():void
    {
        progressBar.visible = progress > 0; // Hide the progressBar when progress is 0
        progressBar.graphics.clear();
        if (progress > 0) {
            progressBar.graphics.beginFill(0x348aee);
            var cornerRadius:Number = 3;
            var yOffset:Number = -2; // смещение по вертикали на 2 пикселя
            progressBar.graphics.drawRoundRect(0, yOffset, maxWidth * progress, maxHeight + 4, cornerRadius);
            progressBar.graphics.endFill();
        }
    }
    

    With this code, the progressBar will be hidden when the progress is 0, and it will be visible and updated with the progress value when it is greater than 0. The backgroundBar will still be visible as usual.

  5. User

    progressBar.visible = false; // Hide the progress bar так это скрывает прогресс бар, надо сделать чтобы не весь прогресс бар скрывался целиком а только сама полоса а backgroundBar остался

  6. Assistant

    If you want to hide only the progress bar itself (the colored fill) and keep the background bar visible, you can modify the updateProgressBar method in the ProgressBar class. Here's the updated code:

    public function updateProgressBar():void
    {
        progressBar.visible = progress > 0; // Hide the progressBar when progress is 0
        progressBar.graphics.clear();
        if (progress > 0) {
            progressBar.graphics.beginFill(0x348aee);
            var cornerRadius:Number = 3;
            var yOffset:Number = -2; // смещение по вертикали на 2 пикселя
            progressBar.graphics.drawRoundRect(0, yOffset, maxWidth * progress, maxHeight + 4, cornerRadius);
            progressBar.graphics.endFill();
        }
    }
    

    With this code, the progressBar will be hidden when the progress is 0 (no fill will be drawn), and it will be visible and updated with the progress value when it is greater than 0. The backgroundBar will remain visible as usual.

  7. User

    ты не правильно меня понял, когда version.txt одинаковый, мне надо сделать чтобы было видно только backgroundBar без progressBar

  8. Assistant

    Apologies for the misunderstanding. If you want to hide the progressBar completely and only show the backgroundBar when the version.txt files are the same, you can modify the onLocalVersionLoaded function in the ZIPResourceLoader class. Here's the updated code:

    function onLocalVersionLoaded(event:Event):void {
        var localVersion:Number = Number(versionLoader.data);
        if (localVersion != remoteVersion) {
            startDownloadProcess();
        } else {
            Alert.showMessage("Local version is up to date");
            progressBar.visible = false; // Hide the progressBar
            backgroundBar.visible = true; // Show the backgroundBar
            // Пропущен код для распаковки архива
        }
    }
    

    With this code, when the local and remote version.txt files are the same, the progressBar will be hidden by setting its visible property to false, and the backgroundBar will be shown by setting its visible property to true.

  9. User

    надо сделать что что подобное progressBar.progressBar.visible = false;

  10. Assistant

    Apologies for the confusion. In order to hide only the colored fill of the progress bar (progressBar) while keeping the background bar (backgroundBar) visible, you can modify the updateProgressBar method in the ProgressBar class. Here is the updated code:

    public function updateProgressBar():void
    {
        progressBar.visible = progress > 0; // Hide the colored fill when progress is 0
        progressBar.graphics.clear();
        if (progress > 0) {
            progressBar.graphics.beginFill(0x348aee);
            var cornerRadius:Number = 3;
            var yOffset:Number = -2; // vertical offset of 2 pixels
            var fillWidth:Number = maxWidth * progress;
            progressBar.graphics.drawRoundRect(0, yOffset, fillWidth, maxHeight + 4, cornerRadius);
            progressBar.graphics.endFill();
        }
    }
    

    With this code, when the progress is 0, the progressBar will be hidden by setting its visible property to false, and when the progress is greater than 0, the progressBar will be visible and updated with the progress value. The backgroundBar will remain visible as usual.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.