【发布时间】:2015-12-22 12:45:35
【问题描述】:
我在一个名为“player”的影片剪辑中有一个枪子,而“player”在另一个名为“level one”的影片剪辑中。
所以在 gun 类中,代码生成了一个子弹。必须在父母的父母中产生。这样子弹就可以射入关卡了。
private function fire(m: MouseEvent)
{
//when bullet fired
var b = new Bullet;
MovieClip(MovieClip(parent).parent).addChild(b);
}
但是,项目符号永远不会出现在父级的父级中。这可能是什么问题?
更新代码:
在枪类中:
function fire(e:MouseEvent):void
{
dispatchEvent(new Event('fire!', true));
}
在播放器类中:
protected function fire(e: Event)
{
var b: Bullet = new Bullet();
// bullet must be in same position and angle as player.gun
b.rotation = player.gun.rotation;
b.x = player.gun.x; + player.gun.width * Math.cos(player.gun.rotation / 180 * Math.PI);
b.y = player.gun.y + player.gun.width * Math.sin(player.gun.rotation / 180 * Math.PI);
addChild(b);
}
【问题讨论】:
-
你不能从你的
Gun对象中触发一个自定义事件,并在子弹需要产生的任何地方捕获它吗? -
我刚刚尝试了一些非常相似的方法,这似乎对我有用。
Bullet对象被添加到LevelOne对象中。 -
@Crook 这应该可以工作(至少是可见的代码)。你的
fire()函数执行了吗?
标签: actionscript-3