LOADING
Loading
Hi , welcome back.
LogoutLOGOUT
 
  Lost password?  
Hi
 




ReplyREPLY THREAD
 
Thread Tools Display Modes
  #1  
Old 02-23-2009, 02:11 PM
floody123 floody123 is offline
Registered User
 
Join Date: May 2007
Posts: 13
Rep Power: 0
floody123 is on a distinguished road
[CS3] Linestyle, user drawing and issue with variable

Hello,

I'm attempting to make a very simple drawing programme. There will be maybe 4/5 buttons. Three of them when you click on the change the color of the line the user draws. the other two will change the line thickness. I've tried setting up a variable to set to how thick the line should be depending on the button the user has pressed.

But it has no effect - i'm guessing that's because the variable only exists within the confines of the function that I have it in. So how do I allow the variable to exist globally, or am I going the wrong way about this.

Below is the code that has been a class (i'm using as3)....

Code:
 package{
    import flash.display.*;
    import flash.events.MouseEvent;
    
    public class Artist extends Sprite{
    //Are we drawing or not?
    private var drawing:Boolean;
     
    
    public function Artist(){
        
        var color
        var colorNo;
        var colorNo1
        var width1 = 15
        blueButton.addEventListener(MouseEvent.CLICK, blueF)
        blackButton.addEventListener(MouseEvent.CLICK, blackF)
        redButton.addEventListener(MouseEvent.CLICK, redF)
        
        wideButton.addEventListener(MouseEvent.CLICK, wide)
        
        
        function wide()
        {
            width1 = 15
        
        }
        
        
        function blueF()
        {
            colorNo1 = 0x0000FF
            colorNo = 1
            if (colorNo == 1)
        {color = 0x0000FF
        graphic_mc.graphics.lineStyle(width1,color);
        }
        
        
        
            graphic_mc.graphics.lineStyle(width1,color);
            trace ("yes")
            trace (colorNo+"colorNo")
        }
        
        
        function blackF()
        {
            colorNo = 2
            
        if (colorNo == 2)
        {color = 0x000000
        graphic_mc.graphics.lineStyle(width1,color);
        }
        
        
            graphic_mc.graphics.lineStyle(width1,color);
            trace ("yes")
            trace (colorNo+"colorNo")
        }
        
        
        function redF()
        {
            colorNo = 3
            
        if (colorNo == 3)
        {color = 0xFF0000
        graphic_mc.graphics.lineStyle(width1,color);
        }
        
        
            graphic_mc.graphics.lineStyle(width1,color);
            trace ("yes")
            trace (colorNo+"colorNo")
        }
        
        
        graphic_mc.graphics.lineStyle(width1,color);
        drawing = false;//to start with
        stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
    }
    
    public function startDrawing(event:MouseEvent):void{
        //move to the correct starting position for drawing
        graphic_mc.graphics.moveTo( mouseX, mouseY);
        drawing = true;
    }
    
        public function draw(event:MouseEvent){
        if(drawing){
            graphic_mc.graphics.lineTo(mouseX,mouseY);
        }
    }
    
        public function stopDrawing(event:MouseEvent){
        drawing = false;
    }
}
}
Reply With Quote
  #2  
Old 02-26-2009, 02:14 AM
phelpsi phelpsi is offline
Registered User
 
Join Date: Jan 2009
Location: United States
Posts: 10
Rep Power: 0
phelpsi is on a distinguished road
Yikes

I think I see a couple of hairy things happening here:

1) Correct me if I'm wrong, but are you declaring your "colorF" (blackF/blueF/redF) functions inside your constructor? If so, why?

2) Within the colorF functions, you're setting colorNo and then testing it immediately, assigning colorNo1 and color to the same value, then setting the line style outside and inside of the if-block. This seems really redundant.

3) wide() does the same thing you did inside the constructor (wide1 = 15), and even then, is declared local to the constructor.

4) Where does graphics_mc come from? Do you need it?

My suggestions:

1) Declare your vars (color, width, colorNo) and methods OUTSIDE of your constructor, so they stick around.

2) Have one MouseEvent method that sets color/colorNo, and another that sets width. Add the listeners, assign values to them using the listener functions, and then set the line style when when you startDrawing.

3) Don't bother testing a variable you plan on checking immediately. If you set x = 5, and then check if it's == 5... you can skip that step.

4) Don't repeat lineStyles inside and outside of an if-statement. The line style gets set either way.

That's a lot, so if I need to explain through code examples, I can.
Reply With Quote


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump




All times are GMT. The time now is 09:18 AM.