A MoonBit JavaScript binding for Phaser 3 scenes, sprites, animation, audio, camera, tilemaps, input, text, and Arcade Physics.
git clone https://github.com/Ridge-Lab/MoonPhaserKit.git
cd MoonPhaserKit
moon check --target js
moon test --target jsmoon publishmoon add Ridge-Lab/moonphaserkitimport Phaser from "phaser";
globalThis.Phaser = Phaser;
await import("./generated/moon-jumper.js");fn main {
let config = @moonphaserkit.GameConfig::new(parent="game")
ignore(@moonphaserkit.Game::create(
config,
scene => {
scene.load_image(@moonphaserkit.ImageAsset::new("player", "/assets/player.svg"))
scene.load_spritesheet(
@moonphaserkit.SpriteSheetAsset::new("hero-run", "/assets/hero-run.png", 32, 48),
)
scene.load_audio(@moonphaserkit.AudioAsset::new("jump", "/assets/jump.ogg"))
},
scene => {
scene.create_animation(
@moonphaserkit.AnimationConfig::new(
"run",
"hero-run",
@moonphaserkit.FrameRange::new(0, 7),
frame_rate=14,
repeat_mode=@moonphaserkit.AnimRepeatForever,
),
)
let player = scene.add_sprite(120.0, 220.0, "player")
player.apply_options(@moonphaserkit.SpriteOptions::new(collide_world_bounds=true))
player.play_animation("run")
scene.main_camera().start_follow(player)
ignore(scene.delayed_call(@moonphaserkit.DurationMs::seconds(1), () => {
player.set_tint(0xffcc00)
}))
},
_scene => (),
))
}cd examples/moon-jumper-web
corepack enable
pnpm install --frozen-lockfile
pnpm run devmoon check --target js
moon test --target js
cd examples/moon-jumper-web
corepack enable
pnpm install --frozen-lockfile
pnpm run build
pnpm run smoke| 类别 | 公开能力 |
|---|---|
| 游戏与场景 | GameConfig、Game、Scene,支持 Phaser 游戏创建、销毁和场景生命周期。 |
| 场景控制 | SceneControlCommand、start_scene、launch_scene、pause_scene、resume_scene、stop_scene、sleep_scene、wake_scene。 |
| 时间事件 | TimerEventConfig、TimerEvent、delayed_call、add_timer_event,支持延迟、重复和循环回调。 |
| 资源加载 | ImageAsset、TextureAsset、SpriteSheetAsset、TextureAtlasAsset、AudioAsset、TilemapJsonAsset、BitmapFontAsset。 |
| 显示对象 | Sprite、Text、DisplayObjectOptions、TextStyle、SpawnSpec,支持位置、缩放、透明度、层级、文字样式、显示和销毁。 |
| 动画 | AnimationConfig、FrameRange、AnimationPlayOptions、TweenConfig、SpriteTween。 |
| 音频 | Sound、SoundOptions、SoundMarker、AudioFade、AudioBusState。 |
| 输入 | CursorKeys、Key、Pointer、PointerSnapshot、TouchZone、SwipeSnapshot、KeyBinding。 |
| 物理与碰撞 | ArcadeBody、Collider、PhysicsMaterial、TileCollisionRange、TilePropertyMatcher。 |
| 相机 | Camera、CameraBounds、CameraFollowOptions、CameraFadeOptions、CameraShakeOptions、CameraPanOptions、CameraZoomOptions。 |
| Tilemap | Tilemap、Tileset、TileLayer、TilemapConfig、TilesetConfig、TileLayerConfig、TileObject、TileSpawnPoint。 |
| 工程支撑 | RuntimeCompatibility、IntegrationChecklist、BrowserSmokeScenario、ErrorPathExpectation、FfiCallRecord。 |
pub(all) struct AnimationConfig {
key : String
texture_key : String
frames : FrameRange
frame_rate : Int
repeat_mode : AnimationRepeatMode
repeat_count : Int
yoyo : Bool
delay_ms : Int
} derive(Eq, Debug)fn AnimationConfig::new(key : String, texture_key : String, frames : FrameRange, frame_rate? : Int, repeat_mode? : AnimationRepeatMode, repeat_count? : Int, yoyo? : Bool, delay_ms? : Int) -> AnimationConfigfn AnimationPlayOptions::new(ignore_if_playing? : Bool, start_frame? : Int, delay_ms? : Int, repeat_override? : Int) -> AnimationPlayOptionsfn AnimationStateSnapshot::new(key : String, is_playing : Bool, progress : Double, frame_index : Int) -> AnimationStateSnapshot#external
pub type ArcadeBodyfn AssetManifestEntry::new(kind : AssetKind, key : String, primary_path : String, secondary_path? : String) -> AssetManifestEntrypub(all) struct BrowserSmokeScenario {
name : String
viewport : BrowserViewport
canvas : CanvasExpectation
initial_wait_ms : Int
fail_on_console_error : Bool
fail_on_http_error : Bool
} derive(Eq, Debug)fn BrowserSmokeScenario::new(name : String, viewport : BrowserViewport, canvas? : CanvasExpectation, initial_wait_ms? : Int, fail_on_console_error? : Bool, fail_on_http_error? : Bool) -> BrowserSmokeScenariopub(all) struct BrowserViewport {
kind : BrowserViewportKind
width : Int
height : Int
} derive(Eq, Debug)#external
pub type Camerafn CameraFadeOptions::new(duration_ms? : Int, red? : Int, green? : Int, blue? : Int, force? : Bool) -> CameraFadeOptionsfn CameraFollowOptions::new(lerp_x? : Double, lerp_y? : Double, offset_x? : Double, offset_y? : Double, deadzone_width? : Double, deadzone_height? : Double) -> CameraFollowOptionsfn CameraPanOptions::new(target : Point2, duration_ms? : Int, ease? : EaseKind, force? : Bool) -> CameraPanOptionsfn CameraShakeOptions::new(duration_ms? : Int, intensity? : Double, force? : Bool) -> CameraShakeOptionsfn CanvasExpectation::new(min_width? : Int, min_height? : Int, allow_mobile_fit? : Bool, require_non_blank? : Bool) -> CanvasExpectation#external
pub type CursorKeyspub(all) struct DisplayObjectOptions {
position : Point2
origin : OriginPoint
scale_x : Double
scale_y : Double
rotation : Double
alpha : Double
depth : Int
visible : Bool
active : Bool
} derive(Eq, Debug)fn DisplayObjectOptions::new(position? : Point2, origin? : OriginPoint, scale_x? : Double, scale_y? : Double, rotation? : Double, alpha? : Double, depth? : Int, visible? : Bool, active? : Bool) -> DisplayObjectOptionspub(all) struct ErrorPathExpectation {
kind : ErrorPathKind
operation : String
expected_message : String
} derive(Eq, Debug)fn ErrorPathExpectation::new(kind : ErrorPathKind, operation : String, expected_message : String) -> ErrorPathExpectationpub(all) struct FeatureSupport {
feature : RuntimeFeature
supported : Bool
notes : String
} derive(Eq, Debug)fn FeatureSupport::new(feature : RuntimeFeature, supported : Bool, notes? : String) -> FeatureSupportpub(all) struct FfiCallRecord {
feature : RuntimeFeature
operation : String
success : Bool
failure_code : String
} derive(Eq, Debug)fn FfiCallRecord::new(feature : RuntimeFeature, operation : String, success : Bool, failure_code? : String) -> FfiCallRecord#external
pub type Gamefn GameConfig::new(parent? : String, width? : Int, height? : Int, background? : String, gravity_y? : Int, debug? : Bool) -> GameConfigfn IntegrationChecklist::new(has_moonbit? : Bool, has_node? : Bool, has_phaser? : Bool, exposes_global_phaser? : Bool, builds_generated_js? : Bool, imports_generated_js? : Bool, smoke_test_passes? : Bool) -> IntegrationChecklistpub(all) struct IntegrationStep {
kind : IntegrationStepKind
title : String
required_for_demo : Bool
required_for_package : Bool
} derive(Eq, Debug)fn IntegrationStep::new(kind : IntegrationStepKind, title : String, required_for_demo? : Bool, required_for_package? : Bool) -> IntegrationStepfn InteractionScenarioSummary::new(name : String, step_count : Int, covers_keyboard? : Bool, covers_pointer? : Bool, covers_collision? : Bool, covers_audio? : Bool, covers_camera? : Bool) -> InteractionScenarioSummarypub(all) struct InteractionStep {
kind : InteractionStepKind
value : String
point : Point2
duration_ms : Int
} derive(Eq, Debug)fn InteractionStep::new(kind : InteractionStepKind, value? : String, point? : Point2, duration_ms? : Int) -> InteractionStep#external
pub type Keypub(all) struct PhysicsMaterial {
kind : PhysicsMaterialKind
bounce_x : Double
bounce_y : Double
gravity_y : Double
immovable : Bool
sensor : Bool
} derive(Eq, Debug)fn PhysicsMaterial::new(kind? : PhysicsMaterialKind, bounce_x? : Double, bounce_y? : Double, gravity_y? : Double, immovable? : Bool, sensor? : Bool) -> PhysicsMaterial#external
pub type Pointerpub(all) struct PointerSnapshot {
id : Int
position : Point2
world_position : Point2
is_down : Bool
just_down : Bool
button : PointerButton
} derive(Eq, Debug)fn PointerSnapshot::new(id : Int, position : Point2, world_position : Point2, is_down : Bool, just_down : Bool, button? : PointerButton) -> PointerSnapshotpub(all) struct RuntimeCompatibility {
backend : MoonBitBackend
phaser : PhaserVersion
browser : BrowserTarget
requires_global_phaser : Bool
} derive(Eq, Debug)fn RuntimeCompatibility::new(backend? : MoonBitBackend, phaser? : PhaserVersion, browser? : BrowserTarget, requires_global_phaser? : Bool) -> RuntimeCompatibility#external
pub type Scenefn Scene::add_timer_event(self : Scene, config : TimerEventConfig, callback : () -> Unit) -> TimerEventpub(all) struct SceneControlCommand {
action : SceneControlAction
target_scene : String
} derive(Eq, Debug)fn SceneControlCommand::new(action : SceneControlAction, target_scene : String) -> SceneControlCommandpub(all) struct SceneLayerConfig {
kind : SceneLayerKind
name : String
depth : Int
scroll_factor_x : Double
scroll_factor_y : Double
visible : Bool
} derive(Eq, Debug)fn SceneLayerConfig::new(kind : SceneLayerKind, name? : String, depth? : Int, scroll_factor_x? : Double, scroll_factor_y? : Double, visible? : Bool) -> SceneLayerConfigpub(all) struct SceneTransition {
from_scene : String
to_scene : String
fade : CameraFadeOptions
restart_current : Bool
} derive(Eq, Debug)fn SceneTransition::new(from_scene : String, to_scene : String, fade? : CameraFadeOptions, restart_current? : Bool) -> SceneTransition#external
pub type Soundpub(all) struct SoundMarker {
name : String
start : Double
duration : Double
config : SoundOptions
} derive(Eq, Debug)fn SoundMarker::new(name : String, start : Double, duration : Double, config? : SoundOptions) -> SoundMarkerpub(all) struct SoundOptions {
volume : Double
rate : Double
detune : Int
seek : Double
delay_ms : Int
loop_mode : SoundLoopMode
} derive(Eq, Debug)fn SoundOptions::new(volume? : Double, rate? : Double, detune? : Int, seek? : Double, delay_ms? : Int, loop_mode? : SoundLoopMode) -> SoundOptionspub(all) struct SpawnSpec {
key : String
position : Point2
display : DisplayObjectOptions
physics : PhysicsMaterial
layer : SceneLayerKind
} derive(Eq, Debug)fn SpawnSpec::new(key : String, position : Point2, display? : DisplayObjectOptions, physics? : PhysicsMaterial, layer? : SceneLayerKind) -> SpawnSpec#external
pub type Spritefn SpriteOptions::new(scale? : Double, immovable? : Bool, collide_world_bounds? : Bool, bounce_x? : Double, bounce_y? : Double, gravity_y? : Double) -> SpriteOptionsfn SpriteSheetAsset::new(key : String, path : String, frame_width : Int, frame_height : Int, margin? : Int, spacing? : Int) -> SpriteSheetAssetpub(all) struct SpriteTween {
from : Point2
to : Point2
alpha_from : Double
alpha_to : Double
scale_from : Double
scale_to : Double
config : TweenConfig
} derive(Eq, Debug)fn SpriteTween::new(from : Point2, to : Point2, alpha_from? : Double, alpha_to? : Double, scale_from? : Double, scale_to? : Double, config? : TweenConfig) -> SpriteTweenpub(all) struct SwipeSnapshot {
start : Point2
current : Point2
elapsed_ms : Int
config : SwipeConfig
} derive(Eq, Debug)fn SwipeSnapshot::new(start : Point2, current : Point2, elapsed_ms : Int, config? : SwipeConfig) -> SwipeSnapshot#external
pub type Textfn TextureAtlasAsset::new(key : String, texture_path : String, atlas_path : String) -> TextureAtlasAssetfn TileCollisionRange::new(start_index : Int, end_index : Int, collides? : Bool) -> TileCollisionRange#external
pub type TileLayerfn TileLayerConfig::new(layer_name : String, tileset_name : String, x? : Double, y? : Double, visible? : Bool, alpha? : Double) -> TileLayerConfigfn TileObject::new(name : String, object_type : String, position : Point2, size : Size2) -> TileObjectfn TilePropertyMatcher::new(property : String, value : String, collides? : Bool) -> TilePropertyMatcherpub(all) struct TileSpawnPoint {
name : String
position : Point2
facing : AnimationDirection
} derive(Eq, Debug)fn TileSpawnPoint::new(name : String, position : Point2, facing? : AnimationDirection) -> TileSpawnPoint#external
pub type Tilemappub(all) struct TilemapConfig {
key : String
tile_width : Int
tile_height : Int
width : Int
height : Int
orientation : TilemapOrientation
} derive(Eq, Debug)fn TilemapConfig::new(key : String, tile_width : Int, tile_height : Int, width? : Int, height? : Int, orientation? : TilemapOrientation) -> TilemapConfigfn TilesetConfig::new(name : String, texture_key : String, tile_width : Int, tile_height : Int, margin? : Int, spacing? : Int) -> TilesetConfig#external
pub type TimerEventpub(all) struct TimerEventConfig {
delay_ms : Int
repeat_mode : TimerRepeatMode
repeat_count : Int
start_at_ms : Int
time_scale : Double
paused : Bool
} derive(Eq, Debug)fn TimerEventConfig::new(delay_ms? : Int, repeat_mode? : TimerRepeatMode, repeat_count? : Int, start_at_ms? : Int, time_scale? : Double, paused? : Bool) -> TimerEventConfigfn TweenConfig::new(duration_ms? : Int, delay_ms? : Int, ease? : EaseKind, yoyo? : Bool, repeat_count? : Int) -> TweenConfigpub(all) struct ValidationIssue {
severity : ValidationSeverity
code : String
message : String
} derive(Eq, Debug)fn ValidationIssue::new(severity : ValidationSeverity, code : String, message : String) -> ValidationIssueA MoonBit JavaScript binding for Phaser 3 scenes, sprites, animation, audio, camera, tilemaps, input, text, and Arcade Physics.