Use Haxe API classes explicitly
Asked Answered
T

2

5

I use Haxe targeting Javascript.

I have a package (defined as an extern), "phaser", that contains a Math class along with many others. I use import phaser.*; at the beginning of my files because I use many classes from this package and I don't want to prefix them all with phaser..

I would like to use the Math class from Haxe API, but if I try to use it (e.g Math.random()), the compiler thinks I want to use phaser.Math and tells me there is no such function in it.

Can I explicitly write that I want to use Haxe Math class and not phaser.Math ?

I've tried haxe.Math but no luck...

Thanks in advance

Termite answered 4/7, 2015 at 15:20 Comment(0)
C
4

Try

import Math as HaxeMath;

then use HaxeMath.* instead of Math.*

Note, nothing special about the name HaxeMath, you could do

import Math as Freddy;

then use Freddy.* instead of Math.*. :p

Calloway answered 4/7, 2015 at 15:34 Comment(1)
Thanks! Even import Math; works and grants higher priority to haxe Math over Phaser's, I just didn't think I needed to import it...Termite
M
4

Two ways to solve it:

  1. Use std.Math. e.g. std.Math.floor(1.1);, or typedef HxMath = std.Math;, or
  2. Add import Math as HxMath; before import phaser.*;. If you're using a haxe version earlier than 3.2, use in instead of as, i.e. import Math in HxMath;.
Madden answered 4/7, 2015 at 15:42 Comment(1)
Thanks, std.Math is exactly what I was looking for. It doesn't appear in autocompletion...Termite

© 2022 - 2024 — McMap. All rights reserved.