1

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);
    }
}
sdieters
  • 175
  • 13
  • Are you sure those three "checking distance to" traces are all from the same frame? Could be that Actor 3 is the first entry item that is looped over and the distance fits, so the loop is stopped (`break`). Try putting something like `trace("------");` before the `for` to have a separator. – Gama11 Jul 06 '17 at 11:07
  • Yes i'm sure. I'm not actually using trace in my tests, but I used this for easy reading. In the actual tests I am adding these traces to a list which is cleared before starting the loop, and show them on screen. – sdieters Jul 06 '17 at 12:07
  • Have you debugged `unit.GetCenter()`, `touch.GetPosition()` and `GetDistance(unit.GetCenter(), touch.GetPosition())` inside the loop? – Jonas Malaco Jul 07 '17 at 13:54

3 Answers3

3

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

Gama11
  • 31,714
  • 9
  • 78
  • 100
xadm
  • 8,219
  • 3
  • 14
  • 25
  • That sounds like it could be the issue. Ill try it first thjng when I get home. The reason I made then static is because I kept getting errors like "can't use non static variables in static function" (or something along those lines), so by default I declare them as static. When using those getters and setters, does it still matter wether I make the variables static or not? – sdieters Jul 07 '17 at 16:15
  • This is important to understand all these dependenties and sadly they aren't clearly explained. Feel free to play with https://try.haxe.org/#807e4 – xadm Jul 09 '17 at 10:35
1

Are you sure that getLastCreatedActor() is returning a separate instance each time? If it's returning the same instance each time you will likely see what you're getting.

  • Yes. In the first log you can see that each map key holds a different actor. – sdieters Jul 06 '17 at 12:08
  • But if it's the same instance being returned and renamed per step in the initial loop you get the same output. – Christopher Mandlbaur Jul 06 '17 at 12:11
  • It's not the same instance. Actor1, Actor2 and Actor3, are 3 different instances. And you can see clearly in the first log that it adds these 3 DIFFERENT actors to my map with different keys. – sdieters Jul 06 '17 at 12:12
1

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

  • They don't. Check the first log. You can clearly see that the keys "Thumbstick1, Thumbstick2, Thumbstick3" are holding "Actor1, Actor2, Actor3". This is the moment where they are added to my map. – sdieters Jul 06 '17 at 12:09