Tinkering: Trailing Fountain · May 31, 11:28 PM

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

jpeg that will be replaced by flash

Source: Fountain.zip


Jonathan Greene

comments:

  1. 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?

    Jonathan · Sep 26, 08:43 AM · #

  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.

    Jonathan Greene · Sep 26, 09:03 AM · #

  3. 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!) : )

    Jonathan · Sep 26, 11:27 AM · #

  4. 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.

    Jonathan · Sep 26, 02:24 PM · #

  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:

    public function set color ( color:uint ) : void 
    { 
        _color = color;
        
        var trans:ColorTransform = new ColorTransform();
            trans.color = _color;
            
        transform.colorTransform = trans;
    };
    

    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 :)

    Jonathan Greene · Sep 26, 08:34 PM · #

  6. 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! : )

    Jon · Oct 4, 11:59 AM · #

Name
Email
Website
Message
  Textile Help