Okay, first I’m going to try to address your actual question and then I’ll comment on the rest of your code.
In Hud
, I think you want to do the following:
display.scrollX = 0;
display.scrollY = 0;
to make them stay in place even when the camera moves.
In HudCoords
, you’ll want to do similar:
txt.scrollX = 0;
txt.scrollY = 0;
Now, about the code in general.
Main.as
Your Main
class looks okay. Typically I don’t like using the *
selector in my import
s but that’s completely personal preference.
GameWorld.as
Now, GameWorld
. Personal preference again, but I usually create a separate Assets
class to statically store all my Embed
s but that’s totally up to you; it just seems cleaner and easier to me.
I would recommend you override World
's begin()
function and initialize all your variables there. This is based off a note I heard from someone once that a lot of compilers will not optimize initializer functions but will optimize normal functions (like begin()
). Either way, it doesn’t cost anything, and this way you can be sure that all the World
variables are initialized and your World
is ready to go before you start changing variables.
Your time
variable is unused.
I really don’t understand why you’ve opted for a getter function of hudWindow
when it simply returns textBox
. Usually getters are used either when you want to make a private
property read-only, or if some special calculation needs to be done before returning the variable.
In this case, there’s no reason why you can’t just rename textBox
to be hudWindow
. This is what I would recommend; there’s no point writing a getter and complicating your code if a variable will do.
In your update()
function, you call super.update()
at the very beginning. Usually you want this at the end instead, and I think you do in this case as well.
When checking for inputs in the way it seems your game works, you usually want to use if ... else
statements rather than a bunch of if
s stacked together. Not only will this unmeasurably improve your performance, it more importantly will prevent the player from pressing multiple keys in one frame and potentially messing things up - although with your current code, it looks like this wouldn’t really cause a problem.
I wouldn’t typically use switch
statements like this, and would use if ... else
instead, but your code technically works. I think, as your code gets more complex, switch
statements like this will begin to weigh you down. But it’s up to you.
With the new modifications to Hud
, you no longer need to mess with hudWindow
's coordinates at the end of your update()
function.
Player.as
Instead of having Player
take a Point
as an argument, I would use x
and y
as two Number
arguments, but that’s just how I do things. It would save you from having to create (and import) Point
in every class where you want to add a Player
object.
You should call Draw.resetTarget()
after you’re done using it for the Player
class.
Same notes apply as before with super.update()
and keyboard inputs.
Now, a note about how you’ve structured your Hud
classes.
I don’t see why you need 2 separate classes for this. Why not create a single Hud
class which draws both the background and the text?
Okay, moving on…
HudCoords.as
This class is honestly a bit of a mess. As far as I can tell, either you edited it before posting, or it just doesn’t work and shouldn’t compile.
You change the color
of splashText
, which doesn’t exist. Presumably you mean txt
?
You don’t need to change an Entity
's graphic
to draw to an Entity
. You can use addGraphic()
instead. Note these do two different things but end up with the same result; it’s your call.
You’re missing a semicolon after layer = -3
.
addText
, I think, should have this:
txt.text = coords;
instead of your current
Text(graphic).text = coords;
which I suspect doesn’t work (unless there’s some crazy casting going on here that I’m not understanding).
Hud.as
This looks fine.
A general note:
It could have something to do with pasting it into a forum, but your code looks very messy. There’s inconsistent tabbing and lots of random whitespace. This, of course, compiles just fine, but it’s difficult to read and will become difficult to manage as you write a larger volume of code. I would recommend cleaning it up now before it gets out of control. (A lot of IDEs/programming text editors have a built-in function to automatically tidy up for you, though obviously you won’t have the same level of control as doing it yourself.)
Hope this helps! Sorry to barrage you with a giant wall of text, I hope you can at least learn something from it though. Let me know if you have any questions or want things explained further.