Progress Event not Triggering ONLINE - actionscript-3

I am making a Flash Photogallery and I have a problem with my preloader. My gallery works fine offline(in simulating download) but the problem is when it's online the loading percentage won't show up. So far I figured out that the ProgressEvent is not triggering when gallery loads full size image. Sometimes it triggers on 100%, sometimes it triggers on time but usually it don't trigger. Here is the link of my gallery and some code: solarratko.netii.net
public function kreni(f:String) //function that start when user click on thumbnail
{
URLrequest=new URLRequest(f); //URLrequest for image in full size
dspLoader.load(URLrequest); //loading the image
preloader.visible = true;//prelaoder that shows up is visible
h.visible=true;//text area for percentage is visible
dspLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progres);//adding progress event
dspLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, kraj);//adding complete event
}
public function progres(e:Event):void
{
var perc:Number = e.target.bytesLoaded / e.target.bytesTotal;//calculatin percentage
h.text = Math.ceil(perc*100).toString();//displaying percentage wich is not working online or it start too late
}
public function kraj(e:Event):void
{
h.text="";
preloader.visible = false;
h.visible=false;
}

Related

actionscript 3 contentHeight not updating properly

I am using Adobe Flash Builder with actionscript to make a desktop application.
I am getting some html code from a webpage and putting it into an mx:html element, then attempting to get the content height in order to determine if I should hide the vertical scrollbar or not. However, when using contentHeight it seems to return the height of the previous state of the element, rather than the one just set.
This is the code to fetch the html page
var htmlPageRequest:URLRequest = new URLRequest(url);
htmlPageRequest.method = URLRequestMethod.GET; //set request's html request method to GET
htmlPageLoader.addEventListener(Event.COMPLETE, onHtmlLoaded); //listen for page load
htmlPageLoader.load(htmlPageRequest);//when loaded continue logic in new function
This is the function that is run when the page request is complete
private function onHtmlLoaded(e:Event):void { //logic after html page has loaded
HtmlElement.data = htmlPageLoader.data; //set content
//determine if vscroll bar should be visible
if(HtmlElement.contentHeight > HtmlElement.height) {
scrollbar.visible = true;
}
else {
scrollbar.visible = false;
}
trace(HtmlElement.height);
trace(HTMLELEMENT.contentHeight);
}
I have realised the solution to the problem:
htmlElement.data = htmlPageLoader.data;
Rendering the HTML takes a certain amount of time - the contentHeight is being accessed before the page has actually rendered, causing the previous value to be returned.
In order to fix this, I added an event listener for (htmlRender) in order to not access contentHeight until the rendering is complete.
private function onHtmlLoaded(e:Event):void { //logic after html page has loaded
htmlElement.addEventListener(Event.HTML_RENDER, onHtmlRendered); //once the html has rendered, move on
htmlElement.data = htmlPageLoader.data; //render content
}
private function onHtmlRendered(e:Event):void { //logic for after the page has rendered
//if the content of the HTML element is bigger than the box, show the scrollbar
if(htmlElement.contentHeight > htmlElement.height) {
scrollbar.visible = true;
}
else {
scrollbar.visible = false;
}
}
I am assuming that you are using a URLLoader (htmlPageLoader) instance here to load the webpage into the mx:HTML element, which may not be actually required.
The mx:HTML component actually provides an inbuilt way to load an webpage into itself. This can be done using the location property of the mx:HTML class. It expects a simple string which could be the URL of your webpage you are trying to load.
Once the webpage is loaded, the Event.COMPLETE method is fired, within which you should be able to get your content height properly. So please try the following code:
htmlElement.addEventListener(Event.COMPLETE, onHtmlLoaded);
htmlElement.location = "your URL goes here";
private function onHtmlLoaded(e:Event):void
{
htmlElement.removeEventListener(Event.COMPLETE, onHtmlLoaded);
trace(htmlElement.contentHeight);
}
I've tried the above with a couple of URL's and it seems to be working fine. Also, I took the liberty of using camel case naming convention for htmlElement. It's just best practice.
Hope this helps. Cheers.

How to add a clickTAG function on AS3?

I have this banner that has 3 tabs that would require clickTAGs added to the buttons of each tab section. Currently, the banner works fine with the code attached, but when I try to insert a clickTAG function, everything gets screwy and I end up having a blinking banner. I hope someone here can help me out.
This is the code that I am using:
this.btn_launchsite.addEventListener(MouseEvent.CLICK,f);
function f(e:MouseEvent) : void {
navigateToURL(new URLRequest ("http://kpmginfo.com/cfo/"),"_blank");
}
I tried adding this between the void { and navigateToURL:
var click_url:String = root.loaderInfo.parameters.clickTAG;
if(click_url) {
And this made the whole banner blinking.
i'm not sure entirely what your are trying to achieve ie. do you want to track clicks? within flash? send tracking data to an external php? either way I would firstly created your clickTag function outside your click button function & then call the clickTag function from within the click banner function.
eg. something like.
var URL_Tag = 0;
function ClickTagger (e:Event=null):void {root.URL_Tag = root.URL_Tag + 1;}
this.btn_launchsite.addEventListener(MouseEvent.CLICK, ClickBanner);
function ClickBanner (event:MouseEvent) : void {
navigateToURL(new URLRequest ("http://kpmginfo.com/cfo/"),"_blank");
ClickTagger();
}
trace (URL_Tag); // this line is for testing and debugging.

AS3 Preloader doesn't work locally, loaderInfo ProgressEvent.PROGRESS event does not fire

Making a custom AS3 preloader, I noticed that when my SWF is executed locally (file:///) the preloader gets stuck in the loading screen when previewed in a web browser like Chrome.
When executed from a remote server or through the standalone Flash Player, then it works. I noticed other SWF that have preloaders do not have this issue. What I need to change?
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preloaderProgress);
function preloaderProgress(event:ProgressEvent):void {
var loadedPercent:Number = event.bytesLoaded/event.bytesTotal*100;
if (loadedPercent == 100){
this.gotoAndStop(2);
}
}
Loading locally goes very fast. Not only you should check if the file is completely loaded using the Event.COMPLETE and not the ProgessEvent but you should also make sure to register your listeners before actually calling load or the file might end up completely loaded before you register your listeners.
Following the example at http://stephenscholtz.com/201110/single-movie-flash-preloading-as3
It seems that there was no ProgressEvent.COMPLETE, but there is a Event.COMPLETE instead, a bit confusing.
I changed my code to the following, (also including some tweaks for right click menu to prohibit the user from right clicking and pressing Play before movie has loaded and such)
//Initialize any variables
var loadedPercent:Number = 0;
//Remove all items from right click Flash Player menu (except Quality, and the mandatory About... & Settings...)
var cxMenu:ContextMenu = new ContextMenu();
cxMenu.hideBuiltInItems();
cxMenu.builtInItems.quality = true;
this.contextMenu = cxMenu;
/* or disable right click menu completely (Flash Player 11.2.202.228 and over) */
/* this.addEventListener(MouseEvent.RIGHT_CLICK, onMouseRightClick);
function onMouseRightClick(event:MouseEvent)
{
return false;
} */
//Disable shortcut keys and window menubar when played from desktop Standalone Flash Player app
if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
fscommand("showmenu", "false");
fscommand("trapallkeys", "true");
}
/* Preloader: begin */
//Update loading bar and percent text as SWF loads
function onProgress(e:Event) {
//Get the amount of bytes that have been loaded / bytes total to load
loadedPercent = this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal*100;
//Update the text of _preloaderProgress movieclip
_preloaderProgress.text = int(loadedPercent)+"%";
//Update the _preloaderBar amount by scaling horizontally as it loads
_preloaderBar.scaleX = loadedPercent/100;
}
//Go to next frame after everything loads
function init(e:Event) {
gotoAndStop(2);
}
//Attach the events to the stage
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, init);
/* Preloader: end */
//Stop to 1st frame and wait until it is loaded
stop();
This will allow the movie to play both remotely and locally with no problems.

Progress Bar reset in Flex

I have been working on resetting a progressBar in flex, but the thing is that progressbar correctly displays the bytes loaded while the file is downloaded for the first time, but for other consecutive file downloads, it just stays at 100% although the file is downloaded, How do I reset the the progressbar so that it correctly shows the progress for other files too.
I have tried using pb.setProgress(0,100) before the progressbar starts to set the progress to zero but it also does not helps.
Also I have set the mode to manual.
Ok here is what I have done :
Have a progress Event in which I am setting the progress of the progressBar as :
public function progressHandler(event:ProgressEvent):void
{
pb.width=300;
pb.height=30;
pb.labelPlacement="center";
pb.mode="manual";
pb.setProgress(event.bytesLoaded, event.bytesTotal);
}
Also in the open event I am calling a title window and adding the progress bar into it and then popping it using the popup manager as :
public function openWindow(event:Event):void {
tWin.title = "Please Wait";
tWin.width=400;
tWin.height=100;
tWin.addChild(pb);
PopUpManager.addPopUp(tWin, this, true,PopUpManagerChildList.POPUP);
PopUpManager.bringToFront(tWin);
PopUpManager.centerPopUp(tWin);
}
Then finally in the complete event, i am removing the popupmanager and setting the progress to be zero as :
public function completeHandler(evt:Event):void {
PopUpManager.removePopUp(tWin);
pb.setProgress(0,100);
}
But still the progress bar remains at 100% for any consecutive file downloads.
Any help..??

Flash: Stage.FullscreenInteractive -> Difference if class or 1st frame

example 1: 1st frame of my app
var screenBounds = Screen.mainScreen.bounds; //Bounds of current screen
var full:Sprite = new Sprite(); //Sprite Fullscreen
//Enter Fullscreen
function goFullScreen(e:Event = null) {
//myClass.goFullscreen();
full.graphics.clear();
full.graphics.beginFill(0xccff00);
full.graphics.drawRect(0,0,screenBounds.width, screenBounds.height);
full.graphics.endFill();
addChild(full);
this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
example 2: a normal as class package
private var full:Sprite = new Sprite(); //Sprite to show fullscreen
private var screenBounds = Screen.mainScreen.bounds; //Bounds of current screen
public function favoritesFullscreen():void {
full.graphics.clear();
full.graphics.beginFill(0xccff00);
full.graphics.drawRect(0,0,screenBounds.width, screenBounds.height);
full.graphics.endFill();
addChild(full);
this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
So, tell me WHERE IS THE DIFFERENCE?
I'm a macuser, you know, at the top the menubar and in my case the dock is aligned left
It's weird but example 1 does exactly what it should. It creates a fullscreen rectangle across the ENTIRE screen (from 0,0 to the right bottom)
However, example 2 kind of calculates the width of the top-menubar and the dock and starts the fullscreen rectangle a approx 40px from the left edge of the screen (dock) and 20px from the top (menubar)... i don't get why a external class acts different than as on the first frame.
??? thank you for your help!
I'm guessing it has to do with when the Screen.mainScreen.bounds are retrieved. On compile time, flash internally moves all frame code within functions in the DocumentClass and calls them after the stage has completly initialized. So, in the 1st example, you are retrieving the screenBounds after everything is initializated, but in the 2nd example you are doing it much earlier.
I guess waiting for the ADDED_TO_STAGE event would be enough. If it's not, the first ENTER_FRAME event dispatched should definitely work.