Error with the "Hello World" tutorial.



  • This post is deleted!


  • Aside from that, everything seems to be working. Thank you so much. ^^

    Also, on the "Groundwork" tutorial, I think you forgot to mention that you need to add 'import flixel.FlxG;"
    I know it's supposed to do it automatically, but for some reason it's not doing that on my PC.

    I do have a few other questions, but they're mainly just "How do I do this?" rather than something not working.



  • @vulpicula

    This cheat-sheet has a few examples of common things.

    I found it helpful to check out the demos and try things with the code to see how things work.



  • @dean I've been doing that so far. A bit of splicing code in and out as well has helped a ton. I don't quite understand some of the code behind it, but I'm starting to understand what does what.

    Only question I'm struggling with, is "How do I add a text field so the user can type something in, that then gets saved to a string?"



  • @vulpicula

    You'll need to use flixel-addons for that.

    enable flixel-addons in Project.xml

    <haxelib name="flixel-addons" />
    

    Then import this in the class you want to use it in.

    import flixel.addons.ui.FlxInputText; 
    

    Define the input textbox as private var so you can use it in other functions in the class.

    private var _inputText:FlxInputText;
    

    Create and add it in create()

    _inputText = new FlxInputText();
    add(_inputText);
    

    Now whatever gets typed in the box is accessed with
    _inputText.text which is a string that you can use or save in some other function.



  • @dean Thank you! I would've been unable to figure that out on my own most likely.

    Only thing I'm confused about, is why things like enter won't get it to save the string or so.



  • @vulpicula

    Here's some example.

    _inputText = new FlxInputText(10, 10, 100, "Test", 10, 0xFF00FFFF, 0xFF333333);
    _inputText.setFormat(null, 8, 0xFF00FFFF);
    _inputText.setPosition(100, 100);
    _inputText.maxLength = 16;
    _inputText.fieldBorderColor = 0xFFFFFF00;
    _inputText.backgroundColor = 0xFF333333;
    _inputText.fieldBorderThickness = 3;
    _inputText.fieldWidth = 200;
    _inputText.caretColor = 0xFFFF9900;
    _inputText.scrollFactor.set(); // keep it from scrolling with the camera
    add(_inputText);
    // use the callback to save the text when ENTER is pressed
    _inputText.callback = inputTextCallback;
    

    use the callback function to save, etc.

    private function inputTextCallback(text:String, action:String):Void
    {
    	// this is called when the text changes or ENTER is pressed.
    	// text is the text in the textbox
    	// action is one of these: "input" "delete" "backspace" "enter"
    	
    	// when enter is pressed you can save
    	if (action == "enter")
    	{
    		trace(text);
    	}
    }
    


  • @dean

    Got it. I assume I can then take the trace text and write that to a string.





  • @vulpicula

    text is a string and you can do whatever you want with it :) But that's how you get it with the callback.



  • @dean Awesome! One question, though.

    This is actually something I've never been able to do in 'any' programming language. I've always been pretty horrid when it comes to multi-class objects, (in C++ I did the horrible thing and used global ints.)

    How would I, be able to take the string that was generated via the input in that class, and use it in another?
    First attempt revolved around trying something like "private var charName:NameSelectState;" but that doesn't seem to work. Searching around results in nothing. >.<

    If it's impossible, I'll just see if I can find a weird way around it.



  • @vulpicula

    You can define it public static var myVar:String and it will be available in other classes. Or you can pass it to another class as an argument through the new() constructor.

    public static var myInputText:String;
    

    Whatever class you define it in, let's say you define it in PlayState, you access from other classes with

    PlayState.myTextInput;
    

    Usually I keep these static vars in Reg.hx and then access them anywhere with

    Reg.myTextInput;
    

    public static function myFunction() works the same way. Use it in other classes with

    Reg.myFunction();
    

    If you want to pass it as an argument to a class it won't need to be public static.

    private var _inputString:String;
    
    public function new(inputString:String) 
    {
    	super();
    	
    	_inputString = inputString;
    }
    

    and _inputString is available in the class you passed it to.



  • @dean Awesome. And I assume, in theory, if I wanted to at one point change the string, I could just drop the "static" part from the variable creation thing?

    Got it. ^^

    Hopefully this doesn't have the same error as C++, where it'd behave strangely.

    (Such as in, if I set an integer like this to 5 in one class, it'd only show 5 in the class I changed it to, and not show the change in any of the other classes, if that makes any sense.)



  • @vulpicula

    well, static allows access with MyClass.myVar and it is static.

    If you make it public var myVar:String without static, then you access it with

    var myObject = new MyClass();
    var objString = myObject.myVar;
    


  • @vulpicula

    static means it is one instance and in memory. If you change it in one class, the value is changed and other classes use the same instance.

    If you don't want that, you can make it public var and if you have a class for a Barrel, and you add many, those public (non static) vars are unique in each Barrels you add.

    Hope that makes sense.

    private var is only available in the class it's defined in.

    public var is available in other classes but is unique to each class instance for a reused class.

    class Barrel extends FlxSprite
    {
    	public var value:Int;
    
    	public function new(x:Float = 0, y:Float = 0) 
    	{
    		super(x, y);
    	}
    }
    

    ...

    var barrel = new Barrel();
    barrel.value = 10;
    
    var barrel2 = new Barrel();
    barrel2.value = 20;
    

    or you can pass the value in new() as argument and set the public var.

    class Barrel extends FlxSprite
    {
    	public var value:Int;
    
    	public function new(x:Float = 0, y:Float = 0, value:Int) 
    	{
    		super(x, y);
    		
    		// 'this' is needed to distinguish between the public var value and the argument.
    		this.value = value;
    	}
    }
    
    var barrel = new Barrel(50, 50, 10);
    trace(barrel.value); // 10
    
    var barrel2 = new Barrel(100, 50, 20);
    trace(barrel2.value); // 20;
    

    public static var is available anywhere and is only one instance and accessed with ClassName.myVar.



  • @dean Ah, I see, so most of the time I should be using static variables. Gotcha.



  • @vulpicula

    If you want them static, yes :)



  • @Vulpicula

    This topic went off topic. Probably should break down the questions to new topics next time to make it easier for others to find answers.

    You got me with the 'just one question though...', a couple of times :)


  • administrators

    @vulpicula said in Error with the "Hello World" tutorial.:

    so most of the time I should be using static variables

    No, that's usually considered bad practice actually..

    Odd. Even after setting it to use FlashDevelop via the setup, it still insists on running it for Visual Studio Code. (Or more specifically, it defaults to that every time I restart the CMD.)

    That is odd... Sounds like the config is not being saved or something along those lines. Fyi, you can override the configured IDE by using passing the -ide argument, e.g. flixel tpl -name "HelloWorld" -ide fd



  • @dean Yeah, sorry, that's a habit of mine. >.<

    I'll do it in the future.


Log in to reply
 

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