How to make an object disappear in Action Script 3 - actionscript-3

I have used the following code to show an HTML page in my App which is supported by Action Script 3.0 and Adobe Air.
imports
import flash.events.Event;
import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;
//setup variables
var _stageWebView:StageWebView;
var myAdvertURL:String = "http://letschant.890m.com/v25.html";
// check that _stageWebView doesn't exist
if (!_stageWebView)
{
_stageWebView = new StageWebView() ;
// set the size of the html 'window'
_stageWebView.viewPort = new Rectangle(0, 0, 480, 688);
// add a listener for when the content of the StageWebView changes
_stageWebView.loadURL(myAdvertURL);
}
// show the ad by setting it's stage property;
_stageWebView.stage = stage;
So, If I click a button all these should disappear.
I know that we should use visible:false, but where should i put the code?, This aint a movie clip, so how should I do this?
Please help.
Thank you :D

StageWebView is not a DisplayObject. So you cannot set its visible property. You'll have to use another API call to hide the web view. Setting the viewPort to an rectangle of 0 pixels width and height would do the job.
Addressing your immediate problem, place your button on the stage and give it the instance name btnToggle. Add the following code in the same frame where you've added your current chunk.
this.btnToggle.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void
{
_stageWebView.viewPort = new Rectangle(0, 0, 0, 0);
});

Related

how can I add display.movieclip(include as3 inside and embedded video, means lots of video frames) to starling?

I created a movieclip, I imported a video inside it ,as3 code that loads an image, and animate that image on the video.I use as3 codes like below
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
var imgLoader:Loader = new Loader();
imgLoader.load(new URLRequest(File.applicationStorageDirectory.resolvePath("imagem.jpeg").url));
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded1);
because of performence issues, I choose Starling to record that movieclip. and I use below code for that,
var kmcv2:mcv2 = new mcv2();
kmcv2.x=304;
kmcv2.y=208;
kmcv2.width=750;
kmcv2.height=382;
Starling.current.nativeOverlay.addChild(kmcv2);
but this code does not record it, because my movieclip is on the top.
stage.drawToBitmapData( _bmp );
how can I handle it?
thank you.

Can't have 2 dynamic text fields AS3

This is not a specific situation, this is something that happens a lot to me.
Whenever I have dynamic text (that changes through code, of course) everything works fine.
However, when I add a second Dynamic text, both of them do not show.
An example scenario would be:
textfield1.text="hello";
and on the next frame
textfield2.text="goodbye";
no text would show.
Something is wrong with flash, maybe. The problem starts when I add another textbox to the stage.
I do not want to upgrade to a newer flash, but I could if I have to.
If anybody knows how to fix my problem, please tell me.
I think that embed fonts is optional if you create and add an instance of TextField trough AS3 (excepts if you specify a font that is not present on another computer).
In the example here bellow, the text for textfield1 and textfield2 is always displayed, so I probably misunderstand your question.
Best regards.
Nicolas
PS : the "var timer:Timer" and the callback function are only used to make this example switch an loop from frame 1 to frame 2
Example 1
frame 1:
import flash.utils.Timer;
import flash.events.TimerEvent;
if (! textfield1 && ! textfield2)
{
import flash.text.TextField;
import flash.geom.Point;
var textfield1:TextField = new TextField();
var textfield2:TextField = new TextField();
var tfPosition:Point = new Point(100,50);
}
try{
removeChild(textfield2);
}catch(e:Error){
(trace (" ERROR : textfield2 is not already added"));
}
addChild(textfield1);
textfield1.x = tfPosition.x;
textfield1.y = tfPosition.y;
textfield1.text = "hello";
stop();
function playStop(te:TimerEvent):void{
play();
}
if(!timer){
var timer:Timer=new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,playStop);
timer.start();
}
frame 2 :
removeChild(textfield1);
addChild(textfield2);
textfield2.x = tfPosition.x;
textfield2.y = tfPosition.y;
textfield2.text="goodbye";
stop();
Example 2
If you have two instances of a TextField manually placed on the Timeline, that you select "use device fonts" and the names for the instances are really called "textfield1" on the first frame and "textfield2" on the second frame.
(I've done it by copy and paste in place and didn't get any issue neither).
This works on CS6 too... The text is well displayed
So give us more details please.
Instance of TextField on frame 1 named "textfield1"
code on frame 1 :
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
textfield1.text = "hello";
stop();
function playStop(te:TimerEvent):void {
play();
}
if (! timer)
{
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,playStop);
timer.start();
}
Instance of TextField on frame 2 named "textfield2"
code on frame 2 :
textfield2.text="goodbye";
stop();

can't add an event listener to a mask AS3 Flash

New to AS3. Trying to do a simple mask exercise, but for some reason when I add event listener to 'myMask', the event doesn't trigger. I tried turning both 'myMask' and 'theMaskee' as sprites and movie clips, but no luck. It does trigger if I don't assign 'myMask' as a mask to 'theMaskee'. It also works if I add the listener directly to the stage, but eventually I want to put many things on the stage, and I'm afraid there will be conflict if it has to listen to the same event but perform several functions... especially if I need them one at a time. I looked through textbooks and the API and mask-related questions other people had, but I can't find anything relating to my specific situation.
(this code is written directly in the timeline)
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.sampler.NewObjectSample;
import flash.events.MouseEvent;
var temp = new backGround();
var myBG:Bitmap = new Bitmap(temp);
temp = new splashMaskee();
var theMaskee:Bitmap = new Bitmap(temp);
var myMask = new MovieClip();
myMask.graphics.beginFill(0x000000, 0);
myMask.graphics.drawRect(0, 0, 800, 600);
myMask.graphics.endFill();
myMask.cacheAsBitmap = true;
theMaskee.cacheAsBitmap = true;
theMaskee.mask = myMask;
addChild(myBG);
addChild(theMaskee);
addChild(myMask);
myMask.addEventListener(MouseEvent.CLICK, myMaskClick);
//stage.addEventListener(MouseEvent.CLICK, myMaskClick);
function myMaskClick(e:MouseEvent):void
{
trace("click");
myMask.graphics.beginFill(0x000000, 1);
myMask.graphics.drawCircle(mouseX, mouseY, 30);
}
Thank you for taking the time
You need to add the listener to theMaskee instead, not your mask.
The mask in AS3 does not implement IEventDispatcher therefore can not catch and dispatch events.
Do this:
theMaskee.addEventListener(MouseEvent.CLICK, myMaskClick);
And it should work. :)
Masks dont take any mouse/keyboard events as it is just a mask and not actually present in the display list.

move a Flash object forth and back along the X axis with AS3

I am creating a flash animation with a flash object that moves along the X axis back and forth. I would like to move the object for example from position x=10 to position x=100 then stay in position x=100 for 2 seconds and go back with the same motion characteristics to the initial position. I am using Tween Classes, that let me change eases and so on, i am also using the Tween yoyo method. The problem is that I don't know how to make the object stay for some seconds in the second position (before going back to the initial position) and I don't know how to stop the yoyo event. I want the object to go back and forth and then stay in the initial position but with the yoyo it keeps on repeating (i supose that i should not use the yoyo method...). I attach you the code I have so far:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(rectangle, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
myTween.yoyo();
}
You should set a timeout in the onFinish method. The code should look like this:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(rectangle, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
var delay:int = 1000;
setTimeout(delay,reverse_tween); // delay - delay in milliseconds
}
function reverse_tween():void{
myTween.yoyo();
myTween.removeEventListener(TweenEvent.MOTION_FINISH, onFinish);
}
Hope it helps

Scrollbar not working with TLF text with dynamic content in ActionScript 3

I'm creating a simple Flash project with Flash CS5 and ActionScript 3.
What I want to do is that I want to dynamically update a TLF text container with given source and destination, something like loadData(text_placeX, "markup.xml"); anywhere that I want.
It's working like a charm, but the problem is I can't use any scroll-bar for my text. I have added a UIScrollBar to text container and it's working with the default text that I've putted into text container, but when I update container with my data it's not working. What am I missing?
Another question is that how I can empty my text container before loading new data in it?
My code is:
import fl.text.TLFTextField;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.text.TextFieldAutoSize;
import flashx.textLayout.container.ContainerController;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.conversion.TextConverter;
import fl.controls.ScrollBar;
var ldr:URLLoader = new URLLoader();
var flow:TextFlow = new TextFlow();
function loadData(text_place, fileURL:String):void {
text_place.border = true;
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.addEventListener(Event.COMPLETE, function(evt:Event){ ldr_complete(text_place) }, false, 0, true);
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}
function ldr_complete(text_place:TLFTextField):void {
ldr.removeEventListener(Event.COMPLETE, ldr_complete);
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
initText(text_place, ldr.data);
}
function loadError(e:IOErrorEvent):void {
trace("Error loading an external file. The server may be busy. Try refreshing the page.");
}
function initText(text_place:TLFTextField, fileContent):void {
flow = TextConverter.importToFlow(fileContent, TextConverter.TEXT_FIELD_HTML_FORMAT);
flow.flowComposer.addController(new ContainerController(text_place, text_place.width, text_place.height));
flow.flowComposer.updateAllControllers();
}
UPDATE: When I skip using of initText function contents and instead I use text_place.tlfMarkup = fileContent; it works; but my option on TextFlow is missing. And also I was missing "update scrollbar" after putting content in text-field.
I think this line may be the problem:
ldr.addEventListener(Event.COMPLETE, function(evt:Event){ ldr_complete(text_place) }, false, 0, true);
You have an anonymous function here (function(evt:Event){...) which passes the object text_place to the function ldr_complete(). However, you do not have access to text_place, since it is a variable declared within a different scope. If you make the function into a named one, you will not assume that you have that access. E.g.,
function loadCompleteHnd(evt:Event):void{
[...]
}
However, you still have to gain access to the object in text_place. So, you can make text_place a class-level (global) variable, and set that variable whenever. But that may run the risk of creating a race condition -- if you have a slow load, you might be trying change that object from two places at once.
Another option is to create an entirely new Event which extends the Event.COMPLETE setup. At that point, you can create another parameter for the Event.COMPLETE listener to use. This is complicated, and a bit of a learning curve, but it makes events more versatile.
In either case, you may want to set up a flag that tells you whether something else is editing the same object. It's not foolproof, but it can save some headaches.