Мне пришлось использовать мувик, т.к. класс наследуется от мувика, который нарисован и добавлен в библиотеку. По коду у меня организовано думаю так себе, т.к. писал ровно так, как хватало знаний, я не использую *.pop(не понимаю как его использовать в пуле), сделал проще для себя. Объекты живут разное время, но совсем недолго, просто отживший мувик я удаляю со сцены и кидаю ссылку на новый объект в вектор, увеличивая длину вектора, потом удаляю слушатель для мувика, потом ссылку в null, и убираю из вектора ненужный элемент, перевожу индекс на последний элемент добавленный на сцене, что позволяет при вызове не искать свободный мувик, а сразу вывести.
собственно код, написан некрасива, потихоньку исправляюсь, когда пишу новое
Код AS3:
package package_cl {
import flash.events.Event;
import package_cl.global_variable_cl;
public class bullet_cl extends bullet_mc {
private var gl_variable: global_variable_cl = new global_variable_cl();
public function bullet_cl() {
this.addEventListener(Event.ADDED_TO_STAGE, fn_speed);
}
public function fn_speed(e: Event): void {
x -= gl_variable.speed_bullet * Math.cos(rotation * Math.PI / 180);
y -= gl_variable.speed_bullet * Math.sin(rotation * Math.PI / 180);
}
}
}
Код AS3:
package package_cl {
import flash.events.Event;
import package_cl.Vector_Pool_cl;
import package_cl.global_variable_cl;
//***************************************************************************************
public class Control_bullet_cl {
private var fire_ship_Boolean: Boolean = false;
private var stageX: int;
private var stageY: int;
private var _stage: * ;
private var vectorBullet: Vector_Pool_cl;
private var bullet: bullet_cl = new bullet_cl();
private var gl_variable: global_variable_cl = new global_variable_cl();
public function Control_bullet_cl(stage) {
_stage = stage;
stageX = _stage.stageHeight - 20; //высота/ширина
stageY = _stage.stageWidth - 20;
vectorBullet = new Vector_Pool_cl(gl_variable.max_vector_bullet, bullet);
_stage.addEventListener(Event.ENTER_FRAME, fn_null_bullet);
}
//***************************************************************************************
public function fn_fire_down(_x, _y, _rotation): void {
if (gl_variable.index_bullet == vectorBullet.vectorPool.length) {
vectorBullet.fn_add_pool();
fn_fire_down(_x, _y, _rotation); //рекурсия
} else {
vectorBullet.vectorPool[gl_variable.index_bullet].x = _x;
vectorBullet.vectorPool[gl_variable.index_bullet].y = _y;
vectorBullet.vectorPool[gl_variable.index_bullet].rotation = _rotation;
_stage.addEventListener(Event.ENTER_FRAME, vectorBullet.vectorPool[gl_variable.index_bullet].fn_speed)
_stage.addChild(vectorBullet.vectorPool[gl_variable.index_bullet]);
++gl_variable.index_bullet;
}
}
public function fn_fire_up(_x, _y, _rotation): void {
if (gl_variable.index_bullet == vectorBullet.vectorPool.length) {
vectorBullet.fn_add_pool();
fn_fire_up(_x, _y, _rotation); //рекурсия
} else {
vectorBullet.vectorPool[gl_variable.index_bullet].x = _x;
vectorBullet.vectorPool[gl_variable.index_bullet].y = _y;
vectorBullet.vectorPool[gl_variable.index_bullet].rotation = _rotation;
_stage.addEventListener(Event.ENTER_FRAME, vectorBullet.vectorPool[gl_variable.index_bullet].fn_speed)
_stage.addChild(vectorBullet.vectorPool[gl_variable.index_bullet]);
++gl_variable.index_bullet;
}
}
//***************************************************************************************
public function fn_null_bullet(e: Event): void {
var i = vectorBullet.vectorPool.length;
while (--i > -1) {
if (vectorBullet.vectorPool[i] != null) {
if (vectorBullet.vectorPool[i].stage != null) {
if (vectorBullet.vectorPool[i].y > stageX || vectorBullet.vectorPool[i].y < 150 || vectorBullet.vectorPool[i].x > stageY || vectorBullet.vectorPool[i].x < 10) {
_stage.removeEventListener(Event.ENTER_FRAME, vectorBullet.vectorPool[i].fn_speed);
vectorBullet.fn_push_pool(i);
_stage.removeChild(vectorBullet.vectorPool[i]);
vectorBullet.vectorPool[i] = null;
vectorBullet.vectorPool.splice(i, 1);
--gl_variable.index_bullet;
}
}
}
}
}
}
}
Код AS3:
package package_cl{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.*;
public class Vector_Pool_cl {
public var vectorPool: Vector.<MovieClip> = new Vector.<MovieClip>;
private var lengthVector: int;
private var movieClip: MovieClip;
public function Vector_Pool_cl(_lengthVector: int, _movieClip: MovieClip) {
lengthVector = _lengthVector;
movieClip = _movieClip;
var i = lengthVector;
var movieClipDef:Class = MovieClip(movieClip).constructor;
while (--i > -1){
var movieClipView = new movieClipDef ();
vectorPool.push(movieClipView);
}
}
public function fn_push_pool (i):void{
vectorPool.push(vectorPool[i]);
}
public function fn_add_pool ():void{
var movieClipDef:Class = MovieClip(movieClip).constructor;
var movieClipView = new movieClipDef ();
vectorPool.push(movieClipView);
}
}
}