回合制游戏,你如何决定玩家和电脑之间的转折

时间:2013-11-16 19:02:57

标签: javascript

尝试为游戏编写基于回合制的战斗模式,我无法让它改变玩家和计算机之间的转弯。

我使用随机数设置第一个转弯,但之后它不会改变角色

我到目前为止所尝试的是while循环和if语句

我正在使用enchant.js框架但是普通的java脚本可以做任何人可以帮助

这是我的代码:

var Battle = Class.create(Scene,{
initialize: function(){
    var battle = Game.instance;
    Scene.apply(this);
    console.log('Battle Screne');
 /*
 *  Background
 */
    var bg = new Sprite(320, 320);
        bg.image = game.assets['./assets/BS.png'];
        this.addChild(bg);

 /*
 *  Player Battle Sprite
 */
    BattleHero = Class.create(Sprite,{
        initialize:function(x,y){
        Sprite.call(this, 32,32);
        this.image = game.assets['./assets/SpriteSheet.png'];
        this.x =x;
        this.y =y;
        this.frame = [6];
        this.scaleX += 2;
        this.scaleY += 2;
        //battleScene.addChild(this);
    }});
    hero = new BattleHero(32*1,32*5);
    this.addChild(hero);
/*
 *  Enemy Battle Class
 */
    BattleNpc = Class.create(Sprite,{
        initialize:function(x,y){
        Sprite.call(this, 32,32);
        this.image = game.assets['./assets/SpriteSheet.png'];
        this.x =x;
        this.y =y;
        this.frame = [16];
        this.scaleX += 2;
        this.scaleY += 2;

    }}); 
    npc = new BattleNpc(32*8,32*5); 
    this.addChild(npc);

/*
*   Attack
*/      
    var turn = Math.floor((Math.random()*2)+1);
    console.log('turn equals',turn);

    if(turn == 1){
        console.log('player Turn',turn);
        var attack = new Button("Attack");
            attack.x = 32;
            attack.y = 32*7.5;
            attack.addEventListener(Event.TOUCH_END, function(e) {
                console.log('attack pressed');
                hero.tl.moveTo(32*8, 32*5, 5);//.moveTo(32*1.5, 32*5, 5);
                    if(hero.intersect(npc)){
                        console.log('melee hit');
                        hero.tl.moveTo(32*1.5, 32*5, 5);
                        //game.popScene();
                        turn ++;
                        return move = false;
                        console.log('turn is now',turn);
                    }
            }); 
        this.addChild(attack);

//Item Button
    var item = new Button("Item");
        item.x = 32;
        item.y = 32*8.5;
        item.addEventListener(Event.TOUCH_START, function(e){   
            turn = turn++;
        });
            this.addChild(item);
//Run Button
    var run = new Button ("Run");
        run.x = 32*4;
        run.y = 32*8.5;
        run.addEventListener(Event.TOUCH_END, function(e) {
            game.popScene();
        });
        this.addChild(run);
    }

//Npc Attact
    else{   
    console.log('Enemy Turn',turn);
    npc.frame = [16];
    npc.tl.moveTo(32*1.5, 32*5, 5).moveTo(32*8, 32*5, 5);

        if(npc.within(hero,16)){
            console.log('melee hit');
            npc.x = 32*8;
            return move = true;
            turn= turn--;
        };
    }

}});

1 个答案:

答案 0 :(得分:0)

如果您将代码提取到单独的函数中,而不是在initialize()函数中执行所有操作,则可以随意更改转换。

老实说,你似乎不了解基础知识,所以这可能是一个漫长的项目!