HTML5游戏开发引擎
    		       		warning:
    		            这篇文章距离上次修改已过435天,其中的内容可能已经有所变动。
    		        
        		                
                HTML5游戏开发引擎有很多种,比如Phaser、CreateJS、Egret、Cocos Creator等。以下是一些使用这些引擎的示例代码。
- Phaser: - Phaser是一个开源的HTML5游戏开发框架,它提供了丰富的功能,包括2D和3D的渲染,物理系统,声音处理,接口,等等。 
var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    parent: 'game',
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};
 
var game = new Phaser.Game(config);
 
function preload ()
{
    this.load.image('logo', 'assets/logo.png');
}
 
function create ()
{
    this.add.image(400, 300, 'logo');
}
 
function update ()
{
}- CreateJS: - CreateJS是一个用于创建富互联网应用程序的开源工具包,主要用于构建基于Web的图形用户界面,它可以用来制作动画、游戏、交互体验等。 
var stage = new createjs.Stage("game");
var image = new createjs.Bitmap("path/to/image.png");
stage.addChild(image);
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", function() {
    stage.update();
});- Egret: - Egret是一个开源的游戏框架,用于移动和桌面网络游戏的开发。 
class Main extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
 
    private onAddToStage(event: egret.Event) {
        // Setup the root display object
        var bitmap: egret.Bitmap = new egret.Bitmap();
        bitmap.x = 50;
        bitmap.y = 50;
        this.addChild(bitmap);
        bitmap.bitmapData = egret.BitmapData.create(400, 300);
    }
}
 
egret.runEgret({
    // Allows for loading of external resources, such as images
    resources: ["resource/default.res.json"],
    // Entry point for the application
    entryClassName: "Main",
    // Frame rate for the game, i.e., 60fps
    frameRate: 60,
    // Touch screen input model, "mouse" for desktop and "touch" for mobile devices
    input: egret.InputMode.TOUCH,
    // The width and height of the game's screen canvas
    stageWidth: 640,
    stageHeight: 480,
    // WebGL mode or Canvas mode
    renderMode: "webgl",
});- Cocos Creator: - Cocos Creator是一个全面的游戏制作工具,它可以用来制作2D和3D游戏。 
cc.Class({
    extends: cc.Component,
 
    properties: {
        // foo: cc.Texture2D, // 所有Cocos Creator资源都可以作为属性
    },
 
    // LIFE-CYCLE CALLBACKS:
 
    // onLoad () {},
 
    start () {
        // 实例代码通常在这里开始
        var sprite = new cc.Sprite("path/to/image.png
评论已关闭