Javaskript:简单的减法返回NaN

时间:2017-04-02 09:52:11

标签: javascript nan

我有问题, 我的碰撞检测功能有时会将实体位置设置为NaN。 当我打开控制台(在chrome上)时,实体的位置和碰撞是有效的数字,但是相互扣除它们有时会返回NaN。

<?php

   define('DB_NAME', 'db1');
   define('DB_USER', 'root');
   define('DB_PASSWORD', '');
   define('DB_HOST', 'localhost');

     $link= mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
       if(!$link){
       die('Could not connect:'. mysql_error());
       }
     $db_selected=mysql_select_db(DB_NAME,$link);
       if(!$db_selected){
       die('Cant use' .DB_NAME. ';'. mysql_error());
       }

     $value1="";
     $value2="";
     $value3="";
     $value4="";
     $value5="";
     $value6="";
     $value7="";
     $value8="";
     $value9="";
     $value10="";
     $value11="";
     $value12="";
     $value13="";
     $value14="";
     $value15="";
     $value16="";
     $value17="";
     $value18="";
     $value19="";
     $value20="";
     $value21="";
     $value22="";

     if(isset($_POST['submit'])){
     $value1=$_POST['Full Name'];
     $value2=$_POST['Fathers Name'];
     $value3=$_POST['Mothers Name'];
     $value4=$_POST['DOB'];
     $value5=$_POST['Gender'];
     $value6=$_POST['Marital Status'];
     $value7=$_POST['Husbands Name'];
     $value8=$_POST['Category'];
     $value9=$_POST['Sub-Category'];
     $value10=$_POST['Nationality'];
     $value11=$_POST['Religion'];
     $value12=$_POST['Address'];
     $value13=$_POST['Mobile no'];
     $value14=$_POST['Email-ID'];
     $value15=$_POST['Exam passed'];
     $value16=$_POST['Board'];
     $value17=$_POST['Passing Year'];
     $value18=$_POST['Percentage12'];
     $value19=$_POST['Migcir'];
     $value20=$_POST['ProofDOB'];
     $value21=$_POST['Marksheet'];
     $value22=$_POST['GapCertificate'];
      }
    $sql= mysql_query ("INSERT INTO cslist(Full Name, Fathers Name, Mothers          Name,DOB,Gender,Marital Status,Husbands Name,Category,Sub-Category,Nationality,Religion,Address,Mobile no,Email-ID,Exam passed, Board,Passing Year,Percentage12,Migcir,ProofDOB,Marksheet,GapCertificate) VALUES (
    '$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13','$value14','$value15','$value16','$value17','$value18','$value19','$value20','$value21','$value22')");

    $res=mysql_query($sql) or die ("Error: ". mysql_error(). " with query ".     $sql);
    mysql_close();

    ?>

控制台截图:

(screenshot of console)

所以我真的很困惑,并且不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

我再次重写了代码,我再也没有得到NaN值了

固定功能:

updateCollision = function(entity,rect) {
var a = entity.x - rect.x;
var b = entity.y - rect.y;

var unrotatedCircleY = Math.sin((-rect.angle)/180*Math.PI)*a + Math.cos((-rect.angle)/180*Math.PI)*b +rect.y;
var unrotatedCircleX = Math.cos((-rect.angle)/180*Math.PI)*a - Math.sin((-rect.angle)/180*Math.PI)*b +rect.x;
var r = entity.collRad/2;

var closestX, closestY, aX, aY;


if (unrotatedCircleX < rect.x - rect.width/2 )
{
    closestX = rect.x - rect.width/2;
    aX = closestX;
}
else if (unrotatedCircleX  > rect.x + rect.width/2 )
{
    closestX = rect.x + rect.width/2;
    aX = closestX;
}
else
{
    closestX = unrotatedCircleX;
    aX=rect.x;
}

if (unrotatedCircleY < rect.y - rect.height/2 )
{
    closestY = rect.y - rect.height/2;
    aY = closestY;
}
else if (unrotatedCircleY > rect.y + rect.height/2 )
{
    closestY = rect.y + rect.height/2;
    aY = closestY;
}
else
{
    closestY = unrotatedCircleY;
    aY = rect.y;
}


var collision = false;

var distance = getDistance(unrotatedCircleX , unrotatedCircleY, closestX, closestY);
if (distance < r)
collision = true;
else
collision = false;

if( collision && entity.type == "bullet")
{
    entity.hp = 0;
}
else if(collision)
{
    if( entity.type == "bullet")
    {
        DeleteEntity(entity);
        return;
    }
    if( rect.collType == "solid" )
    {

        var positionAngle = Math.atan2( -closestY + unrotatedCircleY , -closestX + unrotatedCircleX );

        var y_vel = Math.sin((-rect.angle)/180*Math.PI)*entity.x_vel + Math.cos((-rect.angle)/180*Math.PI)*entity.y_vel;
        var x_vel = Math.cos((-rect.angle)/180*Math.PI)*entity.x_vel - Math.sin((-rect.angle)/180*Math.PI)*entity.y_vel;

        y_vel *= 0.9;
        x_vel *= 0.9;



        unrotatedCircleX = closestX + Math.cos(positionAngle)*(r);
        unrotatedCircleY = closestY + Math.sin(positionAngle)*(r);

        a = unrotatedCircleX - rect.x;
        b = unrotatedCircleY - rect.y;

        entity.y = Math.sin((rect.angle)/180*Math.PI)*a + Math.cos((rect.angle)/180*Math.PI)*b + rect.y;
        entity.x = Math.cos((rect.angle)/180*Math.PI)*a - Math.sin((rect.angle)/180*Math.PI)*b + rect.x;

        entity.y_vel = Math.sin((rect.angle)/180*Math.PI)*x_vel + Math.cos((rect.angle)/180*Math.PI)*y_vel;
        entity.x_vel = Math.cos((rect.angle)/180*Math.PI)*x_vel - Math.sin((rect.angle)/180*Math.PI)*y_vel;
    }
    if( rect.collType == "trigger" )
    {
        if( level["commandList"][rect.data].command == "tp" )
        {
            entity.x = level["commandList"][rect.data].x;
            entity.y = level["commandList"][rect.data].y;
        }
        if( level["commandList"][rect.data].command == "loadLevel" )
        {
            levelToLoad = level["commandList"][rect.data].x;
        }
    }
}

但感谢您的帮助:)

答案 1 :(得分:-2)

我需要查看更多代码,看看实际上是什么问题,但NaN肯定是undefined类型的数学运算的结果,所以如果有undefined的话,请仔细看看