A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Removing class instances - suicide possible?

  1. #1
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107

    Removing class instances - suicide possible?

    Do you usually allow a mc class to kill itself, when it needs to be removed (alien shot, or something)? I mean, if the instance itself discovered that it needs to be removed?

    Right now, _root discovers that the instance must die, and removes it like this:
    Code:
    removeMovieClip(Evil[e]);
    Evil.splice (e,1);
    evilCount--;
    but it would be more convenient if the instance itself discovered that it was caught, hit by a bullet etc, so it can deal with a couple of frames of explosions etc first, then erase itself. Would you use something like the code above or is it neccesary to use an Event Dispatch that tells _root that it is finally time to be removed?
    I think Event Listeners are clumsy and I try to avoid them. (Do you use them a lot or do you have better options?)

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    events is the correct OOP approach to this problem. A CustomEvent is dispatched from the object saying that it died and any other object can listen for that event and deal with it specifically.

    but if its for 1 game. You can eliminate this by calling the required methods. So when an enemy/bullet dies just run something like:

    enemy.onBulletHit();
    bullet.destroy();
    _root.removeBullet();

    it all depends on how modular you want to be. I've found that I cannot create a base class for these types of objects because i'm always changing the behavior and hate when these objects are limited.

    so yeah, for a specific game I would suggest calling specific functions, its more efficient and sometimes easier. Things that should be modular imo are the data structures and geometry objects of your game.

    USE INTERFACES! they are the key to modular development. I can use a specific class and implement a CollisionObject interface and walla, my AwesomeCollisionEngine can accept the object.
    lather yourself up with soap - soap arcade

  3. #3
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    well, you could attach another explosion clip at the same coordinates and still remove the instance from the array.

  4. #4
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107

    Cow Icon

    Quote Originally Posted by mr_malee
    events is the correct OOP approach to this problem. A CustomEvent is dispatched from the object saying that it died and any other object can listen for that event and deal with it specifically.

    but if its for 1 game. You can eliminate this by calling the required methods. So when an enemy/bullet dies just run something like:

    enemy.onBulletHit();
    bullet.destroy();
    _root.removeBullet();

    it all depends on how modular you want to be. I've found that I cannot create a base class for these types of objects because i'm always changing the behavior and hate when these objects are limited.

    so yeah, for a specific game I would suggest calling specific functions, its more efficient and sometimes easier. Things that should be modular imo are the data structures and geometry objects of your game.
    Ok, I guess I might have to do it with events then. I don't understand how you mean I could do it another way... the problem with what you wrote here:
    bullet.destroy();
    _root.removeBullet();
    is that the bullet will not have time to execute the destroy() function (if if means something that takes some frames of time) since you remove it right away!

    I want something like this. My baddie gets killed. class code:
    Code:
    if (this.hitTest(_root.bulletX, _root.bulletY,true) and (!dying)) { 
       dying=true;
       gotoAndPlay (14 or whatever);
    };
    if (dying frame is the last one)) { 
       removeMe Event or if possible, suicide...
    }
    USE INTERFACES! they are the key to modular development. I can use a specific class and implement a CollisionObject interface and walla, my AwesomeCollisionEngine can accept the object.
    Looked up interfaces, but I still don't understand the point with them. Declaring in one file and implementing in another?...why?

  5. #5
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Quote Originally Posted by squidgie90
    well, you could attach another explosion clip at the same coordinates and still remove the instance from the array.
    That's interesting, but won't you have a similar problem with the explosion clip that needs to be removed after exploding? Especially if you want a specific explosion clip for each alien!

  6. #6
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    the way i do it, i just put a this.removeMovieClip() in the last frame of the explosion clip

  7. #7
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Quote Originally Posted by squidgie90
    the way i do it, i just put a this.removeMovieClip() in the last frame of the explosion clip
    Hey, that should work! Thanx! What stopped me from doing that was the fact that I held the movie clip in an array, and it would be hard to refer to that variable after removing it from the array. So, I could either do as you suggest, to replace the first mc with a mc that is NOT stored in a variable, or stop storing all my mc in variables and instead refer to them as eval(movieclipname +"number").
    OR, if an instance can somehow keep track of it's place in an array, I could write:

    Code:
    _root.Evil.splice (myArrayPosition,1);
    _root.evilCount--;	
    this.removeMovieClip();
    So, this raises the question to you people, do you usually refer to your movie clips as variables (in an array, for example) or to their string names?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center