save an object forever [AS2]

6 posts

Flag Post

Hello, guys, this is my first post, I hope that will be the first of many of them.
I’m making a platformer game and I need that when I take a object , it disappears forever, that is, to be recorded automatically the object’s unloadMovie status and, when I closing the swf file and reopen it, that object is gone.

var coinNVL1_so = SharedObject.getLocal("thecoins");
var coinNVL1_:Array = [[1], [1]];
var xPos = 275;
var yPos = 55;
for (i = 0; i < coinNVL1_.length; i++)
{
	//attach the container clip
	attachMovie("the_coin","coin" + i,i,{_x:xPos, _y:yPos});
	//increase the y postion each time
	yPos += this["coin" + i]._height + 5;
}



if (coinNVL1_so.data.waiting_to_take == undefined)
{
	coinNVL1_array = [[1], [1]];
}
else
{
	coinNVL1_array = coinNVL1_so.data.taken;
}
for (a = 0; a < coinNVL1_array.length; a++)
{
	this["_root.coinNVL1_" + (a + 1)] = coinNVL1_array[a][0];
	trace(coinNVL1_);
}

onEnterFrame = function ()
{
	if (_root.ball.hitTest(_root.coin0))
	{
		_root.coin0.nextFrame();
		_root.coinNVL1_so.data.waiting_to_take = 2;
	}
};

Thanks in advance!!

 
Flag Post

Not an AS3 programer, but if you don;t want an object to be loaded the next time, just don’t load it? Check before loading it to see if it should be loaded and voila?

 
Flag Post

Saveing a whole level can be very hard. You code a lot diffrent from me so i can’t quite understand it but what I would do (and what you probably have to some extent) is have an array that places the coins on each level and push coins into an array(or vector) called coinsOnThisLevel.

Lets say coinsOnThisLevel looks something like this when filled


[coin1,coin2,coin3]

Then you also add a second array called coinsThatGetRemoved. When first createing the level you push the number 1 into the coinsThatGetRemoved array and when you pick up a coin you run a test through coinsOnThisLevel array and when the coin that was picked up equals the coinsOnThisLevel[i] then that i position in the coinsThatGetRemoved array becomes 0.

So if you pick up coin2 you check through the coinsOnThisLevel array with a loop. When the loop equals 1 or coinsOnThisLevel1 it will = coin2 in the coinsOnThisLevel array and you then make coinsThatGetRemoved[i] or coinsThatGetRemoved1 = 0 (note the starge 1’s are a 1 in square brackets but konrgegate formats them like that)

Now when you close your game you save the data of the coinsThatGetRemoved array as an array in local data.

Now go back to the very first thing I was talking about when making the coinsOnThisLevel array. You simply add in a check against the local data in the coinsThatGetRemoved array. If the coinsThatGetRemoved = 0 then don’t add the coin to the stage else add the coin to the stage.

I don’t know if that makes sense but if you can’t figure it out from that in a day or two I can make you a simple file.

 
Flag Post

Got bored and did it.
Steps to make this work (because I don’t know how to upload a zip)
1. Make an object in a flash .fla file called Player with the instance name Player
2. Make an object in a flash .fla file called Coin with the instance name Coin
3. Click on the stage an in properties put the word Main into the section that says class
4. Make a new .as file and copy and paste in this code.

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.MovieClip;
	import flash.display.Shape;
	public class Main extends Sprite
	{
		// *** Saveing player Variables
		private var statsSaved:SharedObject;
		// *********************************************************** Keyboard
		private var upKeyIsDown:Boolean;
		private var downKeyIsDown:Boolean;
		private var leftKeyIsDown:Boolean;
		private var rightKeyIsDown:Boolean;
		private var upKeyBindVar:int = 87;
		private var leftKeyBindVar:int = 65;
		private var rightKeyBindVar:int = 68;
		private var downKeyBindVar:int = 83;
		// Player
		private var playerVar:Player;
		// Coins
		private var coinVar:Coin;
		private var coinArray:Array = new Array();
		private var coinsThatGetRemovedArray:Array = new Array();
		public function Main()
		{
			// constructor code
			stage.addEventListener(KeyboardEvent.KEY_UP,keyIsUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
			addEventListener(Event.ENTER_FRAME, onFrame);
			loadSaveStats();
			createCoins();
			createPlayer();
		}
		private function loadSaveStats():void
		{
			// Save Stats
			statsSaved = SharedObject.getLocal("kongHelp/kongHelpSaveFile","/");
			// If you want to reset the stats for testing just chace the verion numbers below the something diffrent like 1.01
			// Loads Stats If Saved
			if (statsSaved.data.versionNumber == 1.00)
			{
				coinsThatGetRemovedArray = statsSaved.data.coinsThatGetRemovedArray;
			}
			// Resets Stats If First Load
			if (statsSaved.data.versionNumber != 1.00)
			{
				statsSaved.data.versionNumber = 1.00;
				statsSaved.data.coinsThatGetRemovedArray = new Array();
				coinsThatGetRemovedArray = [];
			}
			statsSaved.close();
		}
		private function createCoins():void
		{
			// Adds Coins For The Level Normally (AKA First Load)
			if (coinsThatGetRemovedArray.length == 0)
			{
				// aCN = Add Coins Normally
				for (var aCN=0; aCN<5; aCN++)
				{
					coinVar = new Coin();
					coinVar.x = 75 + (aCN * 100);
					coinVar.y = 100;
					addChild(coinVar);
					coinArray.push(coinVar);
					coinsThatGetRemovedArray.push(1);
				}
				// Acess Stats;
				statsSaved = SharedObject.getLocal("kongHelp/kongHelpSaveFile","/");
				statsSaved.data.coinsThatGetRemovedArray = coinsThatGetRemovedArray;
				statsSaved.close();
			}
			else
			{
				// Adds Only Coins Not Collected Already (AKA If  A Coin Was Collected Before Then Make Sure It Does Not Reappear)
				if (coinsThatGetRemovedArray.length != 0)
				{
					// Acess Stats
					statsSaved = SharedObject.getLocal("kongHelp/kongHelpSaveFile","/");
					coinsThatGetRemovedArray = statsSaved.data.coinsThatGetRemovedArray;
					statsSaved.close();
					// aCBOS = Add Coins Based On Save;
					for (var aCBOS=0; aCBOS<5; aCBOS++)
					{
						if (coinsThatGetRemovedArray[aCBOS] == 0)
						{
							coinArray.push(null);
						}
						else
						{
							if (coinsThatGetRemovedArray[aCBOS] == 1)
							{
								coinVar = new Coin();
								coinVar.x = 75 + (aCBOS * 100);
								coinVar.y = 100;
								addChild(coinVar);
								coinArray.push(coinVar);
							}
						}
					}
				}
			}
		}
		// Creates Player
		private function createPlayer():void
		{
			playerVar = new Player();
			playerVar.x = 275;
			playerVar.y = 200;
			addChild(playerVar);
		}
		private function onFrame(e:Event):void
		{
			// Player Movement
			if (upKeyIsDown == true)
			{
				playerVar.y -=  5;
			}
			if (downKeyIsDown == true)
			{
				playerVar.y +=  5;
			}
			if (leftKeyIsDown == true)
			{
				playerVar.x -=  5;
			}
			if (rightKeyIsDown == true)
			{
				playerVar.x +=  5;
			}
			// cCVC = Check Coin Array Collision
			for (var cCVC=0; cCVC<coinArray.length; cCVC++)
			{
				// Check If Coin Is One Of The Coins That Exists
				if (coinArray[cCVC] != null)
				{
					// Checks If Player Hits A Coin
					if (playerVar.hitTestObject(MovieClip(coinArray[cCVC])))
					{
						// Removes The Coin
						coinArray[cCVC].parent.removeChild(coinArray[cCVC]);
						coinArray.splice(cCVC,1);
						// Updates The coinsThatGetRemovedArray To Remove The Coin
						coinsThatGetRemovedArray[cCVC] = 0;
						// Acess Stats
						statsSaved = SharedObject.getLocal("kongHelp/kongHelpSaveFile","/");
						statsSaved.data.coinsThatGetRemovedArray[cCVC] = coinsThatGetRemovedArray[cCVC];
						statsSaved.close();
						break;
					}
				}
			}
		}
		private function keyIsUp(e:KeyboardEvent):void
		{
			// Up Down Left Right W A S D
			if (e.keyCode == 38 || e.keyCode == upKeyBindVar)
			{
				upKeyIsDown = false;
			}
			if (e.keyCode == 40 || e.keyCode == downKeyBindVar)
			{
				downKeyIsDown = false;
			}
			if (e.keyCode == 37 || e.keyCode == leftKeyBindVar)
			{
				leftKeyIsDown = false;
			}
			if (e.keyCode == 39 || e.keyCode == rightKeyBindVar)
			{
				rightKeyIsDown = false;
			}
		}
		private function keyIsDown(e:KeyboardEvent):void
		{
			// Up Down Left Right W A S D
			if (e.keyCode == 38 || e.keyCode == upKeyBindVar)
			{
				upKeyIsDown = true;
			}
			if (e.keyCode == 40 || e.keyCode == downKeyBindVar)
			{
				downKeyIsDown = true;
			}
			if (e.keyCode == 37 || e.keyCode == leftKeyBindVar)
			{
				leftKeyIsDown = true;
			}
			if (e.keyCode == 39 || e.keyCode == rightKeyBindVar)
			{
				rightKeyIsDown = true;
			}
		}
	}
}

5. Save the .fla and .as file in the same location and test the movie.
6. Read through the code and try to understand how it works I commented it pretty heavily but if you have a question go ahead and ask.

 
Flag Post

Thank you very much for your time, man. But I forgot to say that I’m doing my code in AS2, not AS3. But I tried your flash file and I found a small bug in it. The two bugs say:

1. “Script-1, Line 275 1084: Syntax error: expecting identifier before rightparen.”
2. “Script-1, Line 277 1084: Syntax error: expecting rightparen leftbrace before.”

   But I do not understand AS3, but I will certainly be helpful your code.

This is the original code [AS2] which I gave a friend:

Code in the mainline:


coinNVL1_so = SharedObject.getLocal("theCoins");// I call to sharedobject
if (coinNVL1_so.data.coinsOnThisLevel == undefined) { /*if the coinsonthislevel are undefined*/
	coinNVL1_array = [[1], [1]];
} else coinNVL1_array = coinNVL1_so.data.coinsThatGetRemoved;
for (a=0; a<15; a++) {
	this["coinNVL1_"+(a+1)] = coinNVL1_array[a][0];
}

and this is the code in the coin object (object in stage):


onClipEvent (enterFrame) {
if (_root.coinNVL1_0 != 1) this.unloadMovie();
if (this.hitTest(_root.player) and _root.coinNVL1_0 == 1) _root.coinNVL1_0.data.coinsOnThisLevel = 2;
}

But don’t works anything.

 
Flag Post

You could not have found those errors (not bugs)

Becuase the copy paste code I gave you was 203 lines. There is no line 275 or 277.

Also stop using AS2 right now please! I’m sure every AS3 programmer on this forum will tell you the same (much like they told me for the year or two i was on this form using AS2) It is not worth your time every SECCOND spent programming in AS2 is corrupting your flash abilities further and further. Scrap your current project and begin attempt to re-do it in AS3. There are tonnes of tutorials for platform games in AS3.

I assure you the code I gave you works and I also assure you that if you continue with AS2 you will regret in on the very first day you begin to learn AS3 (and you probably will learn AS3 when you learn just how awesome it is.)

Good luck with you game – and if you want to see my code work then follow the steps in my previous post exactly.

Edit: Sorry I just attempted to copy paste the code and I noticed that Kongregate formatting put a space between each line and also copy pasteing the "" symbol doesn’t transfer to flash properly. If you PM me your email I can send you the >AS3< Source files and you can check em out.