enchant.js 熊を探せ

x軸y軸をランダムに表示させて、touchstartで、位置とスコアを変えていくゲームスクリプトです。

enchant();

window.onload = function(){
    var core = new Core(320, 320);
    core.preload('chara1.png');
    core.fps = 15;

    var score = 0;
    var timeLeft = 5 * core.fps;

    core.onload = function(){
        var bear = new Sprite(32, 32);
        bear.x = rand(320);
        bear.y = rand(320);
        bear.frame = 0;
        bear.image = core.assets['chara1.png'];

        bear.on('touchstart', function(){
            score++;
            scoreLabel.text = 'Score: ' + score;
            this.x = rand(320);
            this.y = rand(320);
        });

        var scoreLabel = new Label('Score: 0');
        scoreLabel.x = 200;
        scoreLabel.y = 5;

        var timeLabel = new Label('Time: 0');
        timeLabel.x = 5;
        timeLabel.y = 5;

        core.on('enterframe', function(){
            timeLeft--;
            timeLabel.text = 'Time: ' + timeLeft;
            if (timeLeft <= 0){
                alert('Your score: ' + score);
                this.stop();
            }
        });

        core.rootScene.addChild(scoreLabel);
        core.rootScene.addChild(timeLabel);
        core.rootScene.addChild(bear);


        core.rootScene.addChild(bear);
    }
    core.start();
}

function rand(n){
    return Math.floor(Math.random() * (n+1));
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <script type="text/javascript" src="enchant.js"></script>
    <script type="text/javascript" src="main.js"></script>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
</body>
</html>

%e7%84%a1%e9%a1%8c

enchant.js

JSのゲーム開発用フレームワーク、enchant.js

enchant();
/*

Core
- rootScene
-- Sprite (bear)

*/

window.onload = function(){
  var core = new Core(320, 320);
  core.preload('chara1.png');
  core.fps = 15;
  core.onload = function(){
/*
    var bear = new Sprite(32, 32);
    bear.image = core.assets['chara1.png'];
    bear.x = 0;
    bear.y = 0;

    bear.addEventListener('enterframe', function(){
      if (core.input.right) this.x += 5;
      // intersect
      if (this.intersect(enemy)){
      //  label.text = 'hit!';
      }
      // within
      if (this.within(enemy, 10)){
        // label.text = 'HIT!';
        core.pushScene(gameOverScene);
        core.stop();
      }
    });

    var enemy = new Sprite(32, 32);
    enemy.image = core.assets['chara1.png'];
    enemy.x = 80;
    enemy.y = 0;
    enemy.frame = 5;

    var gameOverScene = new Scene();
    gameOverScene.backgroundColor = 'black';


    var label = new Label();
    label.x = 280;
    label.y = 5;
    label.color = 'red';
    label.font = '14px "Arial"';


  core.rootScene.addChild(bear);
   core.rootScene.addChild(label);
   core.rootScene.addChild(enemy);
*/
  var Bear = Class.create(Sprite, {
    initialize: function(x, y){
      Sprite.call(this, 32, 32);
      this.x = x;
      this.y = y;
      this.frame = rand(5);
      this.opacity = rand(100) /100;
      this.image = core.assets['chara1.png'];

      this.tl.moveBy(rand(100), 0, 40, enchant.Easing.BOUNCE_EASEOUT)
        .moveBy(-rand(100), -rand(20), rand(20))
        .fadeOut(20)
        .fadeIn(10)
        .loop();

      core.rootScene.addChild(this);
    }
  });
  var bears = [];
  for (var i=0; i < 100; i++){
    bears[i] = new Bear(rand(320), rand(320));
  }
}
  core.start();
};

function rand(n){
  return Math.floor(Math.random() * (n+1));
}

a