How do I access a variable from another FlxSprite class?



  • Hi. This is a bit of a beginner question, but I'm asking because I cannot find a clear answer through tutorials or open source projects I sift through.

    The position of one of my game's objects relies on the position of another game object, what is the easiest way to access the x of that object in a different flxsprite class? [sprite].x gives me an error, and unless something is seriously wrong with my project, this is to be expected. My current solution is to make a public float in the second flxsprite class, and set it to the x of the first flxsprite in the playstate. I assume that there must be a more convenient solution.



  • The solution of [sprite].x should work fine. Maybe you are not creating an instance of the sprite? Would it be possible to see some code?



  • Anything extending FlxSprite already has a x and y variable that you can read/write!
    So you don't need to create a new public float when extending a FlxSprite :)
    For example:

    var player:FlxSprite = new FlxSprite(100, 100);
    add(player);
    var pet:FlxSprite = new FlxSprite(0, 0);
    add(pet);
    
    if (pet.x != player.x)
    {
        pet.x = player.x;
    }
    

    Hope this helps! :)


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.