Flash HTML image link AS3 - html

I have a flash as3 file, and I'm importing an HTML file that contains this link:
<img src="https://www.paypal.com/en_GB/i/btn/btn_buynowCC_LG.gif" id="paypal_button">
and I cannot for the life of me figure it out, I've looked everywhere.
There were a couple of forums that suggested:
function onTicketLoad():void {
if(paypal != null && this.contains(paypal)) {
removeChild(paypal);
}
var imgLoader:DisplayObject = content_text.getImageReference("paypal_button");
//imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onHtmlImageLoaded);
paypal.addChild(imgLoader.content);
}
function onHtmlImageLoaded(e:Event):void {
e.target.removeEventListener(Event.COMPLETE, onHtmlImageLoaded);
paypal.addChild(e.target.content);
paypal.buttonMode = true;
paypal.useHandCursor = true;
addChild(paypal);
}
(that example is fragmented as all hell, I just included it to illustrate what I'm trying to do)
Basically I think I need to load the image into the getImageReference method... but I'm so lost... any help would be appreciated!

I've found creating links on <img> tags in TextFields in Flash can be problematic.
I spotted the issue has been reported on the adobe bug tracker here:
http://bugs.adobe.com/jira/browse/FP-2146
Once as a workaround I created a separate swf file the exact same size as the image and embedded that instead (i.e. a 160x47 stage and a MovieClip/button with a clickable link using navigateToURL). Basically you can embed swf files within TextFields the same way as images.
It's not ideal but it did seem like a decent compromise considering the swf 'button' can now be used and laid out in more or less the same way as you would have used the <img> tag.

If you are going to use the < img > tag in an htmlText box, "you must set the text field to be multiline and to wrap text"
I tried it, and it works fine for me. It loaded the image and the link worked too. Here is my code:
import flash.text.TextField;
var labelText:String = '<img src="https://www.paypal.com/en_GB/i/btn/btn_buynowCC_LG.gif" id="paypal_button">';
var label:TextField = new TextField();
label.border = true;
label.multiline = true;
label.wordWrap = true;
label.width = 200;
label.height = 100;
addChild(label);
label.htmlText = labelText;
My example uses pure AS3, but it should be exactly the same as selecting "multiline" and "wordwrap" from your textfields properties panel.

AIR (Mobile) ignores IMG tags for security apparently... useless in my opinion.
Adding the fact the TextFlow is not recommend or supported for that either in Mobile, and many other fruit less attempts to get an IMG tag to render is beyond challenging so far. I'll wager nearly impossible with the given components (in AIR Mobile land of course, which is not well documented and conflictive with online sources around the issues, so it is unclear and confusing being fresh out of beta).

Related

Play video in the background in TVML

I'm starting a player and then pushing a document onto the navigation stack. Video is playing in the background but is not visible as the document is occupying the screen. Can anyone please tell me how to make the document transparent or is there any better approach to achieve the same?
Overlay TVML?
TVJS player property overlayDocument, see https://developer.apple.com/library/tvos/documentation/TVMLJS/Reference/TVJSPlayer_Ref/index.html or https://forums.developer.apple.com/thread/18431
You can get interactive overlay over videos as referred to in the following link:
http://devstreaming.apple.com/videos/wwdc/2016/212s41rh77qgdg26s86/212/212_developing_tvos_apps_using_tvmlkit_part_1.pdf
Overlays can be made interactive since you are essentially adding documents to overlay doc. So any kind of button lock up will work just fine.
Interactive overlay document is the way to go. The following code shows how this is done.
App.onLaunch = function(options) {
var singleVideo = new MediaItem('video', 'http://somevideo.mp4');
var videoList = new Playlist();
videoList.push(singleVideo)
var myPlayer = new Player();
myPlayer.playlist = videoList;
var parser = new DOMParser();
var parsedDoc = parser.parseFromString(`<document>
<alertTemplate>
<button>
<text>Click Me</text>
</button>
</alertTemplate>
</document>`, "application/xml");
myPlayer.interactiveOverlayDocument = parsedDoc;
myPlayer.interactiveOverlayDismissable = false;
myPlayer.play();
}
Apple doesn't have a lot of doc on it. but they did mention it in WWDC,
slide 98-102 of the WWDC 2016 presentation.
the end of this document-- Apple TV Markup Language Reference-- Compound Multimedia Elements
and an example in TVML Guide: Core concepts in TVML and TVMLKit-- Server/PlaybackVideo/index.js
This thing does have some bugs, namely the interactiveOverlayDismissable value not being active if the video is the first thing you display, or does this whole thing play well with the native code outside of TVML. But this is the easier part to do video background.
Note, you might want to add some even listeners to replay the video at the end, as the default behavior is to pop the view.

AS3 POST png to PHP

Have read many similar but no solutions: I'm trying to do something (that i think is) really simple,
Take a photo from gallery or camera - and POST this to a URL. No saving / returning to application. The PNG is just used by the webpage in display and user moves onward.
public function postPicture():void {
PNGEncoder2.level = CompressionLevel.FAST;
var encoder : PNGEncoder2 = PNGEncoder2.encodeAsync(_bitmapData);
encoder.targetFPS = 12;
encoder.addEventListener(Event.COMPLETE, function (e):void {
var png : ByteArray = encoder.png;
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("https://myurl.com/mypage");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = png;
navigateToURL(jpgURLRequest, "_blank");
});
}
Also to note, we're getting some odd characters on the end of the URL: C9%90NG
Hunch is that I'm decoding and then not encoding properly before sending i.e. these characters some part of raw image info.
When I test from chrome POST extension with another image, server side stuff works fine so guessing a problem with AS3.
basically its working when I use chrome to browse > my.png and POST it at the page. Am I missing something with the AS3 png encoder? i.e. how to turn that bmpdata into a 'file' rather than an array of bytes.
thankyou
You need to use multipart upload. It's a common situation with a well defined standards for that. You can implement it by yourself but it will take some time and efforts.
What I've used before is the Multipart URLLoader Class provided by Eugene Zatepyakin, one the best Flash guru's out there :)
It's some kind of old but does the job extremely well. You can add variables, files, whatever content you want to your loader and simply submit it. There are examples with the php side of the thing so you can understand what's going on.

Downloading a dynamically generated SVG file from the browser

If I create an image using HTML SVG element, can I then offer this as an SVG file download to the user. For example I may want to load an SVG image, apply some basic transformations to it, add some text, then let the user download the result as a vector image.
Is that possible? I have been doing something similar with Canvas but have been struggling creating a vector image. I wasn't aware that SVG elements were so versatile when I cam across them this morning but if I can do the above it would be great.
Simple solution using a data URI:
var svg_root = document.getElementById('your_svg_root_element_here');
var svg_source = svg_root.outerHTML;
var svg_data_uri = 'data:image/svg+xml;base64,' + btoa(svg_source);
var link = document.getElementById('anchor_element');
link.setAttribute('href', svg_data_uri);
Although it worked, when clicking on the link, the browser stalled for a few seconds.
This seems to be the simplest solution and should be compatible with all modern browsers. However, it has some noticeable overhead. If someone else knows a different solution (maybe using blobs or something similar), please add here as another answer!

unload dynamically loaded swf after displaying it AS3

I am doing a presentation on which I need to use a lot of video clips. I load all these videos dynamically using Loader. I use a code snippet like this:
var req:URLRequest = new URLRequest("video.swf");
var a:Loader = new Loader();
a.load(req);
stage.addChild(a);
a
Now the issue is: When I get to say the 7th one, it starts lagging. I do not know why, but I think it is because everything is loaded to memory. Is there a way I can erase a video from memory after displaying it? Or is there another solution to this?
If you just load a new video each time and add it to the stage, you will have a rather big hierarchy of movies sitting on top of each other; all kept on stage and in memory.
If you keep a reference to the previously loaded movie, you can just remove it from the stage when the 'next' movie is loaded. Haven't tested this properly, but just to give you an idea of what the code might look like:
var oldLoader:Loader = null;
var a:Loader = new Loader();
a.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
a.load(<your url>);
function loader_complete(e:Event):void {
var newLoader:Loader = e.currentTarget.loader as Loader;
stage.addChild(newLoader);
if(oldLoader != null) {
stage.removeChild(oldLoader);
oldLoader = null;
}
oldLoader = newLoader;
}
I think I found the solution. If you use removeChild, it only stops displaying, but the whole thing is still in memory (cache). So the more you load the videos, the more you fill up the cache. What we need is something that completely clears the cache before loading another video.
I used unloadAndStop() and it seem to have completely done the trick, unless someone has something against it.
Thanks again

scrollbar in flash CS4

I am trying to put a vertical scrollbar onto a dynamic TextField in Flash CS4. When I do this in Scene 1, everything works just fine. However, if I move the TextField+Scrollbar to Scene 2, it breaks. The errors I get when I run the code indicate that Flash is loading the scrollbar right away and then trying to find the associated TextField. The problem is that, since the TextField doesn't load until you get to Scene 2, Flash throws an error.
I tried a lot of things to solve this issue. What it mostly came down to was that I needed to create the TextField and Scrollbar in actionscript (instead of from the component library) so that I could control when each was created. To create the TextField, I typed:
//create a textfield for the story
import flash.text.TextField;
var story_txt:TextField = new TextField();
//story_txt.multiline = true;
story_txt.x = 154;
story_txt.y = 233.5;
story_txt.width = 348;
story_txt.height = 104.5;
story_txt.border = true;
story_txt.type = "dynamic";
story_txt.backgroundColor = 0xffffff;
story_txt.background = true;
story_txt.wordWrap = true;
story_txt.multiline = true;
and to create the scrollbar, I then typed:
import fl.controls.UIScrollBar;
//add the story_txt to the stage
addChild(story_txt);
var mySb:UIScrollBar = new UIScrollBar();
mySb.direction = "vertical";
// Size it to match the text field.
mySb.setSize(story_txt.width, story_txt.height);
// Move it immediately to the right of the text field.
mySb.move(story_txt.x, story_txt.height + story_txt.y);
// put them on the Stage
mySb.scrollTarget = story_txt;
//mySb.scrollTargetName = "story_txt";
stage.addChild(mySb);
The only problem is that this code only works if I make the ScrollBar horizontal and turn off wordwrap. For some reason, the vertical scrollbar will not work with wordrap on (and w/o wordwrap, Flash thinks that there's no need for a vertical scrollbar, as it sees only a single line of text.
Getting a textfield w/a scrollbar should be a simple process, but this is really out of hand. Any ideas on how to get the ScrollBar to work when wordWrap is turned on?
Basically you should never use Scenes in Flash. There are a lot of known issues with them.
This thread might help.
Scene usage is generally bad practice. They are a legacy feature left in to keep current versions of flash compatible with earlier versions. If you must use the IDE to place things on stage you should use distinct frames on the main timeline in place of scenes, and place things that need the timeline to animate inside their own movieclips. The flash IDE actually reduces all the scenes down to one timeline at compile time any way but with many anomalies that wouldn't be present in manual timeline manipulation.
disadvantages of scenes from adobe: http://help.adobe.com/en_US/Flash/10.0_UsingFlash/WSd60f23110762d6b883b18f10cb1fe1af6-7eb3a.html