So I’m in the final stages of designing a game for the iPhone using Flash CS5 and I’m running into a problem with it running on the iPhone. I am a registered Developer, so I’m doing this completely legit… I’m just doing it on a PC and with Flash so there are a few workarounds being done lol. I’m at a point where it runs perfectly in the simulator but not on the iPhone itself. I load up the game and the opening screen loads up where I have my PLAY button located. The problem is, it doesn’t work. It doesn’t click, change screens… nada. So I worked it out to be something I’m doing wrong with the way I’m using the Filesystem and my save game file. If I take out the filestream stuff and just put in hard values into all my save game variables, the game runs just fine. The PLAY button does its thing and the game is good to go, except of course that I’m stuck to just the hard set values instead of being able to do little things like change levels. So here is the code I’m using, and hopefully someone can see where I’ve gone wrong.
The app starts off with running a main.as which calls the saved game file…
private function addedMain(event:Event):void {
//Set up opening screen
gameStart = new GameStart(this);
addChild(gameStart);
//set up the save game info
_savedGame = new SaveGameFile();
_savedGame.initGame();
}
gameStart runs a simple script that just has the title screen with a PLAY button. Clicking the PLAY button removes it and loads up the next screen. This is what isn’t working right now. The system may be just freezing or something, at this point I don’t have a way to tell exactly what’s happening except that the PLAY button does nothing. So the next thing called is the saved game file itself:
package {
import flash.events.Event;
import flash.display.MovieClip;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.errors.IOError;
public class SaveGameFile extends MovieClip {
private var file:File;
private var savedGame:XML;
public function SaveGameFile() {
}
public function initGame():void {
file = File.applicationStorageDirectory;
file = file.resolvePath("savedGame.xml");
xmlLoad();
initVariables();
}
private function xmlLoad():void {
var fileStream:FileStream = new FileStream();
try {
fileStream.open(file, FileMode.READ);
} catch(e:IOError) {
createSaveFile();
xmlSave();
}
var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
savedGame = XML(str);
fileStream.close();
}
public function saveTheGame():void {
xmlSave();
}
private function xmlSave():void {
var writeStream:FileStream = new FileStream();
writeStream.open(file, FileMode.WRITE);
var s:String = String(savedGame);
writeStream.writeMultiByte(s, File.systemCharset);
writeStream.close();
}
private function createSaveFile():void {
savedGame =
<savedGame>
<levelsCompleted>0</levelsCompleted>
<levelWorkingOn>0</levelWorkingOn>
<volLevel>0.05</volLevel>
<sfxLevel>0.75</sfxLevel>
<level>
<levelNum>1</levelNum>
<highScore>0</highScore> //...etc.
</level>
//you get the idea
</savedGame>
}
So as you can see, I’m creating a XML file called savedGame (created the first time the game runs and then is just loaded when the game runs) and from that I’m pulling all my level information as needed and saving scores and such. Again this is working just fine in the simulator (of course) but if I leave this code in, I don’t get past the starting page. So does anything jump out to anyone as to why this isn’t working? Help! Thanks 😉
UPDATE: So I may have been on the wrong track all along because through messing around with my file on my computer I was finally able to see the error occur where I could easily trace problems compared to tracing on the iPhone. I don’t know if my original code was flawed or not, however, I do know that this code below works perfectly, so I’m keeping it (and I like it better anyway). HOWEVER, my main problem seems to have been the fact that I’m using XML as my save file. This is perfectly fine to do, BUT for some reason, saving the file causes a couple of strange characters to be entered at the very beginning of the file. So instead of starting
it was starting with something like
The | was actually a symbol I couldn’t find that had a line in the middle and a charCode of 25 and the Y was actually a Yen sign. So whatever that was, it was causing me to get a 1088 Error. I solved this by striping any and all characters (whatever they end up being just in case it’s different for everyone… not sure if it is but not taking any chances). So in my code below I added a section between //EDIT START and //EDIT STOP that is only needed if you too are using XML in your script. Otherwise you can leave this out. Hope this helps 🙂
ORIGINAL POST: OK so it seems that through some messing around with the code listed at the bottom of the page linked by danii, I was able to get that sites code working after all. I’m tempted to look back at my original code and mess with it to see if I can get it to work with what I’ve learned from this, but the game is working so I’m moving on and never looking back… lol. What I did find out though is that for my needs, that pages code was not quite workable (and why I gave up on it originally). It might have worked for their needs, but unless you package the game with a file, as far as I can tell their function “getSaveStream” will always return ‘null’ because the file will never be able to be written. So I removed a lot of their if’s and try’s because they were causing the game to fail. Here is what I ended up with in the context of my original post:
So I’m happy to say it tested perfectly and is now running and saving information like it is supposed to. Hope this is of use to someone who runs into the same issue I did. Happy coding!