Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 回合制游戏,你如何决定玩家和电脑之间的回合_Javascript - Fatal编程技术网

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

Javascript 回合制游戏,你如何决定玩家和电脑之间的回合,javascript,Javascript,尝试为游戏编写基于回合的战斗模式,但我无法让它在玩家和计算机之间更改回合 我用随机数来设定第一回合,但之后它不会改变角色 到目前为止,我尝试的是while循环和if语句 我使用的是enchant.js框架,但如果有人能提供帮助,纯java脚本也可以 这是我的代码: var Battle = Class.create(Scene,{ initialize: function(){ var battle = Game.instance; Scene.apply(this);

尝试为游戏编写基于回合的战斗模式,但我无法让它在玩家和计算机之间更改回合

我用随机数来设定第一回合,但之后它不会改变角色

到目前为止,我尝试的是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--;
        };
    }

}});

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


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

代码中有很多关于当。。。您是否尝试在每次转弯时创建新按钮?为了提高性能,您最好在开始时创建所有按钮,然后根据转弯的角度允许操作。。。