How to update a class based on another class field

Monkey Forums/Monkey Programming/How to update a class based on another class field

mteo77(Posted 2013) [#1]
Hello.
I am trying a way to do some "frozen" effect for my sprites.
At the moment i have an actor class and an effect class.
Actor is the main object, in this case is a moving sprite, and effect is a simple image that "should" follow the actor, with some transparency to make the actor appear frozen.
I suppose this can be considered a sort of "parent, child" relationship, but i haven't figured out how to tell the class effect (the child) to update it's coordinates based on the actor class (the parent).
I have added an extra field in actor of class effect like this:
Field myEffect:effects

The problem is how i can tell that my effect has to be on top of my actor only when the flag "frozen" is true?
Should i put a
myEffect=New effects

in the onupdate method of the actor after i checked for the flag?
Second question:
How do i set up the effect class OnUpdate method to follow the parent (actor) class coordinates?
Everything could be easily solved using a global class, but i really want to avoid doing that.
Hopefully i have explained the situation properly.
Thank you.


mteo77(Posted 2013) [#2]
I resolved using another flag called "isApplied" of type Bool.
So when my actor is frozen, it calls for the child class to be created and assign it the isApplied flag to true, so no more instances of the child class are created.
Then i manually modify the child x and y coordinates inside the parent OnUpdate method, based on if the child exist or not.
Not sure this is the right way to do it but it works..

But now i got another problem...my idea to create a frozen effect is pretty basic, it just doesn't look very good because all i am doing is overlay the parent with a shape that resembles it, and apply some alpha channel to the image to make it appear a bit frosty...
Is there a way to modify the image of the actor itself to appear frosty without applying another one on top?
Something like modifying the pixels of it?
(i hope it make sense, i don't think i can speak english properly today!)


muddy_shoes(Posted 2013) [#3]
Your child update solution is okay as long as you don't keep adding more effects/children. If you do it's likely to become unwieldy to add more and more flags.

In the long run you might want to look at creating a generic method for attaching child actors and make them as self-contained as possible (e.g. the child updates its position based on the parent rather than the parent updating the child).

As for your frozen effect, manipulating the pixels of the image is bound to be more awkward and slow than simply having a second "frosted" image or overlaying a frost effect image.


mteo77(Posted 2013) [#4]
Regarding the effects, yes you are absolutely right; i will see if i can find some examples to implement a "child, parent" class because at the moment i haven't got a clue.
Regarding the image, i have been toying with the idea of swapping the image, is it ok to do it during the OnUpdate?
Something like:
select state
     case normal
          image=loadimage("blabla.png")
     case frozen
          image=loadimage("blabla-frozen.png")
end select

I was wondering if swapping images during on update would cause slowdowns;
i am making sure my images are preloaded though.


Shinkiro1(Posted 2013) [#5]
Swapping images, assuming you are just swapping references will not slow down your game.
However, in the above code you load the images normally. Monkey isn't able to figure out you have already loaded them so it will load them again.

If you are sure you will never have more than 2 - 3 states your solution is ok.
Else you can lookup the strategy pattern, it's pretty useful in cases you have an unknown number of states to deal with.


Gerry Quinn(Posted 2013) [#6]
Just preload all the images and give them different names, or put them in an array or whatever. You shouldn't normally be using LoadImage except at (say) the start of a level.

Then your class instances can draw whatever image is appropriate at any time.

Imaging writing a font class... you wouldn't call LoadImage( "A.png" ) when you needed to write an A - you'd load all the letter images at the start and decide which image is the correct one to draw for the current letter!


mteo77(Posted 2013) [#7]
Sorry in my example above i was supposed to write:
image=blabla

Because the images were already preloaded..


mteo77(Posted 2013) [#8]
But i haven't thought of using an array for loading, thanks for pointing it out.
I think the better solution for me at the moment is to have an array of images of every actor, based on it's state.
Also i think i will do my image manipulating in a program, and swap the image in monkey instead of trying to apply effects on the fly...easier.