Tinkering: Trailing Fountain

I have been going back though the my favorite AS3 book, Keith PetersActionscript 3.0 Animation and decided to tinker with the Fountain example:

Source: Fountain.zip

7 Responses to 'Tinkering: Trailing Fountain'

  1. Jonathan says:

    Hi there, thank you so much for providing the fountain demo. I could not have wished for a better springboard to introduce me, as a complete beginner, to flash coding, and oops style.

    I have been modifying the program to learn how it works and have hit a wall on how to change the ball’s color! You’d think it would be easy, right, but I found I can only set the color when then ball is generated, with it’s contructor. I want to be able to change it every time it’s position is reset, for a visual effect I’m creating. The code b.color = 0×00ff0f; for example, doesn’t work! I cannot set the color in the same way I can set the vx and vy attributes and I don’t understand why! Can somebody please enlighten me?

  2. Hey Jonathan, nice to have another Jonathan out there :)

    The reason you can’t change the ball’s color after it has been constructed is because the the color property of the ball is private. If you go an edit the Ball class and create a setter function for the color, then you will be able to change its color. Without too much explanation of creating setters, just have a look at the methods relating to its other properties, (vx, vy, etc…) and create one more to modify the _color property.

  3. Jonathan says:

    Hi there… ok, I know this isn’t a general help page, so I hope this is [almost] ok…

    I went back to basics (as I’ve been playing with this program a while a changed many thing). I got the .zip afresh and just made these changes:

    To the Ball class I added a new setter, duplicating the format of the others thusly:

    public function set color ( color:uint ) : void { _color = color; };

    I put it on line 33 of the Ball class, alongside the others, and saved the file.

    Then I went to Fountain.as and, within the code which regenerates each Ball object at the mouse-point when it flies off the screen, I added this line:

    b.color = 0×0000FF;

    Which I assumed would just work… even though I don’t really understand all the underscore stuff in the Ball class properties.

    I saved the file and launched. I expected an initial explosion of colored Balls. After this I thought each Ball would be regenerated as a blue Ball with new x and y velocity components at the mouse pointer. The program launched with no errors, but nothing has changed. The program still does exactly what it did before even with my new apparent attribute change at line 62 of the Fountain code.

    What it seems is my setter function doesn’t do anything! This is giving me a headache. Can you see what’s wrong… the lines I have typed here are cut and paste from my source file and I’d be happy to post the code.

    Thank you very much for your rapid response!

    -Jonathan (yes, it’s an uncommon name, sometimes I wish I was called Steve or Chris or something common!) : )

  4. Jonathan says:

    Ok, hold up. With apologies for trolling up your web page I have found a solution:

    The b.color call to the set function works, but the set function, as written, doesn’t work as expected.

    I worked my first answer from the other set functions because they work ok. My understanding is weak at the moment but I believe the problem was something like the Ball class only draws the ball on initialisation, after that, it only moves it. Apparently the radius and color properties only affect the object when used with its own init() function, which is called Only when the ball is constructed!

    When I added an init() call to the color and radius setters, the ball color changed on cue and so my code behaved as I first hoped.

  5. yeah, i forgot to mention that you will need to either re-draw the ball ( init() ) or actually modify the color with something like this:

    <span class="source source_actionscript source_actionscript_3"><span class="meta meta_definition meta_definition_setter meta_definition_setter_actionscript meta_definition_setter_actionscript_3"><span class="storage storage_type storage_type_namespace storage_type_namespace_actionscript storage_type_namespace_actionscript_3">public</span> <span class="storage storage_type storage_type_function storage_type_function_actionscript storage_type_function_actionscript_3">function</span> <span class="storage storage_type storage_type_accessor storage_type_accessor_actionscript storage_type_accessor_actionscript_3">set</span> <span class="entity entity_name entity_name_function entity_name_function_actionscript entity_name_function_actionscript_3">color</span> <span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_actionscript punctuation_definition_parameters_begin_actionscript_3">(</span> color:<span class="support support_class support_class_actionscript support_class_actionscript_3">uin<span class="support support_class support_class_actionscript support_class_actionscript_3">t</span></span> <span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_end punctuation_definition_parameters_end_actionscript punctuation_definition_parameters_end_actionscript_3">)</span> : <span class="storage storage_type storage_type_return storage_type_return_actionscript storage_type_return_actionscript_3">void</span></span> 
    { 
        <span class="variable variable_language variable_language_private variable_language_private_actionscript variable_language_private_actionscript_3">_color</span> <span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">=</span> <span class="support support_class support_class_properties support_class_properties_actionscript support_class_properties_actionscript_3">color</span><span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">;</span>
        
        <span class="storage storage_type storage_type_actionscript storage_type_actionscript_3">var</span> trans<span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">:</span><span class="support support_class support_class_actionscript support_class_actionscript_3">ColorTransform</span> <span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">=</span> <span class="keyword keyword_operator keyword_operator_actionscript keyword_operator_actionscript_3">new</span> <span class="support support_class support_class_actionscript support_class_actionscript_3">ColorTransform</span>()<span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">;</span>
            trans<span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">.</span><span class="support support_class support_class_properties support_class_properties_actionscript support_class_properties_actionscript_3">color</span> <span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">=</span> <span class="variable variable_language variable_language_private variable_language_private_actionscript variable_language_private_actionscript_3">_color</span><span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">;</span>
            
        <span class="support support_function support_function_actionscript support_function_actionscript_3">transform</span><span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">.</span><span class="support support_function support_function_actionscript support_function_actionscript_3">colorTransform</span> <span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">=</span> trans<span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">;</span>
    }<span class="keyword keyword_operator keyword_operator_symbolic keyword_operator_symbolic_actionscript keyword_operator_symbolic_actionscript_3">;</span>
    </span>

    in this case, init just draws the graphics, so you can just re-init the ball, but if it were doing a lot more logic, it would make sense to just adjust the color, not re-init the object.

    ps… run the fountain swf above and press the space bar :)

  6. Jon says:

    Hi there,

    I know this communication is late, but I just wanted to say a big thank you for uploading a new version of the fountain program. It has given me more food for thought, for example the question of why the ENTER_FRAME event listener is not added to a specific object, and the others are? I tried adding E_F to the stage and it seems to work just the same?

    Anyway, although I am delighted you included this new file, keeping the discussion live… a part of me prefers the purity of the original code! : )

  7. Wero says:

    Reload Source file : Fountain.zip
    please

Leave a Reply

Your email address will not be published. Required fields are marked *

*