actionscript 3.0 help

Miscellaneous Forums/General Discussion/actionscript 3.0 help

hi everyone, so im trying to fork with CustomClasses in flash. i now know how to make them, and create instances of them, but i cant figure out how to delete those instances with actionscript. for example, say i make a bullet class, and i want to delete the bullet when it hits something. does anyone know how to do this?

thnx in advance

*Flash* forums are your friend... http://www.kirupa.com/forum/showthread.php?p=1884719

i still don't get how to do it. the delete command doesnt seem to be doing anything.

From above forum, and it is valid for --ANY-- garbage collected language:


(Note: GC deletion from memory is not immediate.)



In your code, make sure that your bullet is no longer visible and becomes 'disabled' once it hits a target. You cannot expect the GC to remove it in the very moment you call delete(bullet); GCs work asynchronously from your own code, and delete() only marks an object for deletion, it does not actually delete it.

Hi Jerome,

Have you tried setting the instance to NULL?

ive tried typing, this = null, but i keep getting an error

Jermoe,

Have you created a class and then set a variable to reference the class?

Are you within the class and attempting to set it to NULL?

Can you explain your reason for using the [this] keyword and where you are using it, or show me a bit of your code where you are instantiating, using and attempting to delete the reference?

Thanks,

Kevin.

You have to set the instance variable to null. You can't set "this" to "null".

var Test:SomeClass = new SomeClass();
trace(Test); // --> [object SomeClass]
Test = null;
trace(Test); // --> null


You want to make another class that handles the bullets.

It will contain
- a static array
- a static method for adding a new bullet into the static array
- a static method for updating the bullets (loops thru the array and calls a method in the bullet class)
- a static method for removing a bullet instance from the static array (make the bullet instance in the array a null).

In the bullet class you will call the static method for remoting a bullet instance (pass "this" as parameter) when you want to delete the bullet.

hey kevin, im tryiing to delete the instance within the class, thats why i would type in this = null, or delete this, whichever im supposed to use.

Okay, Arctic Fox has covered what you need to do so if you can have a go at changing your code it will resolve your issue.
Any questions along the way please ask...

ok ill try it when i get a chance, thnx for all the help guys

hey guys, ive been trying to do what arcticfox explained, but im getting really confused about how to do it. here is the code i have for my DeleterClass:

package {
	
	import flash.display.MovieClip;
	
	public class DeleterClass extends MovieClip{
				
		public var arr:Array = new Array();
		
		public function DeleterClass() : void {

		}
		
		public function Add(addMC:MovieClip) :void {
			arr.push(addMC);
		}
		
		public function Remove(removeMC:MovieClip) :void {
			var i:Number;
			for(i=0;i<10;i++){
				if(i==removeMC){
					arr.splice(i);
					removeChild(removeMC);
					removeMC = null;
				}
			}
			}
		
	}

}


i keep getting errors when i run this, plus i dont even know if it is correct. alrighty, any help would be great.

Hard to say for sure without seeing the full code and knowing the error, but:
		public function Remove(removeMC:MovieClip) :void {
			for(var i:Number=0; i<arr.length; i++){
				if(arr[i]==removeMC){
					arr.splice(i);
					removeChild(removeMC);
					removeMC = null;
					return true;
				}
			}
			return false;
		}	


Here's a simple sample of how to do it. The sample consists of three files. BulletClass.as which is the code for a bullet itself. BulletManager.as with which you add and update bullets. Test.fla which creates a BulletManager and adds some bullets.

This is just a sample that shows the basic logic. In the game you would have a loop in which you call BulletManager.Update(). In the game you would add code in the BulletManager which removes nulled bullets from the array. And of course the BulletClass would contain some graphical elements. :)

BulletClass.as:
package  {
	import BulletManager;
	
	public class BulletClass {
		
		private var Distance:Number = 0;
		
		public function UpdateBullet():void {
			Distance += 1;
			trace("A bullet has flown for distance of",Distance);
			if (Distance >= 3) {
				trace("A bullet has flown far enough.");
				BulletManager.Remove(this);
			}
		}
		
	}
}


BulletManager.as:
package  {
	
	import BulletClass;
	
	public class BulletManager {
		
		public static var Bullets:Array = new Array();
		
		public static function Add():void {
			trace("BulletManager adds a new bullet.");
			Bullets.push(new BulletClass());
		}
		
		public static function Remove(pC):void {
			trace("BulletManager needs to remove a bullet...");
			for (var i:int = 0; i < Bullets.length; i++) {
				if (Bullets[i] != null) {
					if (Bullets[i] == pC) {		
						Bullets[i] = null;				
						trace("...and removed it!");
					}
				}
			}
		}
		
		public static function Update():void {
			trace("BulletManager starts updating bullets...");			
			for (var i:int = 0; i < Bullets.length; i++) {		
				if (Bullets[i] != null) {
					Bullets[i].UpdateBullet();
				}
			}			
			trace("BulletManager ends updating bullets.");
		}

	}
}


Code in Test.fla:
import BulletManager;
BulletManager.Add(); // fires a bullet
BulletManager.Update(); // updates all bullets
BulletManager.Add(); // fires another bullet
BulletManager.Add(); // fires yet another bullet
BulletManager.Update(); // updates all
BulletManager.Update(); 
BulletManager.Update();
BulletManager.Update();


When running Test.fla, the output should be:

BulletManager adds a new bullet.
BulletManager starts updating bullets...
A bullet has flown for distance of 1
BulletManager ends updating bullets.
BulletManager adds a new bullet.
BulletManager adds a new bullet.
BulletManager starts updating bullets...
A bullet has flown for distance of 2
A bullet has flown for distance of 1
A bullet has flown for distance of 1
BulletManager ends updating bullets.
BulletManager starts updating bullets...
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
A bullet has flown for distance of 2
A bullet has flown for distance of 2
BulletManager ends updating bullets.
BulletManager starts updating bullets...
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
A bullet has flown for distance of 3
A bullet has flown far enough.
BulletManager needs to remove a bullet...
...and removed it!
BulletManager ends updating bullets.
BulletManager starts updating bullets...
BulletManager ends updating bullets.


wow thanks arcticfox! ill test this out tonight if the homework is light!:)

hi arctic,
ok so i tried out your code and it worked out great, but im having a little trouble adding an actual bullet image to the code. could you show me how?
thnx