Search code examples
haxestencyl

Haxe for loop only uses last item


After some hours doing some testing I figured out that my map contains the correct values, but the loop that I am using only seems to be using the last added value in this map. Am I missing something obvious here?

The function that adds items to the map: (controls is the map variable)

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!");
        }
    }

Upon creating 3 thumbsticks, the log states the following:

added key: Thumbstick1 with value: [Actor 1,Thumbstick]
added key: Thumbstick2 with value: [Actor 2,Thumbstick]
added key: Thumbstick3 with value: [Actor 3,Thumbstick]

When the screen is touched, it should loop through each item in my map, but it is using the last added item 3 times to check the distance with, rather then all 3 items once. Here is the Listener that is being called when the screen is touched:

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!

Upon touching the screen, the log states the following:

checking distance to [Actor 3,Thumbstick]
checking distance to [Actor 3,Thumbstick]
checking distance to [Actor 3,Thumbstick]

I am quite new to the Haxe language, so my guess is that I am missing something obvious, even after I have followed the Haxe API very closely. This is the example used from the Haxe API page:

var map4 = ["M"=>"Monday", "T"=>"Tuesday"];    
for (value in map4) {
    trace(value); // Monday \n Tuesday
}

All explanations are welcome!

Added ControlUnit class:

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

Solution

  • You just used static for vars in class definitions - they aren't instance aware/based. Check 'properties', getters, setters etc. in https://haxe.org/manual/class-field-property.html