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();
}
}
}