Author Topic: Witch one is better  (Read 1393 times)

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Witch one is better
« 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?

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Witch one is better
« Reply #1 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.

alejoamiras

  • Member
  • **
  • Posts: 63
  • Karma: +0/-1
    • View Profile
Re: Witch one is better
« Reply #2 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

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Witch one is better
« Reply #3 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
« Last Edit: Sun, Jul 31, 2011 by mightiest_hero »

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: Witch one is better
« Reply #4 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.

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Witch one is better
« Reply #5 on: Sun, Jul 31, 2011 »
okay thank you guys for the input

ExpertNovice

  • Active Member
  • ***
  • Posts: 124
  • Karma: +0/-0
  • Now we're cookin' with bacon!
    • View Profile
Re: Witch one is better
« Reply #6 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.

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Witch one is better
« Reply #7 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

ExpertNovice

  • Active Member
  • ***
  • Posts: 124
  • Karma: +0/-0
  • Now we're cookin' with bacon!
    • View Profile
Re: Witch one is better
« Reply #8 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.)

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Witch one is better
« Reply #9 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