[英]AS3 hitTestObject different classes
如何使我的點擊測試對象起作用,圖片是:
另一個類中的一個對象應該感測來自其他類的對象何時通過hitTestObject對其進行觸摸。
動作腳本:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Grey extends Sprite {
var blue:Blue = new Blue();
private var changes:Boolean
private var directions
private var speed:int = 20;
public function Grey() {
// constructor code
stage.addEventListener(Event.ENTER_FRAME, loop)
/*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
}
function loop(e:Event){
movements();
hitTesting();
}
function movements(){
if (changes==true){
directions = -1;
}else{
directions = 1;
}
x+=speed * directions
}
function hitTesting(){
if (this.hitTestObject(blues)){
changes=true
}
}
}
}
就在幾天前,我正在開發一款游戲:D
一方面,您沒有在任何地方聲明憂郁。 您要做的是將所有Blue對象存儲到一個數組中,如下所示:
var blues:Array = new Array();
然后,無論何時創建數組,請確保將其放入:
var blue:Blue = new Blue();
blues.push(blue); //yay variable names!
最后,在EnterFrame函數中(或hitTesting,因為您在框架函數中調用了它):
for each(var blue:Blue in blues){
if (this.hitTestObject(blue)){
changes = true; //or whatever functionality. I use a contact function in the object hit
}
}
編輯:需要注意的一件事:您的Blue類不知道灰色在觸摸它。 設置代碼的方式看起來好像灰色知道藍色,但是藍色並不關心一種或另一種方式。 只是想確保清楚,因為您提出問題的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.