Flash Actionscript GetURL not working - actionscript-3

i´m making a banner for a webpage in flash. And i want to put buttons with links there.
So far i got it working with this script:
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
fisicleta.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = newURLRequest("url");
request.method = URLRequestMethod.GET;
var target:String = "_blank";
navigateToURL(request, target);
}
But his opens the link in a new tab, and i´m trying to keep it all in the same tab. I tried changing the target to "_self" but the link goes dead.
Thanks in advance!

You can use:
navigateToURL(new URLRequest("url"), "_top");
it will call your url from the window.top location and not from the current self location.

Related

Music is not Playing

Hi Iam trying to play sound i have been already inserted the media to library but it was not playing but here trace working...
Please Someone help me to find it out Thank you...
import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.MouseEvent;
stop();
var Myrequest:URLRequest = new URLRequest("hello.wav");
var mySound:Sound = new Sound();
mySound.load(Myrequest);
musicBtn.addEventListener(MouseEvent.CLICK, OnPlay);
function OnPlay(e:MouseEvent)
{
mySound.play();
trace("ok");
}

How to save input text as bitmap in as3? Can anyone please tell me why this code is not working?

1.How to save input text as bitmap in as3? Can anyone please tell me why this code is not working?
Some class files are imported here
import flash.text.TextField;
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PNGEncoderOptions;
import flash.utils.ByteArray;
import flash.net.FileReference;
import com.adobe.images.JPGEncoder;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
Code starts here
var bitmapdata: BitmapData = new BitmapData(200, 200);
bitmapdata.draw(txt);
var xmv: MovieClip = new MovieClip();
xmv.graphics.beginBitmapFill(bitmapdata);
xmv.graphics.drawRect(0, 0, 200, 300);
xmv.graphics.endFill();
var jpgEncoder: JPGEncoder = new JPGEncoder(100);
save.addEventListener(MouseEvent.CLICK, click)
function click(e:MouseEvent)
{
var imgByteData: ByteArray = jpgEncoder.encode(bitmapdata);
var file = new FileReference();
file.save(imgByteData, "Image.jpg");
}
Thank you all for your kind responses! I appreciate it a lot. There hasn't really been any issue with the code. After I replaced the 'txt' input box with a new one, it functioned properly. So the problem might have been with some of the changes I've made unknowingly to the input box. I even customized the code for user input to be displayed as bitmap. Thanx again!

How to set JPG type of image screenshot saved from flash using Actionscript 3?

I have a problem when i wanna save screenshot entire flash stage using ActionScript 3. when i click button that use function SimpanGbr it's works for saving image with name "NamaGambar" but without type of JPG image. How to save image with it's type. I use these codes:
stop();
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import com.adobe.images.JPGEncoder;
stage.displayState = StageDisplayState.FULL_SCREEN;
exit.addEventListener(MouseEvent.CLICK,keluar);
function keluar(event:MouseEvent):void
{
fscommand("quit");
}
function SimpanGbr(e:MouseEvent):void {
var qImageData:BitmapData = new BitmapData(800, 600);
qImageData.draw(stage);
var qEncoder:JPGEncoder = new JPGEncoder(100);
var qBytes:ByteArray = qEncoder.encode(qImageData);
var qFile:FileReference = new FileReference();
var nama:String="NamaGambar";
qFile.save(qBytes, nama+".jpg");
}
You can do so using this library.
After downloading copy the source files (the com folder) on to your Class path or your application root directory. Apart from the JPEGEncoder, this library is packaged with a lot of other classes related to Hasing, Text, Date etc.
This is how we save an image from a flash movie to a local machine.
1) Create a BitmapData object out of the MovieClip or the Loader.
2) Encode the BitmapData using the JPEGEncoder and form a ByteArray.
3) Use FileReference.save() to download the image on to the User machine.
See an example:
Create a blank Flash movie and name it as “ImageSave.fla”. In the same folder create “ImageSave.as” file and copy the below code in to it. You need the “com” folder copied from the as3corelib to this folder. I have a button on the stage named “save_mc” which triggers the saveImage function.
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.utils.ByteArray;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import com.adobe.images.JPGEncoder;
public class ImageSave extends MovieClip {
private var imgFile:URLRequest;
private var img:MovieClip;
private var imgLoader:Loader;
private var file:FileReference;
public function ImageSave() {
imgFile = new URLRequest("coffee.jpg"); //change it to your image path
imgLoader = new Loader();
imgLoader.load(imgFile);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
save_mc.addEventListener(MouseEvent.CLICK, saveImage);
}
private function onLoaded(evt:Event):void {
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
imgLoader.cacheAsBitmap = true;
addChild(imgLoader);
}
function saveImage(e:MouseEvent):void {
var myBitmapData:BitmapData = new BitmapData(imgLoader.width, imgLoader.height);
myBitmapData.draw(imgLoader);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var imgByteData:ByteArray = jpgEncoder.encode(myBitmapData);
file = new FileReference();
file.browse(new Array(new FileFilter("Images (*.jpg, *.jpeg)", "*.jpg;*.jpeg")));
file.save(imgByteData, "test.jpg");
}
}
}
For your convenience you can download a working copy of this sample here.

Flash/AS3 - preload or stream embedded sounds

I'm currently working on a project in flashdevelop and I want to include some music. I'm building a game, so multiple files are not an option. Currently all resources are embedded and a simple preloader loads everything (like the flashdevelop default preloader).
I don't want to load the music at the beginning, I'd rather like to stream it when required.
Is it possible to stream embedded sounds?
If not, is it possible to embed these files inside the .swf file and load them later on?
Thanks in advance!
You can do two things. One is to start loading the sounds after the initial loading finishes and save them in a Dictionary maybe. Second is to export a RSL (Runtime Shared Library) from Flash which is a SWF file which you can then load and have access to all the classes defined there.
In the first approach you basically load every sound like this and save them to dictionary:
import flash.media.Sound;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.Dictionary;
var mSounds:Dictionary = new Dictionary();
function loadSound(url:String, soundName:String)
{
var sound:Sound = new Sound();
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
sound.load(new URLRequest(url));
function onSoundLoadComplete(e:Event):void
{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
trace(soundName,"Sound Loaded");
mSounds[soundName] = sound; // save it to dictionary
// then you can load it from dictionary
// using the name you assigned
if(mSounds["crystalised"])
(mSounds["crystalised"] as Sound).play();
}
}
loadSound("C:\\Users\\Gio\\Desktop\\Crystalised.mp3", "crystalised");
In the second approach you have to do more steps, but you load it once. I'll list the steps here:
Make a new Flash Document (FLA)
Import all the sounds you need to the library
In the properties menu of each sound select the Actionscript tab and tick the Export for Runtime Sharing checkbox and fill in the name for output SWF
After you publish this FLA you can load it in your application or game and use it like this:
import flash.display.Loader;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.utils.getDefinitionByName;
function loadRSL(url:String):void
{
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onRSLLoadComplete);
loader.load(new URLRequest(url), context);
function onRSLLoadComplete(e:Event):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onRSLLoadComplete);
trace("RSL Loaded");
// creating a new instance of the sound which is defined in RSL
var soundClass:Class = getDefinitionByName("Crystalised") as Class;
var sound:Sound = (new soundClass() as Sound);
sound.play();
}
}
loadRSL("SoundLibrary.swf");

AS3 Undefined Method for button class

I'm trying to make a button link to an external page. In the first frame I placed this code:
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = new URLRequest("http://pruebainteligente.com");
request.method = URLRequestMethod.GET;
var target:String = "_blank";
navigateToURL(request, target);
}
To make the button class I went to the library > properties > And named the button Class "myButton".
What I'm doing wrong?
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
If that code is on the frame itself it is referring to an instance of an object/symbol, not the class itself. You will need to drag your button from the library and place it onto the stage, set its instance name (in the properties tab) to "myButton", then your code will work.
Alternatively you can add a copy of your object purely through code:
var newButton = new myButton();
addChild(newButton);
newButton.addEventListener(MouseEvent.CLICK, onMouseClick);