Skip to main content
Inspiring
April 7, 2014
Answered

Am I making garbage?

  • April 7, 2014
  • 2 replies
  • 1033 views

Hi,

In certain frames of a MC (Sandal)  which I add it to another MC (gamelevel), I  run a hitTestObject between it (Sandal) and some other MCs (they are in bArray).

Here in the main class I make the sandal:

public function hitBySandal () {

            var sandalH:Sprite = new Sprite();

            var sandal:Sandal = new Sandal();

            sandal.scaleX = .5;

            sandal.scaleY = sandal.scaleX;

            sandalH.x = mouseX - sandalH.width;

            sandalH.y = mouseY - sandalH.height;

            sandalH.addChild (sandal);

            gamelevel.addChildAt (sandalH, bArray.length + 1);

        }

and here in some sandal's frames I coded:

var mc:MovieClip = MovieClip(this.root);   //this.root is my mainclass

addEventListener (Event.ENTER_FRAME, checkHit);

function checkHit (event:Event) {

    for (var i:int = 0; i < mc.bArray.length - 1; i++) {

        if (this.hitTestObject(mc.bArray)) {

            mc.kill (mc.bArray);

        }

    }

}

I add and remove this eventListener in some other frames where I need to hitTestObject.

1. I need to know if var mc:MovieClip = MovieClip(this.root) is just copying my main class that is a huge scene, and if yes, does it (the memory) completely vanish after removing the sandal?

2. Can I NOT make the mc above and instead work directly with this.root? (it knows this.root BUT not its variables as bArray)

For removing sandal and its holder I coded in its last frame:


stop ();

removeEventListener(Event.ENTER_FRAME, checkHit)

this.parent.parent.removeChild (parent);

So am I doing things right or just making garbage?

This topic has been closed for replies.
Correct answer kglad

mc is a reference to your main timeline.  it's not your main timeline.

that reference is lost when the movieclip that contains the reference is gc'd.

i wouldn't say you're making garbage, but you're  making a mess by having some class code and some timeline code.  although, if your only class code is your main class code, that's just a regular mess typical of novice actionscript coders.

2 replies

Amy Blankenship
Legend
April 7, 2014

innerUFOmaker wrote:

Hi,

In certain frames of a MC (Sandal)  which I add it to another MC (gamelevel), I  run a hitTestObject between it (Sandal) and some other MCs (they are in bArray).

Here in the main class I make the sandal:

public function hitBySandal () {

            var sandalH:Sprite = new Sprite();

            var sandal:Sandal = new Sandal();

            sandal.scaleX = .5;

            sandal.scaleY = sandal.scaleX;

            sandalH.x = mouseX - sandalH.width;

            sandalH.y = mouseY - sandalH.height;

            sandalH.addChild (sandal);

            gamelevel.addChildAt (sandalH, bArray.length + 1);

        }

and here in some sandal's frames I coded:

var mc:MovieClip = MovieClip(this.root);   //this.root is my mainclass

addEventListener (Event.ENTER_FRAME, checkHit);

function checkHit (event:Event) {

    for (var i:int = 0; i < mc.bArray.length - 1; i++) {

        if (this.hitTestObject(mc.bArray)) {

            mc.kill (mc.bArray);

        }

    }

}

I add and remove this eventListener in some other frames where I need to hitTestObject.

1. I need to know if var mc:MovieClip = MovieClip(this.root) is just copying my main class that is a huge scene, and if yes, does it (the memory) completely vanish after removing the sandal?

2. Can I NOT make the mc above and instead work directly with this.root? (it knows this.root BUT not its variables as bArray)

For removing sandal and its holder I coded in its last frame:


stop ();

removeEventListener(Event.ENTER_FRAME, checkHit)

this.parent.parent.removeChild (parent);

So am I doing things right or just making garbage?

I will take a much stronger position than kglad. If you want to be following good Object Oriented practices, then, yes, you are making garbage.

Here are some rules of Object-Oriented Programming that you are violating:

  1. Single Responsibility Principle: Children should only be responsible for themselves. As soon as they are responsible for their parents, they can't be used within parents that are built a different way. Also, the parents get very dependent on these specific children and it becomes really hard to make changes.
  2. Liskov Substitution: If you must know about the parent, you should be referring to it as a type of object that declares the properties and methods you'll be using as part of its Type definition.  This allows you to substitute another object of the same type that doesn't happen to be the parent at need (aka loose coupling).
  3. Dependency Inversion Principle: Again, if you're going to have a reference to a parent, it should be passed in rather than the child reaching out and grabbing it.

For more on these and other principles you should be aware of, see SOLID. You may also want to look at GRASP, because you're violating Indirection, High Cohesion, Information Expert, and Low Coupling (at least).

Here are some ways to fix this:

Instead of having the child perform the "hit by sandal" action, it should instead simply dispatch a custom event when it detects that this has happened. This can be either on the Display List on an EventDispatcher that is shared by key components of the system (aka an event bus). The parent (or something higher up the chain that properly has the responsibility to act on this event) can then take whatever actions are needed, including adding children to the gameLevel if that's needed. It's possible, though, that you're better off adding a "hit" state to whatever gets hit by the sandal and simply calling the hit() method on those instead.

As Kglad says, you're very likely causing memory leaks by listening to ENTER_FRAME to something that can get removed from the display list if you're not listening to REMOVED_FROM_STAGE and cleaning it up there. Listening to this event causes a direct reference from the stage to whatever function you're adding (and hence the object where that function is declared).

Information Expert says that since your root already knows about your Sandal instance, its own bArray, and its own kill() method, it is a much better candidate to be also carrying out the hit test, so you might want to completely remove any responsibility for knowing if Sandal has hit anything from Sandal and move it closer to Main. Then Sandal becomes a pure, "dumb" Display Class and it is then infinitely portable. You may want to consider adding a Controller in there somewhere to translate back and forth between your Model/Data layer and the View.

Inspiring
April 8, 2014

Thank you Amy, I just googled and read about them. you are talking principles while I'm totally in my dummy way, way away of principles:D

Btw, it was understandable and I'll consider things you mentioned (try to).

I'm gonna try to move the hit test closer to main and If i face an issue I'll ask about it here:)

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 7, 2014

mc is a reference to your main timeline.  it's not your main timeline.

that reference is lost when the movieclip that contains the reference is gc'd.

i wouldn't say you're making garbage, but you're  making a mess by having some class code and some timeline code.  although, if your only class code is your main class code, that's just a regular mess typical of novice actionscript coders.

Inspiring
April 7, 2014

Thanks kglad

No my main class in not the only one;

This sandal mc has a complicated hand made animation with any kind of tweens, in some of it's frames it may hit the enemies. When I tried to addFrameScript instead of direct timeline coding there were issues i couldn't fix and also as my main class is soo huge i'd prefer to code less in it.

Dammit Dr., if i lived near u I'd pay like hell to have ur help on my game:))

kglad
Community Expert
Community Expert
April 7, 2014

i just posted this for someone that was running into a similar situation.  it's an excerpt from a book i wrote (Flash Game Development).  previous to these paragraphs i started coding for a game just the way someone with no formal training (but wanting to advance beyond timeline coding) would start; with code in a document class.  after starting and adding some complexity (and debugging problems), i've reached an inevitable (for any game/application with enough complexity) conclusion:

Everything works the way it should but there is a major problem. Actually, there is more than one major problem and there are some minor problems but I will address the biggest problem, first: my Main class is quickly becoming a mess.

I have all this code for controlling player in Main and I will need more code which will make Main an even bigger mess. In addition, I will need to add an introduction view (by view, I mean what is presented on-stage) to give some information about the game and I should probably allow users to customize the keys they use for movement. Not everyone likes using the arrow keys. (Heck, I do not like using the arrow keys.) In addition, I will need a game-over view, eventually.

I am heading for a major mess in Main. So, while I can continue to put all my coding in Main, that is just like putting all your code in frame 1 of the main timeline and that bypasses one of the biggest advantages of OOP, encapsulation.

p.s.  i've never met face-to-face with anyone for whom i coded.  all my coding work is via the internet using email (and rarely chat) to communicate.