Flixel Forums

development => help => Topic started by: mightiest_hero on Sat, Jul 30, 2011

Title: Witch one is better
Post by: mightiest_hero on Sat, Jul 30, 2011
I want to make object that has different sprite, width, and height.
Witch is better make 1 class that load graphic when change type or make different class?
Title: Re: Witch one is better
Post by: Arkeus on Sat, Jul 30, 2011
I do this by calling loadGraphic when I need it to change. That way everything else about the object (hp, position, velocity, etc) stays the same, if you need it to.
Title: Re: Witch one is better
Post by: alejoamiras on Sat, Jul 30, 2011
I do this by calling loadGraphic when I need it to change. That way everything else about the object (hp, position, velocity, etc) stays the same, if you need it to.

I agree, just make a function like

Code: [Select]
private function loadProperties(Type:String):void{
          switch(Type){
          case "Red":
          loadGraphic(etc,etc,etc,etc,etc);
          width =etc;
          heigth = etc;
          break;
          }
}

Lol and that's it xD
Title: Re: Witch one is better
Post by: mightiest_hero on Sun, Jul 31, 2011
do you have any reason why? witch one better in performance when there are an event to change object type? thanks in advance
Title: Re: Witch one is better
Post by: zadvornykh on Sun, Jul 31, 2011
If you want to change the graphic and size on the fly (after you create the object using new ...), it would be better to add a method to do so, like suggested above.

If you only need to configure the object once (at the time of creation), it will make no difference at all. In that case, I would create a base class which holds all the logic common to the different types, and have one method which configures the object, which I override in each sub class. I believe this leads to cleaner code... but that is just my preference.
Title: Re: Witch one is better
Post by: mightiest_hero on Sun, Jul 31, 2011
okay thank you guys for the input
Title: Re: Witch one is better
Post by: ExpertNovice on Mon, Aug 1, 2011
I'm no pro, but from what I understand unless your doing something frequently - like every update - you don't have to worry so much about performance.  The thing gets redrawn every time it moves so switching the graphic isn't such a burden.

I could be mistaken, however.

If your changing one object into another, it depends on what information you want to keep.

If it has variables, I'd assume it's much more burdensome to send all of that information into a new object, delete the old one, and draw the new one.  You could just draw the old one with a new sprite.

If most things are going to change during the switch, create the new, give it any needed old information (location?), and then make sure to delete the old one.

Worry about performance on things that occur during update() and draw().  Set ups and the occasional call won't drop the framerate for more than a second even if they are huge.  Like if your checking every sprites location 60x/sec that might slow you down a bit.

Again I'M NO PRO.  I'm just telling you what I assume to be true.
Title: Re: Witch one is better
Post by: mightiest_hero on Mon, Aug 1, 2011
okay thanks for your input. i use object that changed when player move to other area so it change to other object like tree, boulder i just need to change position, size and graphic. they all have same other attribute
Title: Re: Witch one is better
Post by: ExpertNovice on Mon, Aug 1, 2011
Oh okay.  Yeah those are pretty simple objects i'm guessing.

How often will the player be changing areas?

Say your at an Oasis in the desert.  Then you wander into the desert wilderness.  There are no trees here.
If create something named "Tree" you don't want to change it into a rock.  That would be confusing in your code's readability.
I'd just delete the old and make new.
If your doing something where it's like an "Obstacle" that's okay.  It could be anything, it's generic.  It's perfectly okay to change a sprite, bounding box, etc on the fly.  Just keep it below ~once per second.

Changing one tree into another I kind of get, but I'd still just delete and remake.

What you should be worried about is that you will be able to quickly edit and understand your code later on.
Avoid errors.  Keep your code readable.  It'll save you hours of struggling.

What happens if zone 1 has 4 rocks and zone 2 has 3 palm trees?  what happens to the fourth rock?  Do you delete it?  Then what happens when you go back?  Its gone, you can't change it's position or it's sprite.  You have to remake it either way.

Were you planning on hiding it?  Now /that/ would be burdensome.  (Flixel can have hundreds at any time ;] )

Now if you have 100 trees and 50 rocks per scene, you probably don't want to remake those all at once.
I'm betting Flixel could handle it, however.


Avoid errors before they happen, take the microscopic performance hit.
(Unless the performance hit isn't microscopic, of course.)
Title: Re: Witch one is better
Post by: mightiest_hero on Mon, Aug 1, 2011
okay thanks for the advise. i'm planning to recycle the object. if not used i kill the object and revive the object when i need it