5 Replies - 949 Views - Last Post: 08 July 2011 - 04:22 AM Rate Topic: -----

#1 amatzprogrammer   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

FLASH AND PHP

Posted 08 July 2011 - 12:20 AM

I am new in php. And we need to make a game using php. Is it possible to make a game using flash then associate/use it with php . And how? Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: FLASH AND PHP

#2 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: FLASH AND PHP

Posted 08 July 2011 - 12:58 AM

Yes, I've had a project like this.

It was insanely hard to do, I can't even imagine why you would need PHP in a flash game (except for high scores maybe), but to do it, look at this.
Was This Post Helpful? 0
  • +
  • -

#3 amatzprogrammer   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: FLASH AND PHP

Posted 08 July 2011 - 01:50 AM

Thanks. This will help a lot. :)
Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: FLASH AND PHP

Posted 08 July 2011 - 02:47 AM

Hey.

What creativecoding posted works for Actionscript 2, but for Actionscript 3 things work a little differently. It's more OOP.

You use the URLLoader and URLReqeust classes to send requests to URLs and to listen for the event callbacks. For example, this is a sort of template I've used to make database records provided by PHP in XML format available to my AS3 code without much effort:
package  
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	/**
	 * An example of how to load XML data from an external PHP script.
	 * @author Atli
	 */
	public class UserData extends EventDispatcher
	{
		/** The URL to the PHP script that will return the data. */
		protected static var SCRIPT_URL = "http://localhost/test/UserData.php";
		
		/* The user data we want to receive from the PHP script */
		public var id:Number;
		public var name:String;
		public var email:String;
		
		/**
		 * The constructor. You can pass it a user ID to start it loading
		 * right away, or else not and use the load methods.
		 * @param	userID The ID of a user to load
		 */
		public function UserData(userID:Number) 
		{
			if (userID is Number && userID > 0)
				loadByID(userID);
		}
		
		/**
		 * Load a user by it's ID.
		 * @param	userID The ID of the user to load.
		 */
		public function loadByID(userID:Number)
		{
			// Set up a URLLoader so that when the URLRequest below is
			// done, the events will trigger the callback functions
			// defined in this class.
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onloaderComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onloaderError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onloaderError);
			
			// Set up the URL request for the above loader, to load the
			// PHP script, passing the ID of the user as a URL variable.
			var request:URLRequest = new URLRequest(SCRIPT_URL);
			request.data = new URLVariables("id=" + userID + "&time=" + Number(new Date().getTime()));
			
			// Start loading the PHP script.
			loader.load(request);
		}
		
		/**
		 * Callback for when the PHP is successful.
		 */
		public function onloaderComplete(evt:Event)
		{
			// Turn the response from the PHP script into a XML
			// object. This requires that the PHP script returns
			// valid XML data!
			var xml:XML = new XML(evt.target.data);
			
			// Retrieve the data from the XML object.
			this.id = parseInt(xml.id);
			this.name = xml.name;
			this.email = xml.email;
			
			// Fire a COMPLETE event, to indicate the data is ready.
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/**
		 * Callback for when the PHP is unsuccessful.
		 */
		public function onloaderError(evt:Event)
		{
			// Just forward the error.
			dispatchEvent(evt);
		}
		
	}

}


It just expects PHP to take in the id as a GET parameter, and return a XML file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <id>1</id>
    <name>TheUsername</name>
    <email>[email protected]</email>
</root>


(I'll leave the implementation of that to your imagination.)

Then you can use that in your main AS3 code like so:
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	public class Foo extends Sprite
	{
		public function Foo()
		{
			// Create an new instance of our UserData class.
			var user:UserData = new UserData(5);
			
			// Listen for the COMPLETE event, to know when our data
			// is ready to be used.
			user.addEventListener(Event.COMPLETE, onUserLoaded);
		}
		
		/**
		 * Callback for when the user data is ready.
		 */
		private function onUserLoaded(evt:Event)
		{
			// Fetch the UserData instance we created above.
			var user:UserData = UserData(evt.target);
			
			// And do stuff with the data PHP gave us.
			trace("UserLoaded!");
			trace(" ID: " + user.id);
			trace(" Name: " + user.name);
			trace(" Email: " + user.email);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: FLASH AND PHP

Posted 08 July 2011 - 04:03 AM

Flash is a resource hog, and it's often used to turn your computer into an advertisement. That's why many people use software like Click2Flash. I suggest you do your project in PHP, HTML and Javascript unless you want people seeing and ignoring this:
;)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: FLASH AND PHP

Posted 08 July 2011 - 04:22 AM

View PostCTphpnwb, on 08 July 2011 - 11:03 AM, said:

Flash is a resource hog, and it's often used to turn your computer into an advertisement. That's why many people use software like Click2Flash.
I suggest you do your project in PHP, HTML and Javascript

Agreed. The FlashBlock extension is always the first thing I install on a new browser. Followed closely by AdBlock and Ghostery. (The number of trackers that follow you where ever you go is scary!)

If you can avoid using Flash, I would do so. But that's not to say it can't be useful when the occasion calls for it. Right now, Flash is probably the best way to create graphically heavy online games, but if it's just a text game that can easily be replicated with HTML and CSS, save your users the grief and use Javascript instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1