/* * constant */ var SCREEN_WIDTH = 640; var SCREEN_HEIGHT = 480; var SCREEN_CENTER_X = SCREEN_WIDTH/2; var SCREEN_CENTER_Y = SCREEN_HEIGHT/2; // アセット var ASSETS = { "CAT": "neko.png" }; /** * リソースの読み込み */ tm.preload(function() { // 音 tm.sound.SoundManager.add("hit_se", "hit_se.wav"); }); //画像読み込み tm.asset.AssetManager.load("TitleImage" , "title.png"); // ユーザーのデータの定義 tm.util.DataManager.set("userData", { score: 10, time: 100 * 60, speed: 1 }); /* * メイン処理(ページ読み込み後に実行される) */ tm.main(function() { // ユーザーデータの生成 userData = tm.util.DataManager.get("userData"); // アプリケーション作成 var app = tm.app.CanvasApp("#world"); app.resize(SCREEN_WIDTH, SCREEN_HEIGHT); // リサイズ app.fitWindow(); // 自動フィット app.replaceScene(TitleScene()); // 実行 app.run(); }); /** * TitleScene */ tm.define("TitleScene", { superClass : "tm.app.TitleScene", init : function() { this.superInit({ title : "タイトル画面", width : SCREEN_WIDTH, height : SCREEN_HEIGHT, backgroundImage : "TitleImage" }); }, update: function(app) { var key = app.keyboard; if (key.getKey("z")) { // シーンの遷移 var loadingScene = tm.app.LoadingScene({ assets: ASSETS, nextScene: MainScene, width: SCREEN_WIDTH, height: SCREEN_HEIGHT }); this.app.replaceScene(loadingScene); } } }); tm.define("MainScene", { superClass: "tm.app.Scene", init: function() { // 親の初期化 this.superInit(); this.rect = tm.app.RectangleShape(64,64); this.addChild(this.rect); // シーンに追加 this.star = tm.app.Sprite("CAT"); this.star.scaleSetSize = function() {this.setSize(this.width * this.scaleX , this.height * this.scaleY);} this.addChild(this.star); // シーンに追加 // ユーザーデータの生成 userData = tm.util.DataManager.get("userData"); // スコアのラベル l_obj = { x : SCREEN_WIDTH, y : 40, width : tm.app.width, align : "end", fontSize : 32 , textReDraw : function(){ this.text = "score : " + userData.score; } } this.scoreLabel = this.makeLabel(l_obj); // タイムのラベル l_obj.x = 240; l_obj.textReDraw = function(){ this.text = "time : " + userData.time / 60; } this.timeLabel = this.makeLabel(l_obj); // スピードのラベル l_obj.y = 80; l_obj.textReDraw = function(){ this.text = "speed : " + userData.speed; } this.speedLabel = this.makeLabel(l_obj); }, update: function(app) { var p = app.pointing; // this.rect.x = 300; this.rect.y = 300; // マウス位置 or タッチ位置に移動 this.star.x = p.x; this.star.y = p.y; // if(this.star.isHitElement(this.rect) && this.rect.visible == true){ tm.sound.SoundManager.get("hit_se").play(); this.rect.visible = false; // スコアを更新 userData.score += 100; this.scoreLabel.textReDraw(); } // クリック or タッチ中は回転させる if (app.pointing.getPointing() == true) { this.star.rotation += 15; } //キーボタン受付 var key = app.keyboard; if (key.getKey("z")) { this.star.scaleX += 0.1; this.star.scaleY += 0.1; this.star.scaleSetSize(); } if (key.getKey("c")) { userData.speed += 1; this.speedLabel.textReDraw(); } //time,60フレーム(1秒)に1回減って表示。 userData.time -= 1; if(userData.time % 60 == 0){ this.timeLabel.textReDraw(); } }, makeLabel : function(l_obj){ var label = tm.app.Label(); label.position.set(l_obj.x , l_obj.y); label.textReDraw = l_obj.textReDraw; label.textReDraw(); label.align = l_obj.align; label.width = l_obj.width; label.fontSize = l_obj.fontSize; this.addChild(label); return label; }, });