How to pass on data through navigateToURL? - actionscript-3

navigateToURL(new URLRequest("other.html"), "_self");
other.html has a swf embedded in it.
Is there a way I can pass on variables to the swf embedded in other.html.
The reason is that I need to tell the swf if the user has logged in or not, and if he has logged in, pass it the user details. Feel free to suggest alternative methods that might me more efficient.
My entire website is made in flash.
Thanks

haven't written flash in a long time, but assuming that '+' is the string concatenation method...
for GET variables i would just do:
var url:String = "other.html";
url += "?someParam=someValue";
url += "&someOtherParam=someOtherValue";
navigateToURL(new URLRequest(url), "_self");
which is effectively making your url:
"other.html?someParam=someValue&someOtherParam=someOtherValue"
Edit:
something like this to actually grab the query vars...
function loaderComplete(myEvent:Event)
{
var myQueryStrings=this.loaderInfo.parameters;
var someParamValue:String=myQueryStrings.someParam;
}
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

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.

AS3 navigateToURL only works when testing in Flash CC

I've tried searching stackoverflow to find an answer to this question but so far haven't found anything.
I have a swf file with several movie clips on the stage. When I've managed to create an event where the movie clips swap out to a new movie clip when the user hovers. The problem that I am having is when you click that new movie clip it's supposed to open a URL in a browser window. That happens when I test the movie within Flash CC but when I test it in a browser nothing happens.
Any help for this would be greatly appreciated. I apologize if this question has been asked elsewhere on the site but, as I said, I did a search earlier and didn't find anything.
import flash.events.MouseEvent;
addEventListener(MouseEvent.MOUSE_OUT, gobackAnimation);
function gobackAnimation(event:MouseEvent):void
{
MovieClip(parent).gotoAndStop(1)
}
addEventListener(MouseEvent.CLICK, openURL);
function openURL(event:MouseEvent):void
{
ExternalInterface.call("open", "URL I'm trying to open");
MovieClip(parent).gotoAndStop(1)
}
To open a URL you have to use the navigateToURL function as documented here.
It appears that you are using the following code to trigger a URL to open:
function openURL(event:MouseEvent):void
{
ExternalInterface.call("open", "URL I'm trying to open");
MovieClip(parent).gotoAndStop(1)
}
However, I don't know where or what this ExternalInterface is and how the call function is built.
If you want to open a URL though, you should be doing something along the following lines:
function openURL(event:MouseEvent):void
{
var myURL:String = "http://your.url.goes/here"; // set your url here
var window:String = "_blank"; // you can set this to be whatever you need based on how you want the window opened
var request:URLRequest = new URLRequest(myURL);
request.data = variables;
try {
navigateToURL(request,window);
}
catch (e:Error) {
// handle error here
}
MovieClip(parent).gotoAndStop(1)
}

How to use Remote Shared Objects

I'm trying to use a remote shared object to send data messages between the client and server using the sharedobject.send method for a chat application.
I am really having difficulty understanding the documentations. The code i attempted to use simply didn't work.
Can someone be kind enough to show me the relevant API calls? And, take a close inspection at my code and tell me were exactly i am going wrong?
Thank You.
Client-Side
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
var nc:NetConnection = new NetConnection();
nc.connect("rtmfp://fms/exampledomain");
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
function netHandler(event:NetStatusEvent):void {
if (event.info.code == "NetConnection.Connect.Success") {
trace("Your Connected");
//Creating the sharedobject
var my_so:SharedObject = SharedObject.getRemote("users", nc.uri, false);
my_so.connect(nc);
my_so.addEventListener(SyncEvent.SYNC, syncHandler);
my_so.setProperty("users");
my_so.setDirty("users");
function syncHandler(event:SyncEvent):void {
var changelist:Array = event.changeList;
}
my_so.send function sendMessage(str){
Server-Side
application.onConnect(clientObj)() {
application.acceptConnection(clientObj) {
trace("connected up");
}
var my_so = SharedObject.getRemote("users", false);
my_so.send("Hello user", "Its only a test");
}
After editing your question, it looks like some code is missing in your client-side sample.
From what I see, you don't need the client-side code for your example to work. You can call the send() method directly from the client. But to test whether it works or not, you will need to have two clients connected.
About your NetConnection or your SharedObject, make sure you add your event listeners before you call the connect() method.
If what you want to achieve is simply sharing data between clients, then you don't even need to use the send() method but simply set properties on your shared object. But be aware of the following:
my_so.setProperty("users");
is actually deleting the key users as it is the same as this:
my_so.setProperty("users", null);
Finally, you would need to call setDirty() in very specific cases, but not in this one. Have a look at this answer for a more detailed example on how to share data with SharedObjects

AS3: return array from event listener?

I have an event listener applied to an xml load and it currently traces out the values it grabs which is fine, but what I want it to do is return an array for me to use. I have the Array creation and return working from "LoadXML" (it returns the array) but I can't get this to work with an event listener.
The event listener runs the "LoadXML" function fine, but I have no idea how to take the returned array for use, this is an example of how my event listener works right now:
xmlLoader.addEventListener(Event.COMPLETE, LoadXML());
and my assumption of how I would take the array (this doesn't work):
var rArray:Array = xmlLoader.addEventListener(Event.COMPLETE, LoadXML());
so instead I tried the following:
xmlLoader.addEventListener(Event.COMPLETE, function():Array{
var rData:Array = LoadXML(datahere);
return rData;
}
but that doesn't worth either.
So: How do I return an array from an eventlistener? Thanks!
I think there is some confusion of how event listeners work. Actually, I'm surprised your not getting compile errors with your current code.
When adding an event listener, what you should be passing in is a reference to a function to be called at a later time. Then when that function gets called, it will pass an Event object with any retrived data for working with. Here is a example:
xmlLoader.addEventListener(Event.COMPLETE, handleLoadComplete/*Note: No brackets, this is a reference*/);
//will be called at a later time, not instantly.
function handleLoadComplete(e:Event):void {
var xml:XML = xmlLoader.data as XML;
//do what ever you want this the XML...
}
Hopefully that makes things clearer for you.
Happy Coding!
Why not just use a component-level object and set its value (xml contents in your LoadXML() method)?
var rArray:Array;
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
private function LoadXML(event:Event=null):void {
// set this.rArray in here...
}
It's possible to make returnedArray contain an array created by convertXML, but not in the way you're trying to do it. Flash simply doesn't work that way.
This is roughly what happen when you run the code from pastebin:
Start running function loadInformation()
var returnedArray:Array = loadinformation("http://website.com/xml.xml");
Tell Flash that when xmlLoader loads completely, it should run LoadXML()
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
Start loading an XML file
xmlLoader.load(new URLRequest(xmlurl));
Tell Flash what LoadXML() is (and convertXML())
function LoadXML(e:Event):void {...}
Stop running function loadInformation()
Flash goes off and does other stuff while waiting for the XML file to load
The XML file finishes loading. Flash calls LoadXML() like it was told to.
Note that LoadXML() is being called by Flash, not by loadInformation().
LoadXML() processes the XML file.
To get the converted array data, you need to do something like clownbaby's answer: set the value of returnedArray directly while inside LoadXML.
var returnedArray:Array;
loadinformation("http://website.com/xml.xml");
function loadinformation(xmlurl:String):Array{
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest(xmlurl));
}
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
returnedArray = convertXML(xmlData);
}
function convertXML(xml):Array{
// Does some stuff with the XML and returns an array
return rArray;
}

Reading in a File (AS3) and repeatedly/dyamically accessing the data

This may be a tired old question, but I have yet to find a good answer. Say for instance you have a class that reads in an xml file to get information such as a grocery store items, prices, etc. This class also allows you to retrieve the information about a grocery store item with a get() function.
var grocery:GroceryStore = new GroceryStore(); //create a class that
//reads in xml about
//grocery items
grocery.get("lettuce"); //get some data
In this scenario, I am constantly running into issues because the get() function is being called before the event that loads in the xml file. It wouldn't make sense to place the get() in the onLoad event for the xml file because I want it to be re-usable and dynamic. Also, AS3 doesn't have a wait() function so I can't stall until the file is loaded? Does anyone have an idea on how to read in a file and then be able to safely access the data dynamically and repeatedly? Hopefully this example and my question is thorough enough, if not let me know.
Thanks
You can use events - listen for the complete event to be dispatched.
Add the following code to GroceryStore class
//constructor or a load method
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest(xmlurl));
function onLoad(e:Event):void
{
//process xml here
dispatchEvent(e);
}
Now use it as:
var grocery:GroceryStore = new GroceryStore();
grocery.addEventListener(Event.COMPLETE, onGroceryLoad);
function onGroceryLoad(e:Event):void
{
grocery.get("lettuce");
}