1

经过几个小时的测试,我发现我的地图包含正确的值,但我正在使用的循环似乎只使用了这张地图中的最后一个附加值。我在这里遗漏了一些明显的东西吗?

将项目添加到地图的函数:(控件是地图变量)

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
}

欢迎所有解释!

添加了 ControlUnit 类:

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

3 回答 3

3

您只是static在类定义中用于 vars - 它们不是实例感知/基于的。在https://haxe.org/manual/class-field-property.html中检查“属性”、getter、setter 等

于 2017-07-07T14:23:41.823 回答
1

您确定getLastCreatedActor()每次都返回一个单独的实例吗?如果它每次都返回相同的实例,你可能会看到你得到了什么。

于 2017-07-06T11:17:34.797 回答
1

Isn't that because all of your keys map to the same value? Try mapping them to different values and test it.

于 2017-07-06T11:39:27.340 回答