0.13.5 • Published 4 years ago

zimper v0.13.5

Weekly downloads
81
License
Apache-2.0
Repository
-
Last release
4 years ago

zimper

A small module to structure && organize zimjs based HTML5 games.

Get Started

    const zimper = require('zimper');

    ///  Support loading by segments
    const assets = {
        segment_1: {
            fonts: {
                MSB: 'xxx/MSB.otf',
            },
            sounds: {
                audio_segment_11: 'xxx',
                audio_segment_12: 'xxx',
                audio_segment_13: 'xxx',
            },
            spritesheet: {
                json: null,
                imgurls: [],
            },
        },
        segment_2: {
            fonts: null,
            sounds: {
                audio_segment_21: 'xxx',
            },
            spritesheet: {
                json: 'xxx',
                imgurls: 'xxx',
            },
        },
        segment_3: {
            fonts: null,
            sounds: {
                audio_segment_31: 'xxx',
                audio_segment_32: 'xxx',
            },
            spritesheet: {
                json: 'xxx',
                imgurls: 'xxx',
            },
        },
    };

    zimper.init({
        width: 750,
        height: 1334,
        spritesheet: assets.segment_1.spritesheet,
        sounds: assets.segment_1.sounds,
        fonts: assets.segment_1.fonts,
        gpu: false,
        outerColor: '#000',
        color: '#000', ///window.__app.themeColor, /// '#e86136',
        bitmaps: [
            {
                key: 'player',
                url: 'https://i.imgur.com/w9IgyPV.png',
            }
        ],
        onProgress(progress) {},
    })
    .then(game => {
        /// Now a player bitmap can be accessed by game.frame.bitmap.loaded.player

        game.addScene('start', require('xxx/start'));
        game.addScene('intro', require('xxx/intro'));
        game.goto('start');
    })
    .catch(console.error);

Scenes

Scenes are nothing but pure Javascript objects with properties and methods which serve as the building blocks of zimpler powered HTML5 games.

Scene Properties:

frame (object)

/-------------------------------------------------------------------- frame augments zimjs frame, check zimjs docs for more details. --------------------------------------------------------------------/

frame.stage                    **(object)**

frame.sprites                  **(object)**

frame.sprites.new(id)          **(function)**    Returns a sprite whuch extends zim.Sprite with a **go()** method

frame.bitmap                   **(object)**

frame.bitmap.load(targets)     **(function)** Returns a Promise and adds the loaded zim.Bitmaps to frame.bitmap.loaded for later access

frame.bitmap.loaded(key)       **(function)**

frame.bitmap.fromUrl(url)      **(function)**

frame.sound                    **(object)**

frame.sound.play(id, conf)     **(function)**

frame.sound.stop(id)           **(function)**

frame.sound.pause(id)          **(function)**

frame.sound.resume(id)         **(function)**

Scene Methods:

width()

height()

left()

top()

goto(key) Switches to another scene

preload() 設定 scene assets 啟動 onProgress() 並且在 assets 載入完成後才呼叫 onCreate()

update() important If onUpdate() is not implemented, you should call update() manually to rerender the canvas.

Scene Event Methods:

onPreload()

onProgress(progress)

onCreate()

onDestory()

onFocus()

onBlur()

onUpdate() important If onUpdate() is set to null, you should call update() manually.

Example

Run-time Loading

    {
        onPreload() {
            /// 設定 preload 資源
            this.preload({
                fonts: '...',
                sounds: {},
                spritesheet: {},
            });
        },
        onProgress(progress) {
        },
        onCreate() {
            /// 動態載入資源
            this.frame.loadGameAssets({
                fonts: '...',
                sounds: {},
                spritesheet: {},
            });
        },
        onUpdate() {
        },
        onFocus() {
        },
        onBlur() {
        },
        onDestory() {
        },
    }

Positioning

    /// 844/1627
    console.log(this.width, '/', this.height)

    /// iPhone 6/7/8: 344/667
    /// iPhone X: 375/812
    console.log(window.innerWidth, '/', window.innerHeight)

    /// iPhone 6/7/8: 46/146
    /// iPhone X: 46/0
    console.log(this.left, '/', this.top)

    /// iPhone 6/7/8: 把物件放在螢幕最左上角
    this.frame.sprites.new('xxx').addTo().pos(46, 146);

    /// iPhone X: 把物件放在螢幕最左上角
    this.frame.sprites.new('xxx').addTo().pos(46, 0);

    /// In general: 把物件放在螢幕最左上角
    this.frame.sprites.new('xxx').addTo().pos(0 + this.left, 0 + this.top);

Load zim.Bitmap from URL

    this.frame.bitmap.load({key: 'test', url: 'https://i.imgur.com/w07f1fk.png'}).then(bmp => {
        console.log(bmp, this.frame.bitmap.loaded)
    }).catch(console.log);

Load zim.Bitmap from Data URL

    const dataURL = 'xxxx';

    zim.Bitmap.fromData(dataURL, bitmap => {
        /// Use bitmap ...
    });

直式/橫式安全互換策略

  1. 中心點為參考原點來定位

  2. this.leftthis.top 相關的物件,需要在 resize 事件中重新定位

    this.frame.on('resize', () => {
        this._oleft = this.frame.offset.left;
        this._otop = this.frame.offset.top;
        some_object.pos(this._oleft, this._otop);
    });
0.13.5

4 years ago

0.13.4

4 years ago

0.13.3

4 years ago

0.13.2

4 years ago

0.12.10

5 years ago

0.12.11

5 years ago

0.12.12

5 years ago

0.12.9

5 years ago

0.12.8

5 years ago

0.12.6

5 years ago

0.12.5

5 years ago

0.12.4

5 years ago

0.12.3

5 years ago

0.12.2

5 years ago

0.12.1

5 years ago

0.11.10

5 years ago

0.11.9

5 years ago

0.11.8

5 years ago

0.11.7

5 years ago

0.11.6

5 years ago

0.11.4

5 years ago

0.11.3

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.9

5 years ago

0.10.8

5 years ago

0.10.7

5 years ago

0.10.6

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.9.5

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.2

5 years ago

0.8.0

5 years ago

0.7.8

6 years ago

0.7.7

6 years ago

0.7.6

6 years ago

0.7.4

6 years ago

0.7.3

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.7

6 years ago

0.6.6

6 years ago

0.6.5

6 years ago

0.6.4

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago