2013-02-04 55 views
1

我有一個可以拖動到另一個對象的對象。我已經爲碰撞事件設定了一個命中測試。當碰撞發生時,我想前進到下一幀,但是,我必須點擊可拖動對象才能這樣做。我希望它立即移動到下一個框架而無需點擊。有沒有什麼辦法解決這一問題?Flash AS3命中測試轉到下一幀

我的意思是,在我拖動了反對者來創建碰撞之後,我需要再次單擊該對象才能前進到下一幀。我不想再次點擊對象,我希望它在碰撞發生時進入下一幀。

這是我的代碼

bottle.buttonMode = true; 

bottle.addEventListener(MouseEvent.MOUSE_DOWN, drag); 

bottle.addEventListener(MouseEvent.MOUSE_UP, drop); 


function collision():void{ 
    if(bottle.hitTestObject(hit)){ 
    nextFrame(); 
    } 
    } 

function drag(e:MouseEvent):void{ 
bottle.startDrag(); 
collision(); 
} 



    function drop(e:MouseEvent):void{ 
    bottle.stopDrag(); 
} 
+0

如果你不這樣做點擊並移動物體,它是如何碰撞的? – Gone3d

回答

0

您應該drop後碰撞檢測,而不是在drag開始:

function collision():void{ 
    if(bottle.hitTestObject(hit)){ 
     nextFrame(); 
    } 
} 

function drag(e:MouseEvent):void{ 
    bottle.startDrag(); 
} 

function drop(e:MouseEvent):void{ 
    bottle.stopDrag(); 
    collision(); 
} 
0

變化collision()成一個事件偵聽器並將其連接到bottle

0

試試這個(改編自加里·羅森茨維格):

bottle.buttonMode = true; 

bottle.addEventListener(MouseEvent.MOUSE_DOWN, startBottleDrag); 
stage.addEventListener(MouseEvent.MOUSE_UP, stopBottleDrag); 

function collision():void { 
    if(bottle.hitTestObject(hit)) { 
     stopBottleDrag(); 
     nextFrame(); 
    } 
} 

// to keep bottle location as it is when clicked 
var clickOffset:Point = null; 

function startBottleDrag(e:MouseEvent) { 
    clickOffset = new Point(e.localX, e.localY); 
    bottle.addEventListener(Event.ENTER_FRAME, dragBottle); 
} 

function stopBottleDrag(e:MouseEvent = null) { 
    clickOffset = null; 
    bottle.removeEventListener(Event.ENTER_FRAME, dragBottle); 
} 

function dragBottle(e:Event) { 
    bottle.x = mouseX - clickOffset.x; 
    bottle.y = mouseY - clickOffset.y; 
    collision(); 
}