我在做一个游戏在AS3。
I'm making a game in AS3.
我已经在我的code:
I've got in my code :
public var _classes:Array = new Array(poubelle1, poubelle2, poubelle3);
public var _movieClips:Array = new Array();
公共职能apparitionDechet(事件:TimerEvent):无效{
public function apparitionDechet(event : TimerEvent):void{
_movieClips.push(new _classes[Math.floor(Math.random() * _classes.length)]());
stageRef.addChild(_movieClips[_movieClips.length-1]);
我试图把一个的addEventListener的影片剪辑。 玩家应该能够在一个影片剪辑点击时,它的出现或者他可以等待。很少会出现,他可以在任何时刻点击它们。 每点击会使MoviClip disapear ..
I'm trying to put an addEventListener on the MovieClips. The player should be able to click on a MovieClip when it's appearing or he can wait. Few will appears, and he can click on them at any moments. Each clicks will make the MoviClip disapear..
所以我把:
_movieClips[1].addEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true);
}
public function removePoubelle(e:MouseEvent):void{
if(e.target=="_movieClips[0]"){
trace("ok1");
}
if(e.target=="_movieClips[1]"){
trace("ok2");
}
if(e.target=="_movieClips[2]"){
trace("ok3");
}
但它不是...
but it's not that...
你知道我能做到这一点? 这是我使用的随机幽灵影片剪辑...
Do you know how I can do that ? It's my first time that I'm using the randomly apparition of MovieClips...
非常感谢你,
修改
所以我跟着你的技巧和做:
So I've followed your tips and did this :
public function apparitionDechet(event : TimerEvent):void{
var mc:DisplayObject = new _classes[Math.floor(Math.random() * _classes.length)]();
_movieClips.push(mc);
stageRef.addChild(mc);
mc.addEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true);
}
public function removePoubelle(e:MouseEvent):void{
var mc:DisplayObject = e.target;
var i:int=_movieClips.indexOf(mc);
if (i>=0){
_movieClips.splice(i,1);
mc.parent.removeChild(mc);
}
}
不过,我已经有了静态类型对象的值的误差1118隐式强制为可能无关的类型flash.display一:的DisplayObject
But I've got the error 1118 Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject
编辑2
虽然快速的问题,是有可能这样做:
Quick question though, is it possible to do :
if(stageRef.contains(poubelle1)) {
trace("poubelle1détécté");
}
if(stageRef.contains(poubelle2)) {
trace("poubelle2 détécté");
}
?
影片剪辑poubelle1和poubelle 2这样定义
movieClips poubelle1 and poubelle 2 are defined like this
public var _classes:Array = new Array(poubelle1, poubelle2, poubelle3);
public var _movieClips:Array = new Array();
这似乎并没有工作,如果我这样做(错误1027 Contrainte implicite德新英格兰大学valeur杜Class类型VERS联合国型号SANS融洽flash.display一:的DisplayObject)。知道为什么吗? 你要我创建一个新的岗位?
it doesn't seem to work if I do that.(error 1027 Contrainte implicite d'une valeur du type Class vers un type sans rapport flash.display:DisplayObject) Any idea why ? Do you want me to create a new post ?
感谢您
如果您要删除单击的影片剪辑,你已经把它作为事件的目标。所以,你得到它的母公司,并调用 removeChild之()
。不要忘记删除事件监听器关闭目标。
If you are to remove the movieclip that was clicked, you already have it as the event's target. So you get its parent and call removeChild()
. Don't forget to remove the event listener off the target.
public function removePoubelle(e:MouseEvent):void {
var mc:DisplayObject = e.target as DisplayObject;
if (!mc) return; // typecast failed
mc.parent.removeChild(mc);
// mc.removeEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true);
// the line above might not be needed as the listener weakly references the mc
}
和你把听者只要你创建新的影片剪辑。
And you put the listener as soon as you create your new movie clip.
public function apparitionDechet(event : TimerEvent):void {
var mc:DisplayObject = new _classes[Math.floor(Math.random() * _classes.length)]();
_movieClips.push(mc);
stageRef.addChild(mc);
mc.addEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true);
}
你看,你如何避免引用不难看的 _movieClips [_movieClips.length-1]
建设新创建的影片剪辑?你只要,然后实例化的随机MC了 _classes
的局部变量,然后使用该变量来做到这一点真实需要在创作的时候一切
See, how you can avoid referencing the newly created movie clip without ugly _movieClips[_movieClips.length-1]
construction? You just make a local variable which is then instantiated for your random MC out of _classes
, and you then use the variable to do everything else that's needed at the time of creation.
不过,这仍然是不够的 - 你的poubelle依然是你 _movieClips
数组里面,所以它会成长。你需要清理阵列了。因此,添加此code到 removePoubelle
:
But, this is still not enough - your "poubelle" is still inside your _movieClips
array, so it'll grow. You need to clean up the array too. So, add this code to removePoubelle
:
var i:int=_movieClips.indexOf(mc);
if (i>=0) _movieClips.splice(i,1);
这得到阵列内的点击影片剪辑的位置,如果它是一个有效的(零个或多个)数组被告知要删除元素。
This gets the position of the clicked movie clip inside your array, and if it's a valid one (zero or more) the array is told to remove that element.