Level: Beginner
Web Requests
Contents
-
Visiting a URL
-
HTTP Requests
-
Example: Loading levels from a server
-
Accessing Flash variables
Visiting a URL
Did you know that you could visit URLs from within a game? It’s as easy as using this block under Game > Web. The link will open up in your default browser inside a new tab.

What could you use this for?
-
Visiting Social Media (Facebook, Twitter)
-
Asking for a Paypal donation
-
Linking to your mobile game’s entry
Incidentally, we have pre-built behaviors for the first 2 - check them out under the “Utilities” category the next time you import a behavior for a scene.
Note: On mobile platforms, the default browser app will display. We’re working on an integrated in-app browser solution instead.
Note: Some pop-up blockers may prevent your game from opening a new tab.
HTTP Requests (GET / POST)
Your game can make HTTP GET and POST requests to remote sites and servers. This is great for fetching data from REST-based APIs or submitting new data.
Calls are asynchronous (non-blocking), and we callback by executing the body of the blocks when you receive a response.
Like the visit URL block, these are found under Game > Web.

Note: For POST requests, you can pass in multiple fields at once by separating them with ampersands (&), like this:
name=John&id=123456
In Populate, the developer used HTTP requests to implement a level sharing system.
Parsing XML and JSON-based responses is beyond the scope of this article, but check out our example, which uses HTTP GET to load in simple data from a remote server.
Example: Loading Levels from a Server
We’re building a simple game that stores its levels online. We want to create a level loader for this game that takes the data and creates actors based on the type and location.
Here's some sample data: http://dl.dropbox.com/u/2769678/level1.txt
This file contains just 3 entries, one line each. Name of the actor and then x,y location. Pretty simple.
robot,0,0
robot,128,128
hero,256,256
Here’s how we could parse this.

Moral of the story is this: keep your data formats simple, and you can achieve pretty cool things with web requests.
Accessing Flash Variables
Not only does Stencyl let you access data from remote sites, but using Flash variables, you can also access data from a Flash game's containing webpage. The following AS3 code snippet can be used in a Code block or a Code Mode Behavior to achieve this:
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(FlxG.stage.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
print(keyStr);
print(valueStr);
}
Summary
-
Visit URLs to point players to your social media pages, Paypal page or iOS game entry.
-
HTTP requests let you do just about anything. The “Populate” game for Flash and iOS uses HTTP requests to implement a level share system.
-
Flash variables can be used to communicate with a Flash game's containing webpage.
Last Updated: 2013-04-02 by Joe
8597 have read this article
Disclaimer: The Stencyl Team does not actively monitor comments on articles. If you're seeking help for your game,
please ask a question on the forums. Thanks!
Sign In to Comment