7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[tmlib.js] Keyboard クラスでサクッとプレイヤーを移動させてみよう

Last updated at Posted at 2013-10-22

tmlib.js 2.0 で tm.input.Keyboard クラスに getKeyDirection() method を
追加しました.

これを使えばサクッとキーボードでプレイヤーを動かすような
ゲームを作ることができます.

Example

Demo

/*
 * tmlib.js 0.2.0
 */

/*
 * contant
 */
var SCREEN_WIDTH    = 465;              // スクリーン幅
var SCREEN_HEIGHT   = 465;              // スクリーン高さ
var SCREEN_CENTER_X = SCREEN_WIDTH/2;   // スクリーン幅の半分
var SCREEN_CENTER_Y = SCREEN_HEIGHT/2;  // スクリーン高さの半分

/*
 * main
 */
tm.main(function() {
    var app = tm.display.CanvasApp("#world");
    app.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
    app.fitWindow();
    
    app.replaceScene(MainScene());
    
    app.run();
});

/*
 * main scene
 */
tm.define("MainScene", {
    superClass: "tm.app.Scene",
    
    init: function() {
        this.superInit();
        
        // プレイヤー生成
        this.player = tm.display.TriangleShape().addChildTo(this);
        this.player.setPosition(SCREEN_CENTER_X, SCREEN_CENTER_Y);
    },

    update: function(app) {
        var direction = app.keyboard.getKeyDirection(); // 向きを取得
        direction.mul(8);
        this.player.position.add(direction); // 移動
    }
});
7
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?