背景:
尝试做一个简单的“丢球”游戏。
代码位于时间轴的第一帧内。没有更多的东西在舞台上。
问题:
使用QuickBox2D我创建了一个简单的If语句,它可以删除和对象的鼠标x位置:
if (MouseEvent.CLICK) {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
我导入了MouseEvent库:
import flash.events.MouseEvent;
如果我点击,没有任何输出错误也没有发生。
查看实际操作: http://gabrielmeono.com/download/Lucky_Hit_Alpha.swf http://gabrielmeono.com/download/Lucky_Hit_Alpha.fla
完整代码:
[SWF(width = 350, height = 600, frameRate = 60)]
import com.actionsnippet.qbox.*;
import flash.events.MouseEvent;
var sim:QuickBox2D = new QuickBox2D(this);
sim.createStageWalls();
//var ball:sim.addCircle({x:mouseX, y:1, radius:0.25, density:5}); //
// make a heavy circle
sim.addCircle({x:3, y:1, radius:0.25, density:5});
sim.addCircle({x:2, y:1, radius:0.25, density:5});
sim.addCircle({x:4, y:1, radius:0.25, density:5});
sim.addCircle({x:5, y:1, radius:0.25, density:5});
sim.addCircle({x:6, y:1, radius:0.25, density:5});
// create a few platforms
sim.addBox({x:3, y:2, width:4, height:0.2, density:0, angle:0.1});
// make 26 dominoes
for (var i:int = 0; i<7; 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});
}
if (MouseEvent.CLICK) {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
sim.start();
/*sim.mouseDrag();*/
}
答案 0 :(得分:4)
您应该创建鼠标事件监听器:
addEventListener(MouseEvent.CLICK, add_circle_handler);
function add_circle_handler(event:MouseEvent):void {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
}
不再需要在不再需要添加圆圈时删除事件侦听器。这是与听众合作时的最佳实践(关于内存管理):
removeEventListener(MouseEvent.CLICK, add_circle_handler);
答案 1 :(得分:2)
您需要为鼠标事件添加事件侦听器:
addEventListener(MouseEvent.CLICK, mouseClickHandler);
protected function mouseClickHandler(event:MouseEvent):void
{
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
}
答案 2 :(得分:2)
那是因为你正在听老鼠点击错误。
而不是
if (MouseEvent.CLICK) {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
sim.start();
/*sim.mouseDrag();*/
}
你需要做
addEventListener(MouseEvent.CLICK, clicked);
function clicked(e:MouseEvent) {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
sim.start();
/*sim.mouseDrag();*/
}
如果您不需要再收听点击次数,请执行
removeEventListener(MouseEvent.CLICK, clicked);
删除事件监听器(用于释放内存)