Be a Supporter!

Browser-based game

  • 359 Views
  • 3 Replies
New Topic Respond to this Topic
DarkScythes
DarkScythes
  • Member since: May. 20, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Browser-based game 2008-03-04 13:01:21 Reply

I'm not sure if this should be in the flash forums or not.. but it is related to php more, so I'm posting here.

I'm looking for a tutorial on php-actionscript(or mysql-actionscript) communication to make a browser game. I don't know if it's even possible.. but I'm looking anyways.

Any tuts people?
Or is it even possible to make php communicate with AS?

gumOnShoe
gumOnShoe
  • Member since: May. 29, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Browser-based game 2008-03-04 13:14:44 Reply

I'd assume that it's possible through XML. Flash has some ability to retrieve variables stored as HTML as far back as Flash MX 2004. I've never used the system, but if you make methods in your flash animation to interpret the xml and similarly in php, you'll be able to pull of what you want. I'm not sure how well it will work as far as timing, but you'll have to experiment to find out, or find someone who knows what they are talking about.


Newgrounds Anthology? 20,000 Word Max. [Submit]

Music? Click Sig:

BBS Signature
seel
seel
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 21
Musician
Response to Browser-based game 2008-03-04 14:19:21 Reply

At 3/4/08 01:01 PM, DarkScythes wrote: Any tuts people?

Yea, there's a tutorial on this in AS:Main.

Lurk moar.
BoneIdol
BoneIdol
  • Member since: Aug. 14, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Browser-based game 2008-03-04 14:43:07 Reply

Technically this is a flash problem, and I hate developing in flash, but I figure I could share my experience on this problem.

Warning: This is stuff I've used to do a job with a pretty bloody tight deadline; I give no guarantees that this is the best way to go about doing this and I have no formal training in developing in flash. All my flash expertise is from trial and (lots of) error.

Anyway, loading an xml file is dead easy. Just go:

var xmlstuff:XML = new XML(); //Create a new XML object;
xmlstuff.onLoad = function(success)
{
  //This function is called when the xml finishes loading, success is set to true if it's successful.
  if (success){
    play(); 
  }
}
xmlstuff.ignorewhitespace = true; //Don't forget this! Your life will be hell without it
xmlstuff.load('loadnews.php'); //This can be any file at all provided it is (or outputs) valid xml

You can also use the getBytesLoaded and getBytesTotal methods on an xml object when you're loading it. This is pretty neat for preloaders.

Right for the following examples we're using the xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
  <article date="1204658399" poster="BoneIdol" title="example news article"><![CDATA[
Text of the article goes here
]]>
  </article>
  <article date="1204658299" poster="BoneIdol" title="example news article 2"><![CDATA[
Text of the article goes here
]]>
  </article>
</news>

Once you have your xml loaded, we'll need to traverse through it to get any useful information out of it and into a format we can work with in flash. Let's start with traversing through our xml document. There's quite a few available and you should be able to look through the automatic code completion for them. It's pretty obvious what they do, just think of an xml document as being like a family tree. The 2 I found myself using most, however, are these 2:

  xmlstuff.firstChild; 
  xmlstuff.childNode[0]; //This is an array

ChildNode is an array so you can do a .length and pass it through a loop to go through all the child elements in your xml node. Below is an example of how to loop through every <a rticle> and get useful data from it:

  var newsArticles:XMLNode = xmlstuff.firstChild;
  var numArticles:Number = newsArticles.length;
  for (i = 0; i < numArticles; i++){
    article = newsArticles[i];
    txt = newsArticle.toString(); 
    attrs = newsArticle.attributes; //This gets the attributes of an xml node in this case the title, poster and date
    date = Number(attrs.date); //I've got stung here before, toString returns strings...
    title = attrs.title;
    poster = attrs.poster;
    //Now do your thing, duplicate movie clips, fill out text boxes etc. etc.
  }

Whenever you access an xml node, you generally get another xml node returned. Even if the node has no children, so you have to do .toString() to turn it into something you can work with. One of the downsides of this is that you will always get a string returned, which stung me really badly only a few days ago.

To access the attributes of an xml node, (title, date and posts for <a rticle>) just run the node through .attributes. Technically, this is supposed to return an array, but it actually returns an object. So you need to access your attributes by going xmlNode.attributes.attributeName; Bit of a pain really. You don't have to run these through .toString(), but they're returned as strings anyway, so be careful.

Once you have the xml data in a format you can work with in flash, you need to actually do something with it. I can't really offer a great deal of advice here besides that duplicateMovieClip() is your friend, and you'll probably be messing around with it for a while getting it to work, so get a fresh cup of tea or coffee before you get started.

Sorry if I didn't explain that too well, flash is not my strong suit and I don't understand actionscript in enough depth to really explain it well. Mostly, I'm just happy if I can get it working.


Sufficiently advanced incompetence is indistinguishable from malice.