for (var i = 1; i <= 9; i++) { var img_mc:MovieClip = this["tu" + i]; // 添加点击事件监听器 img_mc.onPress = function() { // 创建该图像的副本,并将其设置为可拖动 var target_mc:MovieClip = this.duplicateMovieClip(this._name + "_copy", _root.getNextHighestDepth()); target_mc.startDrag(); target_mc.onPress = function() { this.startDrag(); }; // 在鼠标移动事件处理程序中,将图像的位置设置为鼠标位置 target_mc.onMouseMove = function() { if (Key.getCode() == 72) { // H键的ASCII码值为72 // 从舞台上删除该剪辑 this.removeMovieClip(); return; } this._x = _root._xmouse; this._y = _root._ymouse; // 检查图像是否与其他图像重叠,并根据需要更新其透明度 for (var j = 1; j <= 9; j++) { var other_mc:MovieClip = _root["tu" + j]; if (target_mc != other_mc && target_mc.hitTest(other_mc)) { target_mc._alpha = 50; return; } } target_mc._alpha = 100; }; // 在鼠标释放事件处理程序中,检查图像是否与其他图像重叠 target_mc.onRelease = target_mc.onReleaseOutside = function () { this.stopDrag(); this.onMouseMove = null; target_mc.onRelease = target_mc.onReleaseOutside = function () { this.stopDrag(); }; // 检查图像是否与其他图像重叠,并根据需要更新其透明度 for (var j = 1; j <= 9; j++) { var other_mc:MovieClip = _root["tu" + j]; if (target_mc != other_mc && target_mc.hitTest(other_mc)) { target_mc._alpha = 50; return; } } // 将图像放置在当前位置,并将其透明度设置为100% target_mc._alpha = 100; target_mc.swapDepths(_root.getNextHighestDepth()); }; }; 我按下h键时 副本图片没有被删除
时间: 2023-11-27 13:54:00 浏览: 35
可能是因为你的代码中没有正确绑定键盘事件监听器。你需要添加如下代码:
```
Key.addListener(this);
```
这会将对键盘事件的监听器添加到你的代码中,并确保按下H键时会触发删除副本图片的操作。
相关问题
for (var i = 1; i <= 9; i++) { var img_mc:MovieClip = this["tu" + i]; // 添加点击事件监听器 img_mc.onPress = function() { // 创建该图像的副本,并将其设置为可拖动 var target_mc:MovieClip = this.duplicateMovieClip(this._name + "_copy", _root.getNextHighestDepth()); target_mc.startDrag(); target_mc.onPress = function() { this.startDrag(); }; // 在鼠标移动事件处理程序中,将图像的位置设置为鼠标位置 target_mc.onMouseMove = function() { this._x = _root._xmouse; this._y = _root._ymouse; // 检查图像是否与其他图像重叠,并根据需要更新其透明度 for (var j = 1; j <= 9; j++) { var other_mc:MovieClip = _root["tu" + j]; if (target_mc != other_mc && target_mc.hitTest(other_mc)) { target_mc._alpha = 50; return; } } target_mc._alpha = 100; }; // 在鼠标释放事件处理程序中,检查图像是否与其他图像重叠 target_mc.onRelease = target_mc.onReleaseOutside = function () { this.stopDrag(); this.onMouseMove = null; target_mc.onRelease = target_mc.onReleaseOutside = function () { this.stopDrag(); }; // 检查图像是否与其他图像重叠,并根据需要更新其透明度 for (var j = 1; j <= 9; j++) { var other_mc:MovieClip = _root["tu" + j]; if (target_mc != other_mc && target_mc.hitTest(other_mc)) { target_mc._alpha = 50; return; } } // 将图像放置在当前位置,并将其透明度设置为100% target_mc._alpha = 100; target_mc.swapDepths(_root.getNextHighestDepth()); }; }; }在这串代码中 增加一个功能 当我鼠标移动到影视剪辑副本上面的时候 并且按下h键 这个影视剪辑副本 从舞台上删除 要怎么做
可以在`target_mc.onMouseMove`事件处理程序中添加按键检测,如果检测到按下了“h”键,则使用`removeMovieClip()`方法将该剪辑从舞台上删除。代码如下:
```
target_mc.onMouseMove = function() {
// 检测是否按下了"h"键
if(Key.isDown(Key.H)) {
// 从舞台上删除该剪辑
this.removeMovieClip();
return;
}
this._x = _root._xmouse;
this._y = _root._ymouse;
// 检查图像是否与其他图像重叠,并根据需要更新其透明度
for (var j = 1; j <= 9; j++) {
var other_mc:MovieClip = _root["tu" + j];
if (target_mc != other_mc && target_mc.hitTest(other_mc)) {
target_mc._alpha = 50;
return;
}
}
target_mc._alpha = 100;
};
```
var clips:Array = [tu1, tu2, tu3, tu4, tu5, tu6, tu7, tu8, tu9];// 存储所有影视剪辑的数组 var STATE_NORMAL:Number = 1; var STATE_OVER:Number = 2; var STATE_SELECTED:Number = 3; for (var i:Number = 0; i < clips.length; i++) { var clip:MovieClip = clips[i]; clip.gotoAndStop(STATE_NORMAL); clip.selected = false;// 添加一个selected属性并初始化为false clip.useHandCursor = true; clip.onRollOver = function() { trace("onRollOver called"); this.gotoAndStop(STATE_OVER); }; clip.onRollOut = function() { trace("onRollOut called"); if (!this.selected) { this.gotoAndStop(STATE_NORMAL); } }; clip.onRelease = function() { trace("onRelease called"); if (this.selected) { return; } for (var j:Number = 0; j < clips.length; j++) { var c:MovieClip = clips[j]; c.selected = false; c.mc_normal._visible = true; c.mc_over._visible = false; c.mc_selected._visible = false; } this.selected = true; this.mc_normal._visible = false; this.mc_over._visible = true; this.mc_selected._visible = true; }; }为啥要切换帧
这段代码是一个简单的交互式影视剪辑选择器,它包含了影视剪辑的状态和选择属性。当鼠标滑过一个影视剪辑时,它将切换到 STATE_OVER 状态,当鼠标滑出时,如果它没有被选中,它会切换回 STATE_NORMAL 状态。当用户点击一个影视剪辑时,它将被选中并且其他的影视剪辑将会变回未选中状态。为了实现这些效果,这段代码需要通过切换帧来改变影视剪辑的外观和状态。这些帧可能是不同的图像或者文本,它们代表了影视剪辑的不同状态。
阅读全文