【问题标题】:dynamically positioning and animating images in JavaScript在 JavaScript 中动态定位和动画图像
【发布时间】:2016-10-20 04:33:57
【问题描述】:

我正在尝试通过随机分配坡度和起始位置来动态地为图像设置动画。我不明白为什么我的图像没有出现,当我将 cmets 从动画功能中取出时,我的代码将无法运行。除了底部的动画功能外,一切正常。任何帮助都将被欣然接受!

//Generate the table for the game
function createTable(difficulty, mineArray)
{
document.getElementById("buttons").style.visibility="hidden";
var time = 0.00;
var row = 0;
var size = 0;
var Lives = 0;
var column = 0;
var input = "";
var completion = 0;
var minesLeft = 0;

if(difficulty == 0)
    {
        Lives = 5;
        size = 600;
        row = 30;
        column = 20;
    }
else if (difficulty == 1)
    {
        Lives = 3;
        size = 600;
        row = 30;
        column = 20;
    }
else if (difficulty == 2)
    {
        Lives = 5;
        size = 1000;
        row = 40;
        column = 25;
    }
else if (difficulty == 3)
    {
        Lives = 3
        size = 1000;
        row = 40;
        column = 25;
    }

    for (var i = 0; i < size; i++)
    {
        if (mineArray[i] == 9)
            {
                minesLeft = minesLeft + 1;
            }     
    }
//Header
var head = document.getElementById("header").style.width = "100%"
document.getElementById("lives").innerHTML = "Lives: " + Lives;
document.getElementById("title").innerHTML = "Minesweeper Bullet Hell";
document.getElementById("time").innerHTML = "Time: " + time;
document.getElementById("footer").innerHTML = "Mines Left: " + minesLeft;
var name = document.getElementById("Name");
document.getElementById("names").innerHTML = "Welcome " + name.value;
//Main div (where the game is played)
var main = document.getElementById("main");
main.style.width = "100%"
main.style.height = "100%"
//Table
var div = document.getElementById("Table");
div.style.position = "absolute";
div.style.left = "5%";
div.style.top = "5%";
div.style.right = "5%";
div.style.verticalAlign = "true";
if(difficulty == 1 || difficulty == 0)
    {
        div.style.height = "900";
        div.style.width = "600";
    }
if(difficulty == 1 || difficulty == 0)
    {
        div.style.height = "1000";
        div.style.width = "625";
    }
div.style.zIndex="1";
//Iterate through columns
while(completion < size)
    {
        for (var i = 0; i < column; i++)
            {
                var tr = document.createElement('tr');
                //Iterate through rows
                    for(var j = 0; j < row; j++)
                        {
                            var place = completion;
                            var td = document.createElement('td');
                            //For smaller minefield
                            if (size == 600)
                                {
                                    td.style.width = "30";
                                    td.style.height = "auto";
                                    td.style.color = "blue";
                                    //Add an image
                                    var img = document.createElement('img');
                                    img.src = "grey square.png";
                                    img.style.display = "block";
                                    img.style.height = "30";
                                    img.style.width = "30";
                                    td.appendChild(img);
                                }
                            //For larger minefield
                            else
                                {
                                    td.style.width = "25";
                                    td.style.height = "auto";
                                    td.style.color = "blue";
                                    //Add an image
                                    var img = document.createElement('img');
                                    img.src = "grey square.png";
                                    img.style.display = "block";
                                    img.style.height = "25";
                                    img.style.width = "25";
                                    td.appendChild(img);
                                }
                            //If it is a mine
                            if (mineArray[completion] == 9)
                                {
                                    td.style.backgroundColor = "grey";
                                    td.style.color = "red";
                                }
                            td.style.border = "1px solid #666666"
                            td.style.textAlign = "center"

                            tr.appendChild(td);

                            completion++;
                        }
                //Think about adding an event listener instead of overlaying buttons?
                main.appendChild(tr);
            }
            var cells = document.getElementsByTagName("td");
            for (var j = 0; i < row; j++)
                {
                    cells[j].addEventListener("click", function () {    
                    //show number
                    var thiscol = this.cellIndex;
                    var thisrow = this.parentNode.rowIndex;
                    console.log("Clicked at " + thisrow + thiscol);
                    var cell = main.rows[thisrow].cells[thiscol];
                    cell.innerHTML = mineArray[(thisrow * row) + thiscol];
                    })
                }
    }
setTimeout(function(){bullets()}, 100);
}
function bullets()
{
//randomly generate bullets including starting positions, direction, and trajectory
//Generate starting position
//Generate starting edge
var xpos;
var ypos;
var bullets;
var slopes
var edge = (Math.floor(Math.random() * 4) + 1)
var bullet = document.createElement('img')
var screen = docuemnt.getElementById("bullets");
bullet.src = "blank.png"
bullet.style.position = "relative"
switch (edge)
    {
    //left edge
        case 1:
            bullet.style.left = 20 + "px";
            ypos = (Math.floor(Math.random() * 100) + 1);
            bullet.style.top = ypos + "px";
            bullet.id = "left";
            break;
    //top edge
        case 2:
            bullet.style.top = 20 + "px";
            xpos = (Math.floor(Math.random() * 100) + 1);
            bullet.style.right = xpos + "px";
            bullet.id = "top"
            break;
    //right edge
        case 3:
            bullet.style.right = 20 + "px";
            ypos = (Math.floor(Math.random() * 100) + 1);
            bullet.style.top = ypos+ "px";
            bullet.id = "right";
            break;
    //bottom edge
        case 4:
            bullet.style.bottom = 20 + "px";
            xpos = (Math.floor(Math.random() * 100) + 1);
            bullet.style.right = xpos + "px";
            bullet.id = "bottom";
            break;
    }
//Get the slope
var xslope = (Math.floor(Math.random() * 20) + 5);
var yslope = (Math.floor(Math.random() * 20) + 5);
bullets.append(bullet);
slopes.append(xpos);
slopes.append(ypos);
screen.appendChild(bullet);
//startAnimation(slopes, bullets);
}
/*
function startAnimation(var slopes, var bullets)
{
var j = 0;
var posy;
var posx;
var id;
for(i = 0; i < bullets.size(); i++)
    {
        while(j < (j+2))
            {
                id = bullets(i).id;
                switch(id)
                    {
                        case "left":
                            posx = bullets(i).style.left;
                            posy = bullets(i).style.top;
                            bullets(i).style.left = posx + slopes(j);
                            bullets(i).style.top = posy + slopes(j+1);
                            break;
                        case "top":
                            posx = bullets(i).style.left;
                            posy = bullets(i).style.top;
                            bullets(i).style.left = posx + slopes(j);
                            bullets(i).style.top = posy + slopes(j+1);
                            break;
                        case "right":
                            posx = bullets(i).style.right;
                            posy = bullets(i).style.top;
                            bullets(i).style.right = posx + slopes(j);
                            bullets(i).style.top = posy + slopes(j+1);
                            break;
                        case "bottom":
                            posx = bullets(i).style.left;
                            posy = bullets(i).style.bottom;
                            bullets(i).style.left = posx + slopes(j);
                            bullets(i).style.bottom = posy + slopes(j+1);
                            break;
                    }
                j += 2;
            }
    }
}*/

【问题讨论】:

  • 记录了什么错误?行号和错误消息将很有价值。 minimal reproducible example 是必不可少的。
  • 我希望我知道,通常它给我的唯一错误是函数未定义,这几乎没有帮助。开发人员工具确实抱怨我的事件监听器,但在测试它们时它们似乎工作正常
  • 您有 2 个功能。所以你真的调用函数createTable(),因为我看到你调用了bullets()并且#buttons变得可见了吗?您的事件处理程序依赖于必须由createTable() 完成的表格的表格单元格,但它没有被调用?
  • 是的,这是我程序的视图部分,模型创建数组并调用创建表函数

标签: javascript animation


【解决方案1】:

看起来可能有几件事让startAnimation 停止了你的脚本。以下是一些可以尝试的更改:

  • 在您的startAnimation 函数声明中,尝试删除参数前的var 关键字。所以你有:

    function startAnimation(slopes, bullets) {

  • 在 for 循环中,使用 .length 属性而不是 .size() 来检查数组长度(请参阅:Array.size() vs Array.length

  • while 循环正在创建一个无限循环,因为条件(j &lt; (j+2)) 将始终为真,因为它正在检查j 的当前值。用 for 循环替换它可能更容易。

  • 1234563将这些变量称为函数。由于您已经有一个名为 bullets 的函数,我建议您重命名该函数或您的局部变量/参数,以避免处理变量名称冲突和范围的复杂性(更多信息:What is the scope of variables in JavaScript?

【讨论】:

  • 哇,这对我有很大帮助,代码现在仍然可以运行,我只是不再看到我的空白.png。我没有改变我的循环,因为在循环结束时我有 j+= 2,它应该把它踢出循环。所有其他更改都有帮助。我很难在 javascript 中出现语法错误,因为 ide 没有帮助,开发人员工具通常只会说“(函数名称)未定义,意思是,”某处存在语法错误,祝你好运!我得到的唯一错误是当我添加一个事件侦听器时,它说它无法读取未定义的属性添加事件侦听器。据我所知,它们有效。
  • 对于丢失的图像,相对路径可能很棘手(更多信息请参见:stackoverflow.com/questions/2188218/…)。尝试重命名您的图像文件,以便没有空格(并改用下划线),这样您就不必担心 URL 将空格字符编码为 %20。
  • 我昨天做了很多工作,我让图像出现在随机位置和其他东西上,结果主要问题是我在尝试创建元素时拼写了文档错误。但是动画不起作用,因为由于某种我不明白的原因,子弹数组(我将其名称更改为 bulletArray)被重置为仅包含每帧通过的一件事。我尝试将我的声明移出函数,但这似乎没有帮助。
  • 完成了!结果我不得不像我想的那样将数组初始化从函数中移到全局范围内,但还必须使用“this”来调用它们,非常感谢您的帮助先生!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 2019-07-15
相关资源
最近更新 更多