Actionscript 3 通过类块删除自己的MovieClip
我正在尝试一场船上比赛,我也有一个让对手退步的问题 嗯,这些对手(像船)有一个等级。在这个类中,我做了一个间隔,让它的子对象向左飞行(在函数的第四个参数上选择“添加敌人”(Actionscript 3 通过类块删除自己的MovieClip,actionscript-3,Actionscript 3,我正在尝试一场船上比赛,我也有一个让对手退步的问题 嗯,这些对手(像船)有一个等级。在这个类中,我做了一个间隔,让它的子对象向左飞行(在函数的第四个参数上选择“添加敌人”(AddObserver(opponentX,opponentY,opponentType,opponentVelocity)),当它们中的任何一个坐标X小于-25时,必须通过自身的类块删除 package { import flash.display.* import flash.display.MovieClip; im
AddObserver(opponentX,opponentY,opponentType,opponentVelocity)
),当它们中的任何一个坐标X小于-25时,必须通过自身的类块删除
package {
import flash.display.*
import flash.display.MovieClip;
import flash.utils.setTimeout;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class opponentNave extends MovieClip {
public function opponentNave(opponentVelocitySet) {
var loopMoveClassicOpponentsNave:uint = setInterval(movingClassicOpponentNave, 58);
function movingClassicOpponentNave() {
if (x < -25) {
clearInterval(loopMoveClassicOpponentsNave);
this.parent.removeChild(this);
} else {
x -= opponentVelocitySet;
}
}
}
}
包{
导入flash.display*
导入flash.display.MovieClip;
导入flash.utils.setTimeout;
导入flash.utils.setInterval;
导入flash.utils.clearInterval;
公共类对手已扩展MovieClip{
公共功能opponentNave(opponentVelocitySet){
var loopMoveClassicCoponentsNave:uint=setInterval(MovingClassicCoponentAve,58);
函数movingClassicoponentnave(){
如果(x<-25){
clearInterval(LoopMoveClassicCoponentsNave);
this.parent.removeChild(this);
}否则{
x-=对映式ELOCITYSET;
}
}
}
}
}
我正在使用
this.parent.removeChild(this)
。当对手X小于-25时,我会收到一个错误,此时我想删除对手的子对象。以下是我将如何重构它:(参见代码注释)
包
{
导入flash.display.MovieClip;
导入flash.events.Event;
公共类对手已扩展MovieClip
{
//为速度创建一个类范围的变量
私有var velocitySet:编号;
公共功能opponentNave(opponentVelocitySet)
{
//将速度设置为var
velocitySet=opponentVelocitySet;
//在执行任何面向显示的操作之前,请等待将此对象(opponentNave)添加到显示中
this.addEventListener(Event.ADDED_TO_STAGE,addedStatage,false,0,true);
}
私有函数addedStatage(e:事件):void{
//在应用程序的fps的每一帧刻度上运行一个函数
//这是最好的东西是面向显示,而不是基于时间的方式,如定时器或间隔
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
私有函数enterFrameHandler(e:事件):void
{
如果(x<-25){
if(this.parent)this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
其他的
{
x-=速度集;
}
}
}
}
您遇到了什么错误?只要以前未删除此
。如果(this.parent)this.parent.removeChild(this)
您应该使用计时器
而不是设置间隔
和设置超时
——或者更好的是使用输入帧
处理程序。问题可能是您的间隔运行速度快于帧速率(这可能会有其他问题)-另外,不要使用像那样的内联函数,那么我可以使用ENTER\u FRAME
来实现这一点吗?真的,如果这可以实现,我很感激……我会尝试这样做,谢谢。:o)真的有几轮我使用计时器,错误很严重。
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class opponentNave extends MovieClip
{
//create a class scoped variable for the velocity
private var velocitySet:Number;
public function opponentNave(opponentVelocitySet)
{
//set the velocity var
velocitySet = opponentVelocitySet;
//wait for this object (opponentNave) to be added to the display before doing anything display oriented
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
private function addedToStage(e:Event):void {
//run a function every frame tick of the application's fps
//this is best for things that are display oriented instead of time based ways like Timer or Intervals
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void
{
if (x < -25){
if (this.parent) this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
x -= velocitySet;
}
}
}
}