actionscript 3 contentHeight not updating properly - actionscript-3

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.

Related

How to load a swf and interact with it?

I have tried SWFLoader, but the problem is the loaded content is MovieClip and I don't know how to interact with it, and the MovieClip#numChildren is zero.
And by the way, I can't pass the flashvars to the swf.
Firstly, you should know that there is no exact answer to your question as it depends on your loaded SWF (you know it or not, its display list, ...) but I'll put a simple example to explain things and you have to adapt it to your case.
For this example, let's say that we have a very simple SWF (the loaded SWF) which contain a TextField (called txt_url) and a button (a MovieClip, called btn_go).
The btn_go button will open the URL entered in the txt_url TextField.
For our second SWF (the loader), we will use a Loader object to load our first one (which is in this case will be the Loader.content) and then we will set the URL (the txt_url text) and trigger the click event on the btn_go button.
So here is an example of the code of our loader.swf :
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_SWFLoad);
loader.load(new URLRequest('loaded.swf'));
addChild(loader);
function on_SWFLoad(e:Event): void
{
// get our loaded SWF
var loaded_swf:DisplayObjectContainer = DisplayObjectContainer(loader.content);
// because we know our target objects, we can use "getChildByName()"
// set the URL
TextField(loaded_swf.getChildByName('txt_url')).text = 'http://www.example.com';
// open the URL in the browser by triggering the click event on the "btn_go" button
MovieClip(loaded_swf.getChildByName('btn_go')).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
This example will directly set and open the URL in the browser after loading the SWF, of course we can execute that action after clicking a button or something else but it's just a simple example to show you how you can do ...
Now, the problem is when we don't know anything about the loaded SWF and its children (names, depths, ...), in this case we should do more effort to do what we want : we should traverse the entire display list of the loaded SWF to identify the target objects.
Returning to our example and let's say that we only know that there are a TextField and a button in the stage, so our code can be like this for example :
function on_SWFLoad(e:Event): void
{
var loaded_swf:DisplayObjectContainer = DisplayObjectContainer(loader.content);
var num_children:int = loaded_swf.numChildren;
for(var i:int = 0; i < num_children; i++)
{
var child:DisplayObject = loaded_swf.getChildAt(i);
if(child is TextField)
{
trace(child.name); // gives : txt_url
TextField(child).text = 'http://www.example.com';
}
else
{
if(child.hasEventListener(MouseEvent.CLICK))
{
trace(child.name); // gives : btn_go
child.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
}
}
}
Again, it's a very simple example just to show how we can proceed ...
...
Then about passing values (params) between SWFs, take a look on my answer of this question where you have a little example for that.
For more about Display programming (display list, display object, display object container, ...) take a look here.
Hope that can help.

How to make a close button for a Flash banner ad with Actionscript 3

I am working on a Flash banner ad built with Actionscript 3. It will be embedded into web pages.
The ad needs to have a close button. When the user clicks the button the ad should disappear.
This needs to be done entirely in the banner, as we have no control over the webpages where it will be embedded.
In Actionscript 2 I used to do this by running this code when the user clicked the button:
unloadMovie(this);
This seems to no longer work in Actionscript 3. What can I do to achieve this functionality in AS3?
Thanks for your help.
For documentation purposes, here are the conclusions Andreyu and I reached regarding the issue: unloading/removing a swf file from within the swf to allow users access to the elements under the swf.
One option is to use ExternalInterface to inject js code to:
get the id/name of the swf as registered with the DOM
remove the swf element from the DOM using the found id/name
In terms of code, this is using a technique described on Zeh Fernando's blog
as modified by Andreyu to include the DOM element removal:
// Based on work by Zeh Fernando: http://zehfernando.com/2011/getting-the-swfs-html-objectembed-id-from-within-the-flash-movie-itself/
function getSWFObjectName(): String {
// Returns the SWF's object name for getElementById
// Based on https://github.com/millermedeiros/Hasher_AS3_helper/blob/master/dev/src/org/osflash/hasher/Hasher.as
var js:XML;
js = <script><![CDATA[
function(__randomFunction) {
var check = function(objects){
for (var i = 0; i < objects.length; i++){
if (typeof(eval("objects["+i+"]." + __randomFunction)) != undefined) {
return objects[i].id;
}
}
return undefined;
};
return check(document.getElementsByTagName("object")) || check(document.getElementsByTagName("embed"));
}
]]></script>;
var __randomFunction:String = "checkFunction_" + Math.floor(Math.random() * 99999); // Something random just so it's safer
ExternalInterface.addCallback(__randomFunction, getSWFObjectName); // The second parameter can be anything, just passing a function that exists
return ExternalInterface.call(js, __randomFunction);
}
// Function to remove the SWF from the webpage
function destroyEverything(event:MouseEvent): void {
var js:XML;
js = <script><![CDATA[
function(__SWFContext) {
var element = document.getElementById(__SWFContext);
element.parentNode.removeChild(element);
}
]]></script>;
ExternalInterface.call(js, getSWFObjectName());
}
// Add function to click event of button
close_button.addEventListener(MouseEvent.CLICK, destroyEverything);
If unloadMovie was enough for you, you could simply remove everything from the stage:
//In button click handler, call "removeEverything" function
//function onClickClose(e:MouseEvent):void{
// removeEverything()
//}
//As a result you will get empty stage
function removeEverything():void {
while (stage.numChildren) {
stage.removeChildAt(0);
}
}

Actionscript(AIR) Cant return FB access_token from HTMLLoader

Im simply trying to get my users access token from FB using HTMLLoader. When i trace the output from the location change event all i get are Google/FB urls, none of which have the token.
Here is my code:
var loader:HTMLLoader;
function onChange(e:Event):void {
if(loader.location.indexOf("#") != -1) {
trace("Found token");
}else {
trace(loader.location);
}
}
function connectFb() {
loader = new HTMLLoader();
loader.addEventListener(Event.LOCATION_CHANGE, onChange);
loader.width = 640;
loader.height = 480;
loader.load(new URLRequest("https://graph.facebook.com/oauth/authorize?
client_id=1234567890123456&redirect_uri=http://google.com&type=user_agent"));
}
My Codes output:
https://graph.facebook.com/oauth/authorize?client_id=164534120383085&redirect_uri=http://google.com&type=user_agent
http://www.google.com/
I tried every tutorial on the net and even bought a book on AS3 facebook dev & still cant figure this out; Any help would be VERY appreciated since this is a fairly important project to me
From what I understand, you're trying to retrieve an oauth access token from the facebook API, right? That's what you're expecting to get back from that URL? Correct me if I'm wrong, but HTMLLoader is for (unsurprisingly) loading HTML content. If you're expecting HTML content (ie, an 'authorize' page from FB), then you need to add the HTMLLoader to the display list with:
addChild(loader); // where 'loader' is an instance of HTMLLoader
On the other hand, if you were expecting just the token (and not HTML content), then you should be using URLLoader, and not HTMLLoader.

FLVPlaybackCaptioning + Custom position

Using FLVPlayback Captioning component I would like to move the subtitle text at certain parts in y-position. Is this possible in AS3?
All of my own custom arguments are ignored when subtitles are parsed and wrapping the specific part with some sort of character won't do it either as I cannot change the text during runtime.
The reason is that in my videostreams there is boxes with text content that I don't want the subtitle on top of, and rather above for reading purposes.
I was thinking of either doing an own manual subtitle function or custom flash cuepoints that I can access but want to know if anyone has done this before.
Something like this will do it. I found out that autoLayout was over overridden by the subtitle xml so I forced it to false every "change".
public function Init() : void
{
// captions
_captions = new FLVPlaybackCaptioning();
_captions.autoLayout = false;
_captions.flvPlayback = _video;
_captions.addEventListener(CaptionChangeEvent.CAPTION_CHANGE, onCaptionChange);
_captions.source = "mySubs.xml";
addChild(_captions);
}
private function onCaptionChange(pEvent : CaptionChangeEvent) : void
{
if(!_captions.captionTarget)
return;
_captions.autoLayout = false; // force autoLayout
_captions.captionTarget.y = 666; // position of choice
}

calling function in a runtime loaded swf

Using AS3 / Flash CS4
Alright thanks to everyone who is reading this. My problem specifically I am designing a user interface with controls on it. 5 buttons rotate left, rotate right, zoom in, zoom out, and auto rotate. The interface in itself works fine, I can trace out button clicks, toggle the auto rotate button etc...
My program reads an xml file. Loads some images, fills an array with links for each image, and when the image is clicked a loader loads a swf from a URL and displays it on screen. No problem.
Now I originally had the zoom controls user interface in the runtime_loaded.fla library, and the mouse listeners in the same linked document class. The interface works with the movieClip in runtime_loaded.swf when it is in the same code.
Now to practice good coding, I decided to remove the UI from the runtime_loaded.fla and add it to the Main.fla. This is essential because the main is going to handle possible 100's of images/objects that each have their own unique swf to link too. If I decide to change out the look of the interface but leave the function calls the same, I could essentially put in as many as I want into the main.fla instead of the runtime_loaded.fla which I would have to do for every single object.
THE FILE STRUCTURE
Main.fla <- interface in the library. 5 mouse event functions. Inside each function calls
a property of loaded_swf (loaded_swf.rotateLeft, loaded_swf.rotateRight) etc...
Runtime_loaded.fla <- links specificObject.as in properties (AS3/CS4)
specificObject.as <- has 5 public static functions{ rotateRight, rotateLeft, zoomIn, zoomOut, toggleAutoRotate }
THE CODE
//showFlashBox
function showFlashBox(temp_string:String):void {
if(!flash_box_on){
var temp_top:uint = numChildren;
addChildAt(FlashBoxContainer,temp_top);
newXButton.addEventListener(MouseEvent.CLICK, flashBoxXClick);
1. addChild(new_loader);
2. var url:URLRequest = new URLRequest(temp_string);
new_loader.x = 0;
new_loader.y = 0;
new_loader.load(url);
3. new_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotSwf);
flash_box_on = true;
}
}
function gotSwf(e:Event){
4. //loaded_swf = e.target.content;
trace(e.target.content);
5. new_zoom_controls.button_minus.addEventListener(MouseEvent.CLICK, zoomOutFunction);
new_zoom_controls.button_plus.addEventListener(MouseEvent.CLICK, zoomInFunction);
new_zoom_controls.button_left.addEventListener(MouseEvent.CLICK, rotateLeftFunction);
new_zoom_controls.button_right.addEventListener(MouseEvent.CLICK, rotateRightFunction);
new_zoom_controls.button_rotate.addEventListener(MouseEvent.CLICK, toggleRotateFunction);
function rotateRightFunction(e:MouseEvent){
6. //loaded_swf.rotateRight();
}
function rotateLeftFunction(e:MouseEvent){
//loaded_swf.rotateLeft();
}
function zoomInFunction(e:MouseEvent){
//loaded_swf.zoomIn();
}
function zoomOutFunction(e:MouseEvent){
//loaded_swf.zoomOut();
}
function toggleRotateFunction(e:MouseEvent){
//loaded_swf.toggleAutoRotate();
if(new_zoom_controls.button_rotate.currentFrame == 1){
new_zoom_controls.button_rotate.gotoAndStop(2);
}
else new_zoom_controls.button_rotate.gotoAndStop(1);
}
new_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, gotSwf);
}
If you follow steps 1-6 you see my steps of loading the .swf, mouse event listeners and click handlers, then the object call of the var loaded_swf:Object;
public static function rotateLeft():void
{
object.yaw(animation_turning_speed);
}
public static function rotateRight():void
{
object.yaw(-animation_turning_speed);
}
if I run the main.fla and try to click the buttons. This happens.
ReferenceError: Error #1069: Property rotateRight not found on
ThreedsKU39web and there is no default value. at MethodInfo-82()
ReferenceError: Error #1069: Property rotateLeft not found on
ThreedsKU39web and there is no default value. at MethodInfo-83()
I actually stumbled upon the answer before I finished submitting this as I went through the code to copy it. But after spending a few hours of frustrating moments on this last night, I will post it to ensure the next guy doesn't meet the same demise.
The answer was in the function of the runtime-loaded swf class.
public static function rotateRight():void
{
object.yaw(-animation_turning_speed);
}
It turns out it only needs to be public function instead of public static.
Mainly human error as after the file was working, I attempted to copy the code over to all my other object files, and somehow static got back in there and messed it up. So for future reference when loading in the external swf, public function should do the trick. *Note that many of my variables were returning errors until being declared as public static though.