1
votes

I'm learning how classes work, it's pretty complicated. I need an example of a right object creation. Let's say I have a MovieClip blueprint in my library. It can be used as an example to create many instances with names that can further be used as objects in code to change their properties. So pretend I have a blueprint "AppleBP". I set it as Apple class (assume, it creates a file Apple.as?). I need a code that would create (by pushing a button, for example) an instance Apple01 that would appear at a random place on the screen (just to know it's different from others, I know how to randomize position). Then make Apple02, Apple03, Apple04 and Apple05. If it's hard to set name to "Apple" + N, it's ok to stick to an array with names or at least pick it like if(N = 1){//code for Apple01 creation} then N++ etc.

So for now I tried

var Apple01:Apple = new Apple();
Apple01.visible = true; //just in case I can't see it
Apple01.x = 100;
Apple01.y = 100;
TextField01.text = "blahblah"; //to see if code actually works

Apple doesn't appear, text does. So what am I missing if creating a new class that is a MovieClip blueprint isn't enough? And how would I pick new names for each new instance so that it had variable String instead of pure name? (so that I could change the name as text + number based on it's order)

Add:

package  {
import flash.display.Scene;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.*;
import flash.display.*;

public class MyFirstClass extends MovieClip{ 

        //var N:int;
        var n:int;
        var existingApples:int = 0;
        var apples:Array = new Array();     

    public function MyFirstClass():void{
        MakeNewApple();
        MakeNewApple();
        MakeNewApple();
        MakeNewApple();
        MakeNewApple();

        //apple4.x = 5;
        //apple4.y = 5;

        var draggableApple:Apple = apples[0] ;
        draggableApple.addEventListener(MouseEvent.MOUSE_DOWN, draggableApple.onMouseDown) ;
        draggableApple.addEventListener(MouseEvent.MOUSE_UP, draggableApple.onMouseUp) ;
        info01.text = "Did it";
    }
    public function MakeNewApple():void{
        if(existingApples < 5){
        n = existingApples;
        var apple:Apple = new Apple();
        stage.addChild(apple);
        apple.x = (Math.random()*600+100);
        apple.y = (Math.random()*400+100);
        apple.name = "apple" + n;

        apples.push(apple.name);
        existingApples++;
        trace(existingApples);
        trace(apples);
        }
    }
}
}

Apple.as:

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.display.*;

public class Apple extends MovieClip{

    public function Apple() {

    }

    public function onMouseDown(e:MouseEvent):void{
        this.startDrag();
    }
    public function onMouseUp(e:MouseEvent):void{
        this.stopDrag();

    }
}
}
1
You need to do addChild(Apple01) for it to show up. So first you create an instance by doing new Apple(), then you add it to the display list (the objects on stage) using addChild(Apple01).Lars Blåsjö

1 Answers

1
votes

Here is come commented example:

package 
{
    import flash.display.Scene;
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {

        public function Main():void {   

            var apples:Array = new Array() ; // Declare and array
            for (var i:int = 0 ; i < 10 ; i++) { //Let's make 10 apples
                var apple:Apple = new Apple() ; //Create an apple
                apple.x = Math.ceil(Math.random() * stage.stageWidth)  ; //Assign random x
                apple.y = Math.ceil(Math.random() * stage.stageHeight) ; //Assign random y
                stage.addChild(apple) ; //Add it to stage, so we can see them
                apples.push(apple) ; //Push into array
            }
            //EDITED
            var draggableApple:Apple = apples[0] ;
            draggableApple.addEventListener(MouseEvent.MOUSE_DOWN, draggableApple.onMouseDown) ;
            draggableApple.addEventListener(MouseEvent.MOUSE_UP, draggableApple.onMouseUp) ;

        }

    }

}

And Apple.as

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

public class Apple extends MovieClip {

    public function Apple() {
        this.graphics.lineStyle(2) ;
        this.graphics.beginFill(0x00FF40) ;
        this.graphics.drawCircle(0, 0, 20) ;
        this.graphics.endFill() ;

        //addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown) ;
        //addEventListener(MouseEvent.MOUSE_UP, onMouseUp) ;
    }

    public function onMouseDown(event:MouseEvent):void {
        this.startDrag() ;
    }

    public function onMouseUp(event:MouseEvent):void {
        this.stopDrag() ;
    }
}
}
  • Also, name of all variables must start with a small letter. myVariable
  • Names of classes must implement CamelCaseClass
  • Names of constants contain only big letters and underscores. MY_CONSTANT