Egret

Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎。Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏和Android,iOS,WinPhone原生游戏。

WebPack

webpack是一款模块加载器兼打包工具,它能把各种web开发中常用到的静态资源,包括JS(含JSX)、CoffeeScript、TypeScript、样式(含less/sass)、图片等都作为模块来进行统一的管理以及打包发布,其可以兼容多种js书写规范,且可以处理模块间的依赖关系,具有更强大的js模块化的功能。 其官方主页用下面这张图来说明Webpack的作用:

TypeScript

TypeScript是一种由微软开发的自由和开源的编程语言。它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程。

TypeScript扩展了 JavaScript 的句法,所以任何现有的JavaScript程序可以不加改变的在TypeScript下工作。TypeScript是为大型应用之开发而设计,而编译时它产生 JavaScript 以确保兼容性。

开发环境搭建

首先还是要保证安装了 nodejs,然后通过 npm 安装程序。

https://nodejs.org/en/download/

项目初始化

创建项目目录

初始化 package.json

npm init

安装WebPack

npm install --save-dev webpack

安装 awesome-typescript-loader

npm install --save-dev awesome-typescript-loader

安装 strip-sourcemap-loader

npm install --save-dev strip-sourcemap-loader

配置Egret

Egret引擎的GitHub开源地址 https://github.com/egret-labs/egret-core,可以在此获取到最新的Egret源码。

解压后将egret目录下build、src目录拷贝到项目目录的libs/egret下

安装http-server

npm install --save-dev http-server

配置TypeScript

在项目目录下新建tsconfig.json

{
"compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
},
"files": [
],
"exclude": [
    "node_modules"
],
"compileOnSave": false,
"buildOnSave": false

}

创建HelloWorld应用

项目目录的apps下创建应用目录app_01_hello_world,并创建以下文件:

app/app.ts

/// <reference path="../../../libs/egret/build/egret/egret.d.ts"/>
/// <reference path="../../../libs/egret/build/tween/tween.d.ts"/>
/// <reference path="../../../libs/egret//build/res/res.d.ts"/>
class App extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private onAddToStage(event: egret.Event) {
        this.createGameScene();
    }

    private createGameScene(): void {
        var stageW: number = this.stage.stageWidth;
        var stageH: number = this.stage.stageHeight;
        var colorLabel: egret.TextField = new egret.TextField();
        colorLabel.textColor = 0xffffff;
        colorLabel.textAlign = "center";
        colorLabel.text = "Hello World!";
        colorLabel.size = 40;
        colorLabel.x = stageW - colorLabel.width >> 1;
        colorLabel.y = (stageH - colorLabel.height >> 1) + 50;
        this.addChild(colorLabel);
    }
}
egret.registerClass(App, "App");
window["App"] = App;

index.html

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="full-screen" content="true"/>
    <meta name="screen-orientation" content="portrait"/>
    <meta name="x5-fullscreen" content="true"/>
    <meta name="360-fullscreen" content="true"/>
    <style>
    html, body {
        -ms-touch-action: none;
        background: #888888;
        padding: 0;
        border: 0;
        margin: 0;
        height: 100%;
    }
    </style>
    <script src="../../libs/egret/build/egret/egret.js"></script>
    <script src="../../libs/egret/build/egret/egret.web.js"></script>
    <script src="../../libs/egret/build/game/game.js"></script>
    <script src="../../libs/egret/build/game/game.web.js"></script>
    <script src="../../libs/egret/build/tween/tween.js"></script>
    <script src="../../libs/egret/build/res/res.js"></script>
    <script src="build/app.bundle.js"></script>
</head>
<body>
    <div style="margin: auto; width: 100%;height: 100%;" class="egret-player"
     data-entry-class="App"
     data-orientation="auto"
     data-scale-mode="noScale"
     data-frame-rate="30"
     data-content-width="640"
     data-content-height="960"
     data-show-paint-rect="false"
     data-multi-fingered="2"
     data-show-fps="false" data-show-log="false"
     data-log-filter="" data-show-fps-style="x:0,y:0,size:30,textColor:0x00c200,bgAlpha:0.9">
    </div>
    <script type="">
    egret.runEgret();
    </script>
</body>
</html>

webpack.config.js

var path = require('path');
module.exports = {
     entry: {
        'app': [
            path.resolve('app/app')
        ]
    },
    output: {
        path: path.resolve('build'),
        filename: '[name].bundle.js',
        pathinfo: false // show module paths in the bundle, handy for debugging
    },
    module: {
    loaders: [{
        test: /\.ts$/,
        loader: 'awesome-typescript',
        query: {
        'doTypeCheck': true
        },
        include: path.resolve('app'),
        exclude: /node_modules/
    }],
    noParse: [

    ]
    },
    resolve: {
    alias: {
    },
    extensions: ["", ".js", ".ts"]
    },
    plugins: [
    ]
};

编译及测试

编译,在app_01_hello_world下运行

 ..\..\node_modules\.bin\webpack

运行,在项目目录下运行

.\node_modules\.bin\http-server 

浏览器打开http://localhost:8080/apps/app_01_hello_world/

其他

Egret应用开发实践(01) Egret与WebPack的更多相关文章

  1. Spring Cloud开发实践 - 01 - 简介和根模块

    简介 使用Spring Boot的提升主要在于jar的打包形式给运维带来了很大的便利, 而Spring Cloud本身的优点不是那么明显, 相对于Dubbo而言, 可能体现在跨语言的交互性上(例如可以 ...

  2. Egret飞行模拟-开发记录01

    1.项目结构简介 1.1 index.html:应用入口文件,我们可以在这里面配置项目的旋转缩放模式背景颜色等. 1.2 egretProperties.json:这个文件里面进行项目配置,包括模块和 ...

  3. Android游戏开发实践(1)之NDK与JNI开发01

    Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...

  4. Html5 Egret游戏开发 成语大挑战(九)设置界面和声音管理

    在上一篇中,简单的使用界面元素快速实现了一个游戏中的二级页面,这种直接在游戏页面上做UI的做法并不太好,原因是,UI会让游戏的压力变大,即使它是隐蔽的,如果同样的功能在其它的地方也是一样的,那么就要写 ...

  5. Html5 Egret游戏开发 成语大挑战(二)干净的eui项目和资源准备

    现在我们使用egret来起步开发一个名叫<成语大挑战>的小游戏,关于egret的开发环境就不在这里啰嗦了,直接去官方下载安装就可,egret是我见过开发环境部署最简单的解决方案,这个系列教 ...

  6. Html5 Egret游戏开发 成语大挑战(一)开篇

    最近接触了Egret白鹭引擎,感觉非常好用,提供了各种各样的开发工具让开发者和设计者更加便捷,并且基于typescript语言开发省去了很多学习成本,对于我们这种掉微软坑许久的童鞋来说,确实很有吸引力 ...

  7. Egret 之 消除游戏 开发 PART 6 Egret elimination game development PART 6

    Egret 之 消除游戏 开发 PART 6 Egret elimination game development PART 6 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱: ...

  8. 使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等

    "使用Bootstrap 3开发响应式网站实践"系列,将使用Bootstrap 3.2制作一个自适应网站,无论是在电脑.平板,还是手机上,都呈现比较好的效果.在电脑浏览器上的最终效 ...

  9. Egret白鹭开发微信小游戏排行榜功能

    推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 我的个人博客 最近事情特别多,今天终于实现了排行榜功能,记录下来大家一起学习学习. 一.调用默认排行榜 首先我们需要了解: 1.白鹭开 ...

随机推荐

  1. Chp4: Trees and Graphs

    1.Type of Tree 1. Binary Tree: a binary tree is a tree in which each node has at most two child node ...

  2. 【leetcode】Majority Element (easy)(*^__^*)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. node.js 安装、图文详解

    网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...

  4. java基础知识回顾之javaIO类--File类

    File类是对文件系统中文件以及目录(文件夹)进行封装的对象,可以通过面向对象的思想来操作文件和目录(文件夹).File类保存文件或目录的各种元素的信息,包括文件名,文件长度,最后修改日期,是否可读, ...

  5. hdu 1063 Exponentiation

    求实数的幂,这个用C++写的话有点长,但是用Java写就非常方便了…… );            System.out.println(an);        }    }}

  6. Java运行系统命令并获取值(Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)

    package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. mysql23个知识点

    1.它是一种解释语言:写一句执行一句,不需要整体编译执行. 2.1.没有“ ”,字符串使用‘ '包含 3.一个表只有一个主键,但是一个主键可以是由多个字段组成的 组合键 4.实体完整性:实体就是指一条 ...

  9. python 下划线的使用(转载:安生犹梦 新浪博客)

    Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx      不能用'from module import *'导入 __xxx__ 系统定义名字 __xxx    类中的私有变量名 核 ...

  10. 获取其它进程窗口中的状态栏信息(FindWindowEx GetWindowThreadProcessId OpenProcess SendMessage轮番轰炸)

    HWND hWnd = ::FindWindow(NULL, _T("XXXXX")); if(NULL == hWnd) { return ; } HWND hWndStatus ...