經過一段時間的測試後,我發現我的地圖包含正確的值,但我使用的循環似乎只是使用此地圖中最後一個添加的值。我在這裏錯過了很明顯的東西嗎Haxe for loop只使用最後一項
,增加項目添加到地圖的功能:(對照是地圖變量)
public static function CreateThumbstick(mActorType:ActorType, mLocation:Int, mDirectionLock:Int)
{
var controllerName = "Thumbstick"+mLocation;
if(!controls.exists(controllerName)){
createRecycledActor(mActorType, 0, 0, Script.FRONT);
var lastActor = getLastCreatedActor();
var myPosition = GetPosition(controllerName, lastActor);
lastActor.setX(myPosition.x);
lastActor.setY(myPosition.y);
var myPos = new Vector2(lastActor.getXCenter(), lastActor.getYCenter());
var controlUnit = new ControlUnit(lastActor, myPos, -1);
controls.set(controllerName, controlUnit);
trace("added key: " + controllerName +" with value: "+ lastActor);
} else {
trace("!!WARNING!! Control unit already exists in this position. Command ignored!");
}
}
在創建3個搖桿,該日誌的狀態執行以下操作:
added key: Thumbstick1 with value: [Actor 1,Thumbstick]
added key: Thumbstick2 with value: [Actor 2,Thumbstick]
added key: Thumbstick3 with value: [Actor 3,Thumbstick]
當屏幕被觸摸,它應該循環遍歷我地圖中的每個項目,但它使用最後一次添加的項目3次來檢查距離,而不是所有3個項目一次。這裏是正在調用的時候,屏幕被觸摸監聽器:
addMultiTouchStartListener(function(event:TouchEvent, list:Array<Dynamic>):Void
{
for (unit in controls){
trace(lastDebugLine + "checking distance to " + unit.GetActor());
if(GetDistance(unit.GetCenter(), touch.GetPosition()) < 64){
break;
}
}
});
// used "touch.GetPosition()" instead of actuall code for easy reading. This is not causing any problems!
在觸摸屏幕時,日誌指出以下幾點:
checking distance to [Actor 3,Thumbstick]
checking distance to [Actor 3,Thumbstick]
checking distance to [Actor 3,Thumbstick]
我很新的HAXE語言,所以我的猜測是我錯過了一些明顯的東西,即使我非常密切地關注了Haxe API。這是從Haxe API頁面使用的示例:
var map4 = ["M"=>"Monday", "T"=>"Tuesday"];
for (value in map4) {
trace(value); // Monday \n Tuesday
}
歡迎所有解釋!
新增控制單元類:
import com.stencyl.models.Actor;
class ControlUnit
{
static var actor;
static var center;
static var touchID;
public function new(mActor:Actor, mPosition:Vector2, mTouchID:Int)
{
actor = mActor;
center = mPosition;
touchID = mTouchID;
}
public function GetActor():Actor{
return(actor);
}
public function GetCenter():Vector2{
return(center);
}
public function GetTouchID():Int{
return(touchID);
}
}
你確定這三個「檢查距離」痕跡都來自同一幀?可能是Actor 3是第一個循環並且距離適合的入口項目,所以循環停止('break')。嘗試在'for'之前加上'trace(「------」);''之前要有分隔符。 – Gama11
是的,我確定。我在測試中實際上並沒有使用痕跡,但我使用它來輕鬆閱讀。在實際測試中,我將這些曲線添加到開始循環前清除的列表中,並將它們顯示在屏幕上。 – sdieters
你調試過'unit.GetCenter()','touch。GetPosition()'和'GetDistance(unit.GetCenter(),touch.GetPosition())'在循環內? –