简体   繁体   English

Javascript-重复的if语句不起作用

[英]Javascript - Repeated if-statements don't work

My code works (doesn't fail), but it doesn't do the right thing. 我的代码有效(不会失败),但它做的事情不正确。

I call the "generate" function every time i want to generate a new "chunk", passing a new number into the function each time its called. 每当我想生成一个新的“块”时,我都会调用“生成”函数,每次调用该函数时都会将一个新的数字传递给该函数。 It generates the chunk fine, but it doesn't generate what I want it to generate. 它可以生成块,但不会生成我想要的块。

I want it to generate either a space, a jump, a slide, or a gap, if the previous generation was a space. 如果上一代是空间,我希望它生成一个空间,一个跳跃,一个滑动或一个间隙。 But if the previous generation wasn't a space, generate a space. 但是,如果上一代不是一个空间,请生成一个空间。 It doesn't do that. 它不会那样做。 It sometimes generates 2 gaps, 2 jumps, or 2 slides after each other and i have no idea why... ??? 有时彼此之间会产生2个间隙,2个跳跃或2个滑行,我不知道为什么... ???

Here is my code: 这是我的代码:

var ptg = 'space'; // what was previously generated to top
var wtgt; // what is currently being generated to top
var chunktogenerateto = 0;

function generate(a){
    chunktogenerateto = a;

    var rand1 = Math.floor(Math.random()*100) + 1;

    if(ptg == 'space' && rand1 <= 25){
        wtgt = 'space';
    }
    else if(ptg == 'space' && rand1 <= 50 && rand1 > 25){
        wtgt = 'jump';
    }
    else if(ptg == 'space' && rand1 <= 75 && rand1 > 50){
        wtgt = 'slide';
    }
    else if(ptg == 'space' && rand1 > 75){
        wtgt = 'gap';
    }
    else{
        wtgt = 'space';
    }

    ptg = wtgt;

    topGen(wtgt);
}

function topGen(g){
    document.getElementById('t' + chunktogenerateto).setAttribute('src','images/terrain/t' + g + '.png');
}

I hope it's not a typo... HELP! 我希望这不是错字...帮助!

Where the calls to "generate" are coming from: “生成”的调用来自哪里:

var chunkpos = new Array();
chunkpos[0] = -100;
chunkpos[1] = 0;
chunkpos[2] = 100;
chunkpos[3] = 200;
chunkpos[4] = 300;
chunkpos[5] = 400;
chunkpos[6] = 500;
chunkpos[7] = 600;
chunkpos[8] = 700;
chunkpos[9] = 800;
chunkpos[10] = 900;
chunkpos[11] = 1000;
var temppos = new Array();
var time1;
var millis1;
var time2;
var millis2;
var millis3;
var firstreset = true;
var pos;
var poschange;

function moveLevel(){
    if(firstreset == true){
        resetTime();
    }
    var time2 = new Date();
    var millis2 = time2.getTime();
    var millis3 = millis2 - millis1;
    poschange = Math.floor(millis3 / 5);
    for(i = 0; i < chunkpos.length; i++){
        temppos[i] = chunkpos[i] - poschange;
        if(temppos[i] <= -150){
            generate(i);
            temppos[i] += 1200;
        }
        pos = temppos[i];
        document.getElementById('chunk' + i).setAttribute('style','left: ' + pos + 'px;');
    }
}



function resetTime(){
    time1 = new Date();
    millis1 = time1.getTime();
    if(firstreset != true){
        for(i = 0; i < chunkpos.length; i++){
            chunkpos[i] = temppos[i];
        }
    }
    firstreset = false;
    setTimeout('resetTime()',1000);
}

Where the calls to "moveLevel" are coming from: 调用“ moveLevel”的位置来自:

window.onload = function(){
    if(test = 'runnable')
    {
        gameLoop();
    }
    else
    {
        document.getElementById('gm').innerHTML = (gm + 'Failed to run game.');
    }
}

function gameLoop(){
    if(currentscreen == 'playing'){
        moveLevel();
    }
    setTimeout('gameLoop()',0);
}

Here is a download link to a zip file containing all of the code: ParkourFreak.zip The code i'm having trouble with is under scripts/generation.js. 这是一个包含所有代码的zip文件的下载链接: ParkourFreak.zip我遇到麻烦的代码在scripts / generation.js下。 The main game page (where the generation is visible is index.html. 游戏主页面(可见生成的页面)是index.html。

Rather than each if statement starting with "if(ptg == 'space' ... etc" - do this first in one simple "if not space return space". 而不是每个以“ if(ptg =='space'... etc”开头的if语句-在一个简单的“ if not space return space”中首先执行此操作。

After that you can start on your random - that should work for you. 之后,您可以随机开始-应该适合您。

Roughly a quarter of the time you previously had a space, it will create another, because of this code: 由于以下代码,大约有四分之一的时间您以前有一个空间,它将创建另一个空间:

if(ptg == 'space' && rand1 <= 25){
    wtgt = 'space';
}

Note that it's creating a space when a space was previously created. 请注意,它是在先前创建空间时创建空间的。


Side note: 边注:

There's no need for the && rand1 > 25 on this line: 此行无需&& rand1 > 25

else if(ptg == 'space' && rand1 <= 50 && rand1 > 25){

Since you previously had if(ptg == 'space' && rand1 <= 25){ , it's impossible as of that line for rand1 to be <= 25 when ptg == 'space' . 由于您以前使用过if(ptg == 'space' && rand1 <= 25){ ,所以从ptg == 'space'rand1不能<= 25

(And similarly the && rand2 > 50 on the following condition.) (在以下情况下, && rand2 > 50同样。)

Are you sure you're incrementing chunktogenerateto ? 您确定要增加chunktogenerateto吗?

It appears to stay at 0 with the current code, is that in a segment you haven't shown? 当前代码似乎保持为0,是在您未显示的细分中吗?

Edit: oh, it's the argument of the function. 编辑:哦,这是函数的参数。 Try logging that variable, maybe that's the problem? 尝试记录该变量,也许是问题所在?

// elsewhere
var wtgts = ['space','jump','slide','gap'];

// in the function
if(ptg !== 'space') {
    wtgt = 'space';
} else {
    var randidx = Math.floor(Math.random()*4);
    wtgt = wtgts[randidx];
}

Or even in the function: 甚至在函数中:

var newidx = (ptg == 'space') ? Math.floor(Math.random()*4) : 0;
wtgt = wtgts[newidx];
  1. The weights were equal, so just pick a random index from 0 to 3 and use that to pick an action from an array. 权重相等,因此只需从0到3中选择一个随机索引,然后使用该索引从数组中选择一个动作即可。
  2. Separate the logic for ptg being space so that you only test for it once 分开将ptg作为空间的逻辑,以便只测试一次
  3. Consider denoting the actions as numbers instead of strings, as the assignment, comparison etc will be faster and more robust. 考虑将动作表示为数字而不是字符串,因为赋值,比较等将更快,更可靠。

I thought the problem lied with the generation, when in fact all I had to do to solve it was to change this: 我认为问题在于一代人,而实际上我要做的就是改变这一点:

if(temppos[i] <= -100){
    generate(i);
    temppos[i] += 1200;
}

to this: 对此:

if(temppos[i] <= -100){
    generate(i);
    chunkpos[i] += 1200;
    temppos[i] += 1200;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM