Smart way to execute function in flash from an imported html (or XML) plain text
Ok, here is the think. I have a very simple flash program code in AS3, inside this program there is a dynamic textfield html capable. Also i have a database were i will put some information.
Each element in the database can be linked to other. For example, this database will contain tourist spots, and they can be related with others in the same database.
Ramdom place 1
This interesting spot is near <link to="ramdo2">ramdom place2</link>. And so, so so...
The information in the database is retrived by 开发者_如何学Gothe flash application and it shows the text on the dynamic textfield. I need somehow to tell my flash application that have to be a link to other part of the database, so when the user click the link a function in flash call this new data.
The database is a simple Mysql server, and the data is not yet in there, waiting for your suggestions of how to format it and develop the solution.
Im a php developer too, so i can make a gateway in PHP to read the MySQL and then retrive some format to flash, an XML for example.
check text events in flash : http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html scroll the page till events and look at the link event , it will help you trigger an event when you click a html like then you have to write an event handler to handle actions
Here's what I'd do:
Gather all your data together:
$data=array();
while($row=mysql_fetch_row($rlt))
$data[$row['id']]=$row;
printf('<script type="text/javascript">function getData(){return %s}</script>',
json_encode($data));
If you need data from multiple tables (ie one for rows and one for relations), just vary the code above so all the info you need is encapsulated in a single array.
Then in your AS3, get the data using flash.external.ExternalInterface
:
data=ExternalInterface.call("getData");
Trace data
to see what you have to work with. It'll be your PHP array in dot formation, just like JS.
This method is better than using XML in every way - less processing, simpler to code, less bandwidth etc.
Edit Apparently this app doesn't run in a browser. In which case, this solution is useless. Back to plan A - use XML:
header('Content-Type: application/xml');
$out=new SimpleXMLElement('<response/>');
...
die($out->asXML());
There are plenty of tuts online explaining how to use XML in AS3.
There are several ways to do this. I would suggest one of these two:
1: Add buttons to the sprite containing your TextField at the exact position of the text.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
public class Test extends MovieClip
{
private var textField : TextField;
private function onButtonClick (ev:Event) : void {
trace (ev.target.name);
}
public function Test ()
{
textField = new TextField();
textField.multiline = true;
textField.autoSize = "left",
textField.wordWrap = false;
addChild (textField);
var tx:String = "<p>Ramdom place 1 \n"+
"This interesting spot is near <link to=\"ramdo2\">ramdom place2</link>. And so, so so...</p>";
var xml : XML = new XML(tx);
textField.text = tx.replace (new RegExp (/(<[^(><.)]+>)/gm), "");
for each ( var link:XML in xml.link)
{
var linkText : String = link.text( );
var startIndex : int = textField.text.indexOf( linkText );
var endIndex : int = startIndex + linkText.length-1;
var startBox : Rectangle = textField.getCharBoundaries( startIndex );
var endBox : Rectangle = textField.getCharBoundaries( endIndex );
var button:Sprite = new Sprite();
button.graphics.beginFill(0,.1);
button.graphics.drawRect (startBox.x, startBox.y,(endBox.x - startBox.x)+endBox.width, (endBox.y-startBox.y)+endBox.height);
button.graphics.endFill();
button.name = link.@to;
button.addEventListener (MouseEvent.CLICK, onButtonClick);
button.buttonMode = true;
addChild (button);
}
}
}
}
( I realize this is very dirty, but you get the idea )
2: replace <link>
with actual <a>
links and use TextEvent.LINK to pass an argument:
package
{
import flash.display.MovieClip;
import flash.events.TextEvent;
import flash.text.TextField;
public class Test extends MovieClip
{
private var textField : TextField;
private function onTextEvent (ev:TextEvent) : void {
trace (ev.text);
}
public function Test ()
{
textField = new TextField();
textField.multiline = true;
textField.autoSize = "left",
textField.wordWrap = false;
addChild (textField);
var tx:String = "<p>Ramdom place 1 \n"+
"This interesting spot is near <link to=\"ramdo2\">ramdom place2</link>. And so, so so...</p>";
textField.htmlText = tx.replace ("<link", "<a").replace ("/link>", "/a>").replace ("to=\"", "href=\"event:");
textField.addEventListener (TextEvent.LINK, onTextEvent);
}
}
}
精彩评论