#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    12

    Making random graphic generator into a class


    Hi coders,

    I have created some code that creates a blue circle of random size on the stage, and startmoving it in a random direction, at a random speed.

    But I'd like to create this as a static class, so that I can just import into a banner and make multiple instances appear at a time, but my limited knowledge doesn't allow me to

    Can anyone help me with this?

    Here is my code:

    Code:
    var child:Shape = new Shape()
    
    var childSpeedX:Number = Math.random()
    var childSpeedY:Number = Math.random()
    var childDirectionX:Number = Math.random()
    var childDirectionY:Number = Math.random()
    
    trace (childSpeedX, childSpeedY);
    
    child.graphics.beginFill(33222);
    child.graphics.lineStyle(1, 33222);
    child.graphics.drawCircle(100, 100, Math.random() * 100);
    
    stage_btn.addEventListener(MouseEvent.MOUSE_DOWN, drawChild)
    stage.addEventListener(Event.ENTER_FRAME, moveChild);
    
    
    function drawChild(event:Event):void
    {
        addChild(child);
    }
    
    function moveChild(event:Event):void
    {
        if(childDirectionX < 0.5)
        {
            child.x = child.x + childSpeedX;
        }
        else
            child.x = child.x - childSpeedX;
            
        if(childDirectionY > 0.5)
        {
            child.y = child.y + childSpeedY;
        }
        else
            child.y = child.y - childSpeedY;
    }
    All help is much appreciated
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Location
    Denmark
    Posts
    58
    Rep Power
    68
    I'd suggest making a class that extends Sprite, and contains the code to draw and move the graphics. Then for each instance you need of the graphics, you make a new instance of that class and add it to your stage or wherever you need it.

    Code:
    // Class that contains the code to draw your circle and move it
    public class Circle extends Sprite
    {
       ...your code
    }
    Code:
    public function main():void
    {
       var circle:Circle = new Circle();
       addChild(circle);
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    12
    Thanks for your help I'm pretty much totally ignorant on classes, and I'm having trouble with the tutorials I've tried. Could you spell it out for me a little more? I make an as3 file and place it in the same folder as my Flash file, right? Does it matter what I call it? Can you show me an example where you import a custom class?

    Sorry, I'm a n00b
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    12
    I've made a class that should create the bubble and linked it to an empty movie clip in my fla file, but it will not allow me to use the addChild method in the class. Gives me an undefined error. Can anyone tell why that is? Am I just missing an import, or did I do it all wrong here?

    Help is much appreciated.

    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.display.Shape;
    	
    	public class Bubble extends Sprite
    	{
    		
    		
    		public function Bubble()
    		{
    			
    		}
    		
    		static public function testing():void
    		{
    		
    		var child:Shape = new Shape()
    
    		var childSpeedX:Number = Math.random()
    		var childSpeedY:Number = Math.random()
    		var childDirectionX:Number = Math.random()
    		var childDirectionY:Number = Math.random()
    		
    		trace (childSpeedX, childSpeedY);
    		
    		child.graphics.beginFill(33222);
    		child.graphics.lineStyle(1, 33222);
    		child.graphics.drawCircle(100, 100, Math.random() * 100);
    		
    			if(childDirectionX < 0.5)
    			{
    				child.x = child.x + childSpeedX;
    			}
    			else
    				child.x = child.x - childSpeedX;
    				
    			if(childDirectionY > 0.5)
    			{
    				child.y = child.y + childSpeedY;
    			}
    			else
    				child.y = child.y - childSpeedY;
    				
    			addChild(child);
    		}
    	}
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Location
    Denmark
    Posts
    58
    Rep Power
    68
    Hi again, sorry I didn't respond before, been a bit busy at work, forgot :)

    I wrote a small bubble class you can use to build ontop of. It extends Sprite and draws a circle in itself. It has a direction (x and y coordinates) and a move function.

    actionscript Code:
     
    	import flash.display.Sprite;
    	import flash.geom.Point;
     
    	public class Bubble extends Sprite
    	{
    		public var direction:Point = new Point();
     
    		public function Bubble()
    		{
    			draw();
    		}
     
    		public function draw():void
    		{
    			graphics.clear();
    			graphics.beginFill(0x0000FF);
    			graphics.drawCircle(0, 0, 25);
    			graphics.endFill();
    		}
     
    		public function move():void
    		{
    			x += direction.x;
    			y += direction.y;
    		}
    	}


    What you need to do to use it, is to make a new instance of the class somewhere, like this

    var bubble:Bubble = new Bubble();

    And then add it to your stage.

    addChild(bubble);

    Comments on this post

    • Tann San agrees : Handy :)
    Last edited by FluffieBird; October 5th, 2010 at 11:57 AM.

IMN logo majestic logo threadwatch logo seochat tools logo