expo-phaser

Build awesome 2D games with Phaser.js and Expo
授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发、 手机游戏
软件类型 开源软件
地区 不详
投 递 者 徐鸿文
操作系统 Android
开源组织
适用人群 未知
 软件概览

NPM

expo-phaser

Tools for using Phaser-ce to build native 2D games in Expo ��

Installation

yarn add expo-phaser

Usage

Import the library into your JavaScript file:

import ExpoPhaser from "expo-phaser";

Functions

ExpoPhaser.game({ context: WebGLRenderingContext, ...extras })

Given a context from anExpo.GLView, return aPhaser.Gamethat draws into it.

Props

Property Type Description Default Value
context WebGLRenderingContext Required: context that the Phaser.Game will render to null
width number? Optional: height of the Phaser.Game context.drawingBufferWidth
height number? Optional: width of the Phaser.Game context.drawingBufferHeight
title string? Optional: title of the Phaser.Game "expo-phaser-game"
preventLoop boolean? Optional: Prevents the app from calling context.endFrameEXP() every frame false

Returns

Property Type Description
game Phaser.Game The Phaser-ce game used for rendering game logic

Example

const game = ExpoPhaser.game({ context });

What does it do?

Under the hood, ExpoPhaser is maintaining global instances of a few libraries.

window.PIXI = require("phaser-ce/build/custom/pixi");
window.p2 = require("phaser-ce/build/custom/p2");
window.Phaser = require("phaser-ce/build/phaser");

Other libs can be included but are not required. For instance you can import the custom Creature lib the same way.We also override the PIXI.WebGLRenderer.updateTexture to make it compatible with Expo.

Finally when a new instance of Expo.Game is created, we set the document.readyState to 'complete' and save the global instance of context

global.__context = context;
global.document.readyState = "complete";

Then we create a standard render loop and call context.endFrameEXP() to flush the frame queue and render our context through EXGL.

const render = () => {
  requestAnimationFrame(render);
  context.endFrameEXP();
};

Example

It's important to note that you must preload all of your assets before starting the app, as the Phaser.State.preload method cannot be asynchronous.Creating a game in Expo is very simple with ExpoPhaser, we preload our assets, create a view, initialize our game, then add our assets.

We create an Expo.GLView to render our game to.

return (
  <Expo.GLView
    style={{ flex: 1 }}
    onContextCreate={context => startGame({ context })}
  />
);

Then we create our Phaser.Game instance and assign it a playable state. We can then choose to start said state.

function startGame({ context }) {
  const game = ExpoPhaser.game({ context });

  game.state.add("Playable", {
    preload: function() {
      /// This function cannot be async, preload all assets before getting here.
      game.load.image(
        "man",
        Expo.Asset.fromModule(Assets["man.json"]).localUri
      );
    },
    create: function() {},
    update: function() {}
  });

  game.state.start("Playable");
}

Preloading

In React Native all assets must be static resources, because of this we must create a reference to all the assets we may use, then download them and get their local URI.Expo has a convenient way of saving reference. We preload an Expo.Asset then if we create the same instance later we can simple call asset.localUri.

In a standard Phaser app we would load an asset like this:

game.load.image("man", "./assets/man.png");

In expo we would load it like this:

const preloadedExpoAsset = Expo.Asset.fromModule(require('./assets/man.png'))
await preloadedExpoAsset.downloadAsync();

...

game.load.image('man', preloadedExpoAsset.localUri);

All together

This example shows how to load an animated texture atlas and apply arcade physics to it.

import React from "react";
import Expo from "expo";
import ExpoPhaser from "expo-phaser";

const Assets = {
  "man.png": require("./assets/man.png"),
  "man.json": require("./assets/man.json")
};

export default class App extends React.Component {
  state = { loading: true };
  async componentWillMount() {
    const downloads = [];
    for (let key of Object.keys(Assets)) {
      const asset = Expo.Asset.fromModule(Assets[key]);
      downloads.push(asset.downloadAsync());
    }
    await Promise.all(downloads);
    this.setState({ loading: false });
  }
  render() {
    if (this.state.loading) {
      return <Expo.AppLoading />;
    }

    return (
      <Expo.GLView
        style={{ flex: 1 }}
        onContextCreate={context => startGame({ context })}
      />
    );
  }
}

function startGame({ context }) {
  const game = ExpoPhaser.game({ context });

  game.state.add("Playable", {
    preload: function() {
      const atlas = Expo.Asset.fromModule(Assets["man.json"]).localUri;
      const texture = Expo.Asset.fromModule(Assets["man.png"]).localUri;
      game.load.atlasJSONHash("man", texture, atlas);
    },
    create: function() {
      game.stage.backgroundColor = "#4488AA";

      game.physics.startSystem(Phaser.Physics.ARCADE);

      //  Set the world (global) gravity
      game.physics.arcade.gravity.y = 100;

      const man = game.add.sprite(200, 200, "man");
      game.physics.enable([man], Phaser.Physics.ARCADE);

      //  Here we add a new animation called 'run'
      //  We haven't specified any frames because it's using every frame in the texture atlas

      man.animations.add("run");
      man.body.collideWorldBounds = true;
      man.body.bounce.y = 0.8;
      man.body.gravity.y = 200;

      //  And this starts the animation playing by using its key ("run")
      //  15 is the frame rate (15fps)
      //  true means it will loop when it finishes
      man.animations.play("run", 15, true);
    },
    update: function() {}
  });

  game.state.start("Playable");
}

note: When working with .json asset inclusion, be sure to update the app.json file to handle .json appropriately.

"packagerOpts": {
  "assetExts": [
    "json"
  ]
},

Demo

Within this repo is an examples/basic demo.

  • NumberTweenBuilderConfig Phaser.Tweens.Tween Phaser.Math.DegToRad(deg) // 将给定的角度从度数转换为以弧度为单位的等效角度。 var tween = this.tweens.add(...); // 添加动画 tween.stop(); // 停止动画 tween.resume(); // 恢复之前暂停的 Tween 的播放。

 相关资料
  • Expo 包含一组工具、库和服务,可以让你用 JavaScript 构建 Android 和 iOS 的原生应用。该开源项目主要是客户端软件,包括客户端 app、模块、应用等。 支持访问设备功能,如摄像头、定位、通知、传感器等,并且这些接口是跨平台的。你无需使用 Xcode 或者 Android Studio 就可以生成直接在应用商店发布的应用。支持 OTA 更新,无需应用商店审核。 使用 Exp

  • Tools for creating, running, and deploying Universal Expo and React Native apps �� Read the Documentation | Contributing to Expo CLI �� Documentation �� Project Layout �� Badges �� Contributing ❓ FAQ

  • Expo Voxel Tutorial �� Watch the video tutorial: Youtube �� Stack �� Expo React Native THREE.js Special Thanks �� Voxel Terrain by Mr. Doob Nikhilesh Sigatapu Charlie Cheever Expo Team

  • 我是一名经验丰富的android开发者,并开始学习react native。我正在用ubuntu做一个项目。我使用了像这样的代码,并使用visualstudio代码作为代码编辑器。 要在我的物理设备上运行一个项目,我在终端上运行,将目录更改为项目目录,然后我开始localhost如,然后我通过此代码在android上启动我的项目。该项目在我的物理设备上运行良好。 但是当我使用展览时,我得到了一个错

  • 我已经用Expo创建了应用程序。 一切都很好,我可以在ios和android模拟器上运行应用程序。 当我使用命令获得apk文件时。 我设法从设备上得到了一个堆栈跟踪,下面是它所说的。 Expo SDK需要Expo才能运行。本机Expo模块似乎不可用,此代码未在Expo上运行。访问https://docs.Expo.io以了解有关开发Expo项目的更多信息。 版本信息 因此,我无法将我的应用程序发布