Creating an ActionScript 3 RSS looping feed - actionscript-3

I have the following AS3 code so far. What I need to do is have this code continually loop the feed it is consuming. Any solutions?
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.events.IOErrorEvent;
//Read RSS feeds
var RSS_xmlData: XML = new XML();
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://www.oshawa.ca/news_rss.asp"));
function LoadXML(e:Event):void {
dtext.text="Loading...";
RSS_xmlData = new XML(e.target.data);
}
function pullFeed(rss: XML):void {
var str: String="";
str = rss.channel.item.title;
str = str.replace(/\s*\n/g," | ");
str = str.replace(/'/g,"\"");
//// shows specific entry
var items: Array = new Array();
items = str.split("|");
var tf: TextField = dtext;
var i:Number=0;
var myTimer:Timer = new Timer(4000,items.length);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
tf.text = items[i].toString();
scaleTextToFitInTextField(tf);
i++;
}
myTimer.start();
}
function scaleTextToFitInTextField(txt: TextField):void {
var f: TextFormat = txt.getTextFormat();
f.size = (txt.width > txt.height) ? txt.width : txt.height;
txt.setTextFormat(f);
while (txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 6) {
f.size = int(f.size) - 1;
txt.setTextFormat(f);
}
}
function onIOError(e:IOErrorEvent):void
{
trace(e.toString());
dtext.text="Finding Feed...";
}
I have tried a while loop, a for loop and a timer reset and restart but none appear to enable me to continually loop the feed.
Thanks

Reset your counter when you've finished iterating over your array:
function timerListener (e:TimerEvent):void{
tf.text = items[i].toString();
scaleTextToFitInTextField(tf);
// Reset counter to 0 when we've been all the way through the array
i = i < items.length - 1 ? i + 1 : 0;
}

Related

ActionScript RSS Title issue

The below AS# RSS reader code pull the titles from the RSS items but seem to include the XML markup as well. How does one not include the XML markup without using Regex or string replace?
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.events.IOErrorEvent;
//Read RSS feeds
var RSS_xmlData: XML = new XML();
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://www.oshawa.ca/news_rss.asp"));
function LoadXML(e:Event):void {
dtext.text="Loading...";
RSS_xmlData = new XML(e.target.data);
pullFeed(RSS_xmlData);
}
function pullFeed(rss: XML):void {
var str: String="";
str = rss.channel.item.title;
str = str.replace(/\s*\n/g," | ");
//str = str.replace(/'/g,"\"");
//// shows specific entry
var items: Array = new Array();
items = str.split("|");
var tf: TextField = dtext;
var i:Number=0;
var myTimer:Timer = new Timer(4000,1000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
tf.text = items[i].toString();
scaleTextToFitInTextField(tf);
i = i < items.length - 1 ? i + 1 : 0;
}
myTimer.start();
}
function scaleTextToFitInTextField(txt: TextField):void {
var f: TextFormat = txt.getTextFormat();
f.size = (txt.width > txt.height) ? txt.width : txt.height;
txt.setTextFormat(f);
while (txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 6) {
f.size = int(f.size) - 1;
txt.setTextFormat(f);
}
}
function onIOError(e:IOErrorEvent):void
{
trace(e.toString());
dtext.text="Finding Feed...";
}
Thanks for any help with this.
The reason you're getting the XML markup in there is because the value returned by rss.channel.item.title is of type XMLList, corresponding to all the <title> nodes matched by that selection - rather than just the text content of those nodes. As you've noted, it's rather backwards to convert that to a String and then strip out the extraneous mark-up manually.
I would iterate over all the <item> nodes and add their <title> content to the array as you go along. The new pullFeed method would then look like:
function pullFeed(rss: XML):void {
var items:Array = new Array(); //Declare Array of titles
var numItems:int = rss.channel.item.length(); //Capture number of <item> nodes
for (var i:int = 0; i < numItems; i++) {
items.push(String(rss.channel.item[i].title)); //Add each <item>'s <title> to the Array
}
var tf: TextField = dtext;
var i:Number=0;
var myTimer:Timer = new Timer(4000,1000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
tf.text = items[i];
scaleTextToFitInTextField(tf);
i = i < items.length - 1 ? i + 1 : 0;
}
myTimer.start();
}
One other benefit of iterating through the XML nodes this way is that you could easily capture the <description> or <pubDate> content of each <item> on the same pass through the XML, if you planned to make use of that later.

Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<com.clark::searchVO1>$ to __AS3__.vec.Vector.<com.clark::searchVO1>

I want to first start off asking if anyone has any suggestions of any resource(books or examples) on parsing data/or Sessioning variables in AS3. I researched for some resources but those books or sites, does not really cover the topics i mentioned.
I am trying to parse some JSON data from php into AS3 VO, then the VO into Vector file, the Vector File put the data into boxes, and that sits inside another as file for display. The boxes split the results into specific data. For example Box 1(ID1, Name1, Location1). Box 2(ID2,Name2,Location2). When the specific box is hit, the box will session the ID of that Box(listing), then parse it so it goes to the next AS file(to pull out the details from database) to display the details of that specific Listing.
With the help from other post&poster, i managed to get to parsing the JSON back, but i have 2 errors, because i don't know which Valid vector i need to put as the parameter. And can i have some idea/advice on how i should create the "Sessioning ID" when the box is clicked?
Error #1034: Type Coercion failed: cannot convert
AS3.vec::Vector.$ to AS3.vec.Vector..
At this line var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>); in the file sresultnologin
below.
Error #1069: Property 0 not found on com.clark.SearchVectorTest and there is no default value. Starting from this line
mySearchVector[i].nobed = searchVOs[i].nobed; in the searchVO1 file
below Thanks for your time. SearchVO1
package com.clark
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
import flash.utils.*;
import flash.sampler.NewObjectSample;
public class searchVO1 extends MovieClip {
private var Arvariables:URLVariables;
private var SrSend:URLRequest;
private var SaLoader:URLLoader;
public var nobed:String;
public var zip:String;
public var Location:String;
public var price:String;
public var callMethod:Function;
public var s1:searchpage = new searchpage ();
public function searchVO1():void{
addEventListener(Event.ADDED_TO_STAGE, onadded); // init
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
s1.searchs.addEventListener(MouseEvent.CLICK, ValidateAndsearch);
// Build the varSend variable
SrSend = new URLRequest("http://localhost/search.php");
SrSend.method = URLRequestMethod.POST;
Arvariables = new URLVariables;
SrSend.data = Arvariables;
SaLoader = new URLLoader();
SaLoader.dataFormat = URLLoaderDataFormat.TEXT;
SaLoader.addEventListener(Event.COMPLETE,Asandler);
// private methods
function Asandler(event:Event):void{
// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray :Array = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;
// create vector of SearchVO
var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>();
// loop the result array
for( var i:int = 0; i<len; i++ )
{
searchVOs[i] = new searchVO1();
searchVOs[i].nobed = resultArray[i].nobed;
searchVOs[i].zip = resultArray[i].zip;
searchVOs[i].Location = resultArray[i].Location;
searchVOs[i].price = resultArray[i].price;
}
// call a function to create your boxes
// or maybe create your SearchVector class and pass it your search vector
var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs);
for( var i:int = 0; i<len; i++ )
mySearchVector[i].nobed = searchVOs[i].nobed;
mySearchVector[i].zip = searchVOs[i].zip;
mySearchVector[i].Location = searchVOs[i].Location;
mySearchVector[i].price = searchVOs[i].price;
}
}
}
public function searchVOs( nobed:String, zip:String, location:String, price:String )
{
this.nobed = nobed;
this.zip = zip;
this.Location = Location;
this.price = price;
}
public function ValidateAndsearch (event:MouseEvent):void {
// validate fields
Arvariables.nobed = s1.nobed.text;
Arvariables.zip = s1.zip.text;
Arvariables.Location = s1.Location.text;
Arvariables.price = s1.price.text;
SaLoader.load(SrSend);
var s7:sresultnologin = new sresultnologin()
removeChild(s1);
addChild(s7);
}
}
}
SearchVectorTest
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
public class SearchVectorTest extends MovieClip
{
public function SearchVectorTest(test:Vector.<searchVO1>)
{
super();
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
}
var currentY:int = 270;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing5 = new Listing5();
Bolder.x=80;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf1.x = (Bolder.height-bf.height)*.5
bf3.x = (Bolder.height-bf.height)*.5
bf.x = (Bolder.height-bf.height)*.5
bf.y = (Bolder.height-bf.height)*.15
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
Bolder.y = currentY;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}
sresultnologin
package com.clark
{
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
public class sresultnologin extends MovieClip {
public var s1:Searchreult = new Searchreult ();
public function sresultnologin(){
// init
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
}
var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>);
addChild (s3);
}
}
}
While your mySearchVector Object isn't added to the display list, all it's content cannot be displayed ...
try it, it must work:
function Asandler(event:Event):void
{
// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray :Array = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;
// create vector of SearchVO
var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>();
// loop the result array
for( var i:int = 0; i<len; i++ )
{
searchVOs[i] = new searchVO1();
searchVOs[i].nobed = resultArray[i].nobed;
searchVOs[i].zip = resultArray[i].zip;
searchVOs[i].Location = resultArray[i].Location;
searchVOs[i].price = resultArray[i].price;
}
// create your SearchVector class and pass it your search vector
var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs);
// add your searchVector to the display list
addChild( mySearchVector );
// REMOVE THIS BLOCK !!
/*
for( var i:int = 0; i<len; i++ )
mySearchVector[i].nobed = searchVOs[i].nobed;
mySearchVector[i].zip = searchVOs[i].zip;
mySearchVector[i].Location = searchVOs[i].Location;
mySearchVector[i].price = searchVOs[i].price;
}
*/
}
var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>); is you sending a reference to a class not an instance of an object of that type.
Change to either
var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>());
or
var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>([]));

How do i pass String from Php to VO file to another .as file in AS3?

Hi I am pretty new to AS3, i am trying to parse some data from php to a VO file, and then transfer the string of data into another .as file where it will put the data into boxes. I am stuck in how do i parse the data from the VO file into the other .as File(Pulling the data from php into BookVO, then parsing BookVO to VectorTest). I tried tracing the data in BookVO, it works ok, but i can't get the data from BookVO to VectorTest.
Please help, thanks
BookVO.as
package com.clark
{
import flash.display.*;
import flash.net.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
public class BookVO
{
public var nobed1:String;
public var LoZip1:String;
public var rangelow1:String;
public var rangehigh1:String;
public var Bend:URLRequest;
public var variabless:URLVariables;
public var nLoader:URLLoader;
public function BookVO() {
Bend = new URLRequest("http://localhost/Autoresult.php");
Bend.method = URLRequestMethod.POST;
variabless = new URLVariables();
Bend.data = variabless;
nLoader = new URLLoader();
nLoader.dataFormat = URLLoaderDataFormat.TEXT;
nLoader.addEventListener(Event.COMPLETE,Jandler);
nLoader.load(Bend);
// handler for the PHP script completion and return of status
function Jandler(event:Event) {
var responseVariables: URLVariables = new URLVariables(event.target.data);
this.nobed1 = responseVariables.nobed1 ;
this.LoZip1 = responseVariables.LoZip1;
this.rangelow1 = responseVariables.rangelow1;
this.rangehigh1 = responseVariables.rangehigh1;
}
}
}
}
VectorTest.as
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
public class VectorTest extends MovieClip
{
public function VectorTest()
{
super();
var books:Vector.<BookVO> = new Vector.<BookVO>();
for (var i:int = 0; i < length; i++)
{
var book:BookVO = new BookVO();
book.nobed1 = "nobed1";
book.LoZip1 ="LoZip1";
book.rangelow1 = "rangelow1";
book.rangehigh1 ="rangehigh1";
books.push(book);
}
for (var j:int = 0; j < books.length; j++)
{
trace("Test", j, "has a name of", books[j].nobed1);
trace("Test", j, "Zip", books[j].LoZip1);
trace("Test", j, "ranglow", books[j].rangelow1);
trace("Test", j, "rangehigh", books[j].rangehigh1);
books[j].nobed1;
books[j].LoZip1;
books[j].rangelow1;
books[j].rangehigh1;
}
var currentY:int = 270;
for (var k:int = 0; k < books.length; k++)
{
var Bolder:Listing2 = new Listing2();
Bolder.x=80;
var tf:TextField = new TextField();
var tf1:TextField = new TextField();
tf1.width = 100;
var tf2:TextField = new TextField();
tf2.width = 100;
tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
tf.width = 100;
tf.autoSize = TextFieldAutoSize.CENTER;
tf1.width = 100;
tf1.autoSize = TextFieldAutoSize.CENTER;
tf2.autoSize = TextFieldAutoSize.CENTER;
tf2.width = 100;
tf1.y= tf.height+5;
// Pulling the textfields content out from the current bookVO
tf2.text = books[k].nobed1;
tf1.text = books[k].rangelow1;
tf.text = books[k].rangehigh1;
tf1.x = (Bolder.height-tf.height)*.5
tf2.x = (Bolder.height-tf.height)*.5
tf.x = (Bolder.height-tf.height)*.5
tf.y = (Bolder.height-tf.height)*.15
Bolder.addChild(tf);
Bolder.addChild(tf1);
Bolder.addChild(tf2);
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}
First off, good job on your classes. Much better than most people who say they're new to AS3. ;)
That said...
In VectorTest, you're calling super() on a MovieClip. You don't need this.
If you're not using the timeline of a MovieClip (which, I'd recommend not using anyway), extend Sprite instead; it's lightweight as it doesn't carry the timeline baggage MovieClip does.
Your first for loop in VectorTest Constructor loops to length... of no array.
You're overwriting the properties of BookVO in that loop, which should be populated by your URLLoader. This is superfluous.
Your second loop references the 4 properties of BookVO, neither reporting or setting the variables (ie., books[j].nobed1;). Might want to remove that.
In BookVO, you've written a nested function. Unless you're dealing with a massive number of variables and have issues with variable scope, don't do it. As it stands, the only variables Jandler() is accessing are already Class level globals.
Loading data is an asynchronous operation, meaning, data doesn't populate instantly (LoadRequest > Wait > ProgressEvent > Wait > LoadComplete). Because you're both instantiating BookVO and reading the properties in the same function, all you'll get are null properties. Unlike the VectorTest constructor, because Jandler() is called on Event.COMPLETE (asynchronously), it will have access to the variables you're looking for.
Try this instead...
You'll still need to address the length of how many books you want to instantiate, however, I've split out your reading of the properties from the constructor, and added a reference to the method to call when the loading is complete.
It will print out all the variables, and if it is the last book in your Vector, it'll call finish() which... um... does the rest of what you were doing. :)
-Cheers
BookVO Updated 2013.11.07 # 12:30 AM
package com.clark{
import flash.display.*;
import flash.net.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
public class BookVO {
public var nobed1:String;
public var LoZip1:String;
public var rangelow1:String;
public var rangehigh1:String;
public var Bend:URLRequest;
public var variabless:URLVariables;
public var nLoader:URLLoader;
private var callMethod:Function;
public var data:Object;
public function BookVO(listener:Function = null) {
Bend = new URLRequest("http://localhost/Autoresult.php");
Bend.method = URLRequestMethod.POST;
variabless = new URLVariables();
Bend.data = variabless;
nLoader = new URLLoader();
nLoader.dataFormat = URLLoaderDataFormat.TEXT;
nLoader.addEventListener(Event.COMPLETE,Jandler);
nLoader.load(Bend);
if (listener != null) {
callMethod = listener;
}
}
public function Jandler(event:Event) {
// handler for the PHP script completion and return of status
var responseVariables:URLVariables = new URLVariables(event.target.data);
data = event.target.data;
report(data);
if (callMethod != null) {
callMethod(this);
}
}
private function report(obj:*, prefix:String = ""):void {
for (var k in obj) {
var type:String = getType(obj[k]);
if (type == "Array" || type == "Object" || type == "Vector") {
trace(prefix + k + ": (" + type + ") ¬")
report(obj[k], prefix + " ")
} else {
trace(prefix + k + ":" + obj[k] + " (" + type + ")")
}
}
}
private function getType(value:*):String {
// Returns the class name of object passed to it.
var msg:String = flash.utils.getQualifiedClassName(value);
if (msg.lastIndexOf("::") != -1) {msg = msg.split("::")[1];}
return msg;
}
}
}
VectorTest
package com.clark {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
public class VectorTest extends MovieClip {
public var books:Vector.<BookVO>;
public function VectorTest() {
books = new Vector.<BookVO>();
for (var i:int = 0; i < length; i++) {
var book:BookVO = new BookVO(response);
books.push(book);
}
}
private function response(book:BookVO):void {
trace("Name: ", book.nobed1);
trace("Zip: ", book.LoZip1);
trace("ranglow: ", book.rangelow1);
trace("rangehigh: ", book.rangehigh1);
// call finish() if this is the last book.
if (books.indexOf(book) == books.length - 1) {
finish();
}
}
private function finish():void {
var currentY:int = 270;
for (var k:int = 0; k < books.length; k++) {
var Bolder:Listing2 = new Listing2();
Bolder.x=80;
var tf:TextField = new TextField();
var tf1:TextField = new TextField();
tf1.width = 100;
var tf2:TextField = new TextField();
tf2.width = 100;
tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
tf.width = 100;
tf.autoSize = TextFieldAutoSize.CENTER;
tf1.width = 100;
tf1.autoSize = TextFieldAutoSize.CENTER;
tf2.autoSize = TextFieldAutoSize.CENTER;
tf2.width = 100;
tf1.y = tf.height+5;
// Pulling the textfields content out from the current bookVO
tf2.text = books[k].nobed1;
tf1.text = books[k].rangelow1;
tf.text = books[k].rangehigh1;
tf1.x = (Bolder.height-tf.height)*.5
tf2.x = (Bolder.height-tf.height)*.5
tf.x = (Bolder.height-tf.height)*.5
tf.y = (Bolder.height-tf.height)*.15
Bolder.addChild(tf);
Bolder.addChild(tf1);
Bolder.addChild(tf2);
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}

flash actionscript error 5007 while file is not linked or included in any way

I created an xfl file called myfile.xfl and linked it to Main.as.
Because my script is kind of extensive, I planned to distribute it over several as-files.
The Main.as looks like this and is linked correctly from the xfl-file:
package {
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;
import flash.events.Event;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.text.*;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Main extends MovieClip
{
var bestand:String;
var titelFont:Font = new Titelfont();
var werkFont:Font = new Werkfont();
var titelfmt:TextFormat = new TextFormat();
var werkfmt:TextFormat = new TextFormat();
var inputfmt:TextFormat = new TextFormat();
var snd:Sound;
var cnl:SoundChannel = new SoundChannel();
var xmlLoader:URLLoader;
var myList:XML;
var introClip:Containerclip = new Containerclip();
var dicteeClip:Containerclip = new Containerclip();
public function Main()
{
//DE INFORMATIE UIT DE URL //
//bestand = LoaderInfo(this.root.loaderInfo).parameters["bestand"];
//bestand += ".xml";
bestand = "xml/bron.xml"
loadXml();
}
function loadXml():void
{
xmlLoader = new URLLoader(new URLRequest(bestand));
xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
}
function xmlLoaded(event:Event):void
{
myList = new XML(event.target.data);
myList.ignoreWhite = true;
createTextFormat();
createIntro();
createDictee();
createResultaten();
}
include "CreateStuff.as"
include "Dictee.as"
}
}
You can see my includes at the end: createstuff.as and dictee.as.
I was working on a third include, called Sound.as, and tried to preview my file when error 5007 popped up. "5007: An ActionScript file must have at least one externally visible definition"
I removed the include Sound.as line from my Main.as, thinking that must have cause the problem for some reason. When I republished the error popped up again, and flash automatically reopened the Sound.as file. While there is no link from anywhere to it anymore.
My other files are CreateStuff.as :
function createTextFormat() : void {
titelfmt.size = 32;
titelfmt.color = 0x003399;
titelfmt.font = titelFont.fontName;
werkfmt.size = 24;
werkfmt.color = 0x000000;
werkfmt.font = werkFont.fontName;
inputfmt.size = 32;
inputfmt.color = 0x333333;
inputfmt.font = werkFont.fontName;
//inputfmt.
}
function createTextfield (fmt) : TextField {
var tf:TextField = new TextField();
tf.defaultTextFormat = fmt;
if (fmt == titelfmt) {
tf.multiline = false;
tf.autoSize = "center";
tf.selectable = false;
tf.y = 10;
tf.x = stage.stageWidth / 2;
} else if (fmt == werkfmt) {
tf.autoSize = "left";
tf.multiline = true;
} else if (fmt == inputfmt) {
tf.multiline = true;
tf.width = 300;
tf.height = 40;
tf.border = true;
tf.borderColor = 0xCCCCCC;
tf.type = TextFieldType.INPUT;
}
return (tf);
}
function createIntro() : void {
introClip.x = 0;
introClip.y = 0;
introClip.name = "introClip";
var nttf:TextField = createTextfield(titelfmt);
nttf.text = "Instructie"
introClip.addChild(nttf);
var itf:TextField = createTextfield(werkfmt);
itf.text = "Wat weet jij nog?\nLuister naar de woorden.\nSchrijf ze op.";
itf.width = 300;
itf.x = stage.stageWidth / 2 - 150;
itf.y = 120;
itf.selectable = false;
introClip.addChild(itf);
var startBtn:Beginknop = new Beginknop();
startBtn.x = stage.stageWidth / 2 - startBtn.width / 2;
startBtn.y = 300;
startBtn.addEventListener(MouseEvent.CLICK, showDictee);
introClip.addChild(startBtn);
addChild(introClip);
}
function createResultaten() : void {
}
And Dictee.as :
function createDictee () : void {
dicteeClip.x = 0;
dicteeClip.y = 0;
var nttf:TextField = createTextfield(titelfmt);
nttf.text = "Dictee"
dicteeClip.addChild(nttf);
var iptf:TextField = createTextfield(inputfmt);
iptf.x = 135;
iptf.y = 205;
dicteeClip.addChild(iptf)
var dicteeSpeaker:Luidspreker = new Luidspreker();
dicteeSpeaker.x = stage.stageWidth / 2 - dicteeSpeaker.width/2;
dicteeSpeaker.y = iptf.y - dicteeSpeaker.height - 40;
//dicteeSpeaker.addEventListener (MouseEvent.CLICK, playSnd);
dicteeSpeaker.name = dicteeSpeaker;
dicteeClip.addChild(dicteeSpeaker);
var volgendeKnop:Controleknop = new Controleknop();
volgendeKnop.x = iptf.x + iptf.width + 30;
volgendeKnop.y = iptf.y;
dicteeClip.addChild(volgendeKnop);
}
function showDictee(event:MouseEvent) : void {
addChild(dicteeClip);
removeChild(introClip);
speelDictee();
}
function speelDictee() : void {
//bepaal geluid
//afspelen geluid
//volgende knop-actie
}
I initially called the file Sound.as but thought the error might come from that Sound is a reserved word, so I changed it to Sounds.as
I'm well out of my depth here and I have no clue why the error pops up. Or the file Sound.as while it is not linked.
My flash version is Flash-CC but the tag doesn't exist yet so I couldn't specify it in the tags.
#5007 is commonly caused by
Not correctly using a package
Including an import statement any level below the package level
I'm answering this question because for some reason the error disappeared and I have no idea why. I don't want to leave it open, I'd prefer to delete it as it's of no use to anyone but I'm not sure how to do that.

AS3: Issues with multiple save/load slots

I'm trying to take this very simple "game" and give it three save/load slots. Following a separate tutorial I can make it work with a single save slot but once I try adding more, it gives me the following error message.
1046:Type was not found or was not compile-time constant: save2.
1046:Type was not found or was not compile-time constant: save3.
I am new to actionscript 3 so I'm sure I'm being very newbish but I have tried to figure this out for quite some time now but just can't seem to. The whole thing is controlled by buttons already placed on the scene. I appreciate any help I can get.
The code:
import flash.net.SharedObject;
var saveDataObject:SharedObject;
var currentScore:Number = 0
init();
function init():void{
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save1);
btnSave1.addEventListener(MouseEvent.CLICK, saveData);
btnSave2.addEventListener(MouseEvent.CLICK, save2);
btnSave2.addEventListener(MouseEvent.CLICK, saveData);
btnSave3.addEventListener(MouseEvent.CLICK, save3);
btnSave3.addEventListener(MouseEvent.CLICK, saveData);
btnLoad1.addEventListener(MouseEvent.CLICK, save1);
btnLoad1.addEventListener(MouseEvent.CLICK, loadData);
btnLoad2.addEventListener(MouseEvent.CLICK, save2);
btnLoad2.addEventListener(MouseEvent.CLICK, loadData);
btnLoad3.addEventListener(MouseEvent.CLICK, save3);
btnLoad3.addEventListener(MouseEvent.CLICK, loadData);
}
function save1(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile1");
}
function save2(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile2");
}
function save3(e:MouseEvent):void{
saveDataObject = SharedObject.getLocal("savefile3");
}
function addScore(e:MouseEvent):void{
currentScore += 1;
updateScoreText();
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function loadData(e:MouseEvent):void{
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
I tried your code and it works like a charm...
Anyways, I've made a simpler version that doesn't use so many functions and Events.
Here is a pure AS3 version of it (just save it as Test.as3 and use as Document Class in Flash), but you can copy the content of the Test() method and paste in a action frame.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.SharedObject;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
/***** START: Faking buttons and field *****/
var txtScore:TextField = new TextField();
addChild(txtScore);
var btnAdd:Sprite = new Sprite();
var btnSave1:Sprite = new Sprite();
var btnSave2:Sprite = new Sprite();
var btnSave3:Sprite = new Sprite();
var btnLoad1:Sprite = new Sprite();
var btnLoad2:Sprite = new Sprite();
var btnLoad3:Sprite = new Sprite();
var items:Array = [btnAdd, null, btnSave1, btnSave2, btnSave3, null, btnLoad1, btnLoad2, btnLoad3];
for (var i:int = 0; i < items.length; ++i)
{
var item:Sprite = items[i];
if (item)
{
item.graphics.beginFill(Math.random() * 0xFFFFFF);
item.graphics.drawRect(0, 0, 100, 25);
item.graphics.endFill();
item.x = 25;
item.y = i * 30 + 25;
addChild(item);
}
}
/***** END: Faking buttons and field *****/
var saveDataObject:SharedObject;
var currentScore:Number = 0
btnAdd.addEventListener(MouseEvent.CLICK, addScore);
btnSave1.addEventListener(MouseEvent.CLICK, save);
btnSave2.addEventListener(MouseEvent.CLICK, save);
btnSave3.addEventListener(MouseEvent.CLICK, save);
btnLoad1.addEventListener(MouseEvent.CLICK, load);
btnLoad2.addEventListener(MouseEvent.CLICK, load);
btnLoad3.addEventListener(MouseEvent.CLICK, load);
function getLocal(target:Object):String
{
if (target == btnSave1 || target == btnLoad1)
{
return "savefile1";
}
else if (target == btnSave3 || target == btnLoad2)
{
return "savefile2";
}
else if (target == btnSave2 || target == btnLoad3)
{
return "savefile3";
}
}
function save(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
saveDataObject.data.savedScore = currentScore;
trace("Data Saved!");
saveDataObject.flush();
trace(saveDataObject.size);
}
function load(e:MouseEvent):void
{
var local:String = getLocal(e.target);
saveDataObject = SharedObject.getLocal(local);
currentScore = saveDataObject.data.savedScore;
updateScoreText();
trace("Data Loaded!");
}
function addScore(e:MouseEvent):void
{
currentScore += 1;
updateScoreText();
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore);
trace("Score text updated");
}
}
}
}