Use Haxe API classes explicitly

≡放荡痞女 提交于 2019-12-07 05:54:17

问题


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


回答1:


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




回答2:


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;.


来源:https://stackoverflow.com/questions/31222219/use-haxe-api-classes-explicitly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!