enchant(); // initialize

window.onload = function(){

var game = new Core(320, 320);
game.preload('chara7.png', 'chara6.png', 'icon0.png'); // preload image
game.fps = 20;

game.onload = function(){

    // make new class for player
    var Player = enchant.Class.create(enchant.Sprite, {
        initialize: function(){
            enchant.Sprite.call(this, 32, 32);
            this.image = game.assets['chara7.png'];
            this.x = 288;// add:右側に表示。320-32の位置
            this.frame = 10;                   // set image data
            this.isattacking = -1; // 攻撃状態確認用。0未満がデフォル
            game.rootScene.addChild(this);     // add to canvas
        },
        attackstart: function(){
            this.frame = 16; // 斬っている瞬間の画像
            this.isattacking = 0; // 攻撃状態確認0回目
        },
        attackend: function(){
            if( this.isattacking < 0 ) return; // 攻撃状態では無い場合は後続処理をしない
            if( this.isattacking == 3 ) {
                this.frame = 10; // 普段の画像
                this.isattacking = -1;
            } else {
                this.isattacking ++;
            }
        }
    });

    // make new class for apple
    var Apple = enchant.Class.create(enchant.Sprite, {
        initialize: function(){
            enchant.Sprite.call(this, 16, 16);
            this.image = game.assets['icon0.png']; // set image
//            this.moveTo(16, player.y + 8);       // move to the position
            this.moveTo(300 , player.y + 8);       // 変更
//            this.tl.moveBy(320, 0, 30);        // set movement
            this.tl.moveBy(-320, 0, 30);        // 変更
            this.frame = 15;                   // set image data
            game.rootScene.addChild(this);     // add to canvas
        }
    });

    // make new class for enemy
    var Enemy = enchant.Class.create(enchant.Sprite, {
        initialize: function(){
            enchant.Sprite.call(this, 32, 32);
            this.image = game.assets['chara6.png']; // set image
//            this.moveTo(320, Math.floor(Math.random() * 320)); // set position
            this.moveTo(0, Math.floor(Math.random() * 320)); // 変更
            this.scaleX = -1;
//            this.tl.moveBy(-360, 0, 160);      // set movement
            this.tl.moveBy(360, 0, 160);      // 変更
            this.frame = 9;                   // set image data
            game.rootScene.addChild(this);     // canvas
        },
        onenterframe: function(){
            if(this.within(player) && player.isattacking >= 0){
                game.rootScene.removeChild(this);
                score++; //スコアを1足す
            }
        }
    });

    var player = new Player();

    // generate enemy every 60 frames
    game.rootScene.tl.then(function() {
        var enemy = new Enemy();
    }).delay(30).loop();                    // wait 60 frames and loop it!

    // add event listener (called when click/touch started)
    game.rootScene.on('touchstart', function(evt){
        player.y = evt.localY;    // set position to touch-y position
//        var apple = new Apple();
        player.attackstart();
    });
/*
    // add event listener (called when click/touch end)
    game.rootScene.on('touchend', function(evt){
        player.attackend();
    });
*/
    // add event listener (called when click/touch moved)
    game.rootScene.on('touchmove', function(evt){
        player.y = evt.localY;    // set position to touch-y position
    });

    var score = 0;

    game.rootScene.on('enterframe', function(){
        player.attackend();
/*
        var hits = Apple.intersect(Enemy);
        for(var i = 0, len = hits.length; i < len; i++){
            game.rootScene.removeChild(hits[i][0]);
            game.rootScene.removeChild(hits[i][1]);
            score ++;
        }
*/
    });

    player.tl.delay(game.fps * 10).then(function(){
        alert('game over! score: ' + score);
        game.stop();
    });
    
};

    game.start(); // start your game!
};