2011-11-30 199 views
1

背景:AS3:鼠標點擊事件不工作

試圖讓一個簡單的「誤事」的遊戲。

該代碼位於時間軸的第一幀內。沒有更多的是在舞臺上。

問題:

使用QuickBox2D我做了一個簡單的聲明,如果下降和對象acording的鼠標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();*/ 




    } 

回答

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); 

刪除事件監聽器(用於釋放內存)

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); 
2

您需要添加的事件偵聽器的鼠標事件:使用監聽器(它的內存管理)時,它的最佳實踐

addEventListener(MouseEvent.CLICK, mouseClickHandler); 

protected function mouseClickHandler(event:MouseEvent):void 
{ 
    sim.addCircle({x:mouseX, y:1, radius:0.25, density:5}); 
}