views:

184

answers:

3

I have this code:

package graphics {
 import flash.display.Sprite;
 import flash.events.*;

 public class Ball extends Sprite {
  public function Ball(_stage){
   _stage.addChild(this);
   drawBall();
  }
  private function drawBall(){
   graphics.beginFill(0x0000CC);
   graphics.lineStyle(2,0x000000,1);
   graphics.drawCircle(0,0,10);
   graphics.endFill();
  }
 }
}

ADDED:
and the class that I pass to mxmlc
:

package {
 import flash.display.Sprite;
 import graphics.*;

 [SWF(width='1024', height='768', backgroundColor='#FFFFFF', frameRate='30')]

 public class Application extends Sprite {
  public function Application(){
   var ball:Ball = new Ball(this);
  }
 }
}

Except that when I compile, I get the following error:

ball.as(11): col: 14 Error: Call to a possibly undefined method beginFill.

            graphics.beginFill(0x0000CC);
                     ^

Along with the other three graphics.x() calls.

I am probably doing something wrong here, but I do not know what. Do you?

+1  A: 

I am afraid the problem must be elsewhere in your code. If I take your class and clean it up so it compiles like so:

package
{
import flash.display.Sprite;

public class Ball extends Sprite 
{
    public function Ball()
    {
        drawBall();
    }

    private function drawBall():void
    {
        graphics.beginFill(0x0000CC);
        graphics.lineStyle(2,0x000000,1);
        graphics.drawCircle(0,0,10);
        graphics.endFill();
    }
}
}


    $ mxmlc Ball.as 
    Loading configuration file [...]/flex-config.xml
    /private/tmp/actionscript/Ball.swf (666 bytes)

It draws a blue ball in the top left corner of the screen. So your problem with graphics must be related to code which you have not shown here.


Edit: Based on the new information:

Your namespace graphics for the class Ball conflicts with the property name graphics. You need to rename the package and the directory it lives in.

James Fassett
Sorry, I didn't think that would be the problem. I've updated my question with the rest of the code.
Austin Hyde
Oh, silly me. You wouldn't believe how many times I name something thats already taken... Thanks for the help!
Austin Hyde
A: 

Your class can not add itself to the stage. It has no reference to the stage until it has been added to the display list. Your application class needs to both create the ball and add it to the stage. Also, it's just stage, not _stage.

Branden Hall
Well - in his Application class he is passing a reference to the stage (var ball:Ball = new Ball(this);) into the constructor of Ball. Not pretty or recommended but it is valid code that does what you'd expect.
James Fassett
Ah yes, I tried to answer this on my iPhone and couldn't read the code all the way there. You are correct. That being said, stage may not exist at that point (I'm not sure when, in the Flex bootstrap stage becomes available).
Branden Hall
+2  A: 

Just use this.graphics.method to avoid confusions created by your package name.

Edit - after comment.

After some messing around, it seems that though this.graphics traces as a correct Graphics object as expected, the this.graphics.method is looked in the Ball class. Don't know what messes this up like this, but it can be solved with a simple casting.

package graphics {
    import flash.display.Graphics;
        import flash.display.Sprite;
        import flash.events.*;

        public class Ball extends Sprite {
                public function Ball(_stage) {
                        _stage.addChild(this);
                        drawBall();
                }
                private function drawBall() {
             var g:Graphics = this.graphics as Graphics;
                        g.beginFill(0x0000CC);
                        g.lineStyle(2,0x000000,1);
                        g.drawCircle(0,0,10);
                        g.endFill();
                }
        }
}

Good luck

Virusescu
This is a good example why many say that class members should always be preceded by the "this" keyword even the code seems more verbose.
Virusescu
The compilation fails with the same errors.
Austin Hyde
Yep sorry. It really confuses the compiler. Check my other answer for something that works without the need of changing the packages.
Virusescu
Even though this works (and good on you for discovering it -- I too tried this.graphics without the cast and it didn't work) I would not recommend it as a good solution. IMO, the correct thing to do is the rename even if it is a bit more of a pain.
James Fassett
Yes, it's good to take this into consideration when planning your classes and packages, but sometimes such errors can occur when the application is in a more advanced phase. This, imho, is a workaround for what seems to be a compiler confusion. I would have bet on this.graphics to solve the problem. Sometimes renaming your packages might be more of a hustle than working around like this. So yes, keep this in mind and rename your package if there's nor problem with that :).
Virusescu