当我放置 AddEventListener 时,我得到了这个“类型强制失败消息”
addEventListener(Event.ENTER_FRAME,onEnterFrm);
位于鼠标事件上方:
addEventListener(Event.ENTER_FRAME,onEnterFrm);
stage.addEventListener(MouseEvent.CLICK, _clicked);
我无法看到导致错误的介于两者之间的关系。
//点击功能
private function _clicked(e:MouseEvent)
{
gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
}
// OnEnterFrn功能
private function onEnterFrm(e:MouseEvent):void
{
if (gameBall && gameBall.body.IsSleeping())
{
gameBall.destroy();
gameBall = null;
}
}
如果我点击运行,模拟有效,但滞后,运行时消息会弹出:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@2a03b2e1 to flash.events.MouseEvent.
完整代码:
package {
import flash.display.MovieClip;
import com.actionsnippet.qbox.*;
import flash.events.MouseEvent;
import flash.events.Event;
[SWF(width = 600, height = 600, frameRate = 60)]
public class LuckyHit extends MovieClip {
public var sim:QuickBox2D;
var winBox:QuickObject;
var looseBox:QuickObject;
var gameBall:QuickObject;
var simContacts:QuickContacts;
/**
* Constructor
*/
public function LuckyHit()
{
sim = new QuickBox2D(this);
simContacts = sim.addContactListener();
simContacts.addEventListener(QuickContacts.ADD, contactListener);
//sim.createStageWalls();
winBox = sim.addBox({x:5,y:600/30, width:300/30, height:10/30, density:0});
looseBox = sim.addBox({x:15,y:600/30, width:300/30, height:10/30, density:0});
//buscar fuerza de rebote.
// make obstacles
for (var i:int = 0; i<(stage.stageWidth/50); i++){
//End
sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});
//Mid End
sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});
//Middle Start
sim.addCircle({x:0 + i * 1.5, y:09, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:08, radius:0.1, density:0});
sim.addCircle({x:0 + i * 1.5, y:07, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:06, radius:0.1, density:0});
}
addEventListener(Event.ENTER_FRAME,onEnterFrm);
stage.addEventListener(MouseEvent.CLICK, _clicked);
sim.start();
}
/**
* ..
* @param e MouseEvent.CLICK
*/
private function _clicked(e:MouseEvent)
{
gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
}
private function onEnterFrm(e:MouseEvent):void
{
if (gameBall && gameBall.body.IsSleeping())
{
gameBall.destroy();
gameBall = null;
}
}
private function contactListener(e:Event)
{
if (simContacts.isCurrentContact(winBox,gameBall))
{
// Won.
trace ('You won!');
}
}
}
}
答案 0 :(得分:5)
你的onEnterFrm函数正在寻找一个事件,而不是一个MouseEvent。
变化
private function onEnterFrm(e:MouseEvent):void
到
private function onEnterFrm(e:Event):void
目前,您的onEnterFrm正在获得一个需要MouseEvent的事件。
答案 1 :(得分:1)
事件(Event.ENTER_FRAME)不是MouseEvent的子类型。因此转换错误。更改参数类型:onEnterFrm(e:Event)
。