简体   繁体   English

使用一个动画循环(requestanimationframe)对矩形进行独立动画处理

[英]Animate independently rectangles with one animation loop (requestanimationframe)

I'm trying to represent rows of moving rectangles horizontally. 我试图表示水平移动矩形的行。 For this matter I created instances of the rectangle "class" and move them around incrementing their X property. 为此,我创建了矩形“类”的实例,并在增加其X属性的范围内移动它们。 Following the advise on this forum I used requestAnimationFrame for this task. 遵循此论坛上的建议,我将requestAnimationFrame用于此任务。

Logic problem 逻辑问题

I'm trying to represent packages sent from clients to a server. 我试图代表从客户端发送到服务器的程序包。 (From the left to the right). (从左到右)。

Here's a print screen: 这是一个打印屏幕:

https://s27.postimg.org/fw20wxyz7/Captura_de_pantalla_2014_05_30_a_la_s_17_00_16.png

The problem I face is that when a rectangle "intersects" with any other rectangle (meaning intersecting that one or more rectangles have the same x value at the same time ) I need to wait a random time before I draw a new rectangle in the same row. 我面临的问题是,当一个矩形与任何其他矩形“相交”时(意味着相交一个或多个矩形同时具有相同的x值),我需要等待随机的时间,然后才能在同一矩形中绘制一个新矩形行。

Right now I'm doing all the animation inside a single requestAnimationFrame loop. 现在,我正在单个requestAnimationFrame循环中进行所有动画处理。 How can I wait and draw separately each rectangle. 如何等待并分别绘制每个矩形。

I googled a lot over the topic and it seems like you're supposed to use just one loop, but in this case, how can I do this without using threads like in Java? 我在这个主题上搜索了很多,似乎应该只使用一个循环,但是在这种情况下,如何在不使用Java线程的情况下做到这一点呢?

How can I wait a random time without stopping the whole animation loop? 如何在不停止整个动画循环的情况下等待随机时间?
Should I use independent requestAnimationFrame loops for each rectangle? 我应该为每个矩形使用独立的requestAnimationFrame循环吗?

Rectangle.prototype.move=function(){

    var maxRight=canvas.width-this.width;

    for(var i=0; i< rectangles.length; i++){
        if(rectangles[i].y!=this.y){

            if(intersects(this.x,this.width,rectangles[i].x,this.width) && this.colided==false){
                console.log("choque");
                this.colided=true;
                this.color="#DC143C";
                rectangles[i].color="#DC143C";
                rectangles[i].colided=true;
                numColisions+=2;
            }
        }

    }

    this.x+=this.velocityX;


    if(this.x>maxRight){
        this.end=true;

    }
    return(this);
};

// draw this rect on the canvas
Rectangle.prototype.render=function(){
    ctx.fillStyle=this.color;
    ctx.fillRect(this.x,this.y,this.width,this.height);
    return(this);
}

You can suspend a rect's movement for a random # of animation loops by: 您可以通过以下方式暂停rect的移动以随机执行#个动画循​​环:

  • adding a this.suspend property to the Rectangle class 在Rectangle类中添加this.suspend属性

  • when a rect collides then set its suspend countdown to a random delay 当rect发生碰撞时,将其暂停倒计时设置为随机延迟

  • if a rect is "suspended", instead of moving it just reduce its .suspend countdown 如果rect是“悬浮”的,而不是移动它,只需减少其.suspend倒计时

  • if a rect is active, just move it 如果矩形是活动的,只需移动它

Example code (untested): 示例代码(未经测试):

// add a this.suspend property to the Rectangle class

this.suspend=0;
...

// only move the rect if the rect is not suspended

Rectangle.prototype.move=function(){

    var maxRight=canvas.width-this.width;

    for(var i=0; i< rectangles.length; i++){
        if(rectangles[i].y!=this.y){

            if(intersects(this.x,this.width,rectangles[i].x,this.width) && this.colided==false){
                console.log("choque");
                this.colided=true;
                this.color="#DC143C";
                rectangles[i].color="#DC143C";
                rectangles[i].colided=true;
                numColisions+=2;

                // suspend moving this rect for random time (0-5 loops)
                this.suspend=parseInt(Math.random()*5);
            }
        }

    }

    // decide whether or not to move this rectangle
    if(this.suspend>0){
        // this rect is suspended, reduce the suspension countdown
        this.suspend--;
    }else{
        // this rect is active, move it
        this.x+=this.velocityX;
    }

    if(this.x>maxRight){
        this.end=true;

    }
    return(this);
};

I think you didn't understand properly what I meant by waiting a random time. 我认为您不正确地理解随机等待的含义。 It's not that I want to delay the rectangle that colided, but create a new one from the left margin (like it was sent again from the same client). 不是我要延迟矩形,而是从左边距创建一个新矩形(就像它是从同一客户端再次发送的一样)。 But both of your approaches were really useful, just that I needed to add the suspend property to a new instance of rectangle (or add a negative x to the new rectangle). 但是您的两种方法都非常有用,只是我需要将suspend属性添加到矩形的新实例中(或在新矩形中添加负数x)。

Anyway, my new function looks like that, but I don't know for what reason the whole code is not running when I create the new instances inside the if sentence. 无论如何,我的新函数看起来像那样,但是当我在if语句中创建新实例时,我不知道整个代码没有运行的原因是什么?

Rectangle.prototype.move=function(){ Rectangle.prototype.move = function(){

var maxRight=canvas.width-this.width;

for(var i=0; i<rectangles.length; i++){
    if(rectangles[i].y!=this.y){

        if(intersects(this.x,this.width,rectangles[i].x,this.width) && this.colided==false){
            console.log("choque");
            this.colided=true;
            this.color="#DC143C";
            rectangles[i].color="#DC143C";
            rectangles[i].colided=true;
            numColisions+=2;

            //Create 2 new rectangles 

            var rect1=newRect(0,this.y,40,30);
            rect1.velocityX=speed;
            rectangles.push(rect1);
            rect1.suspend=parseInt(Math.random()*5);


            var rect2=newRect(0,rectangles[i].y,40,30);
            rect2.velocityX=speed;
            rect2.suspend=parseInt(Math.random()*5);
            rectangles.push(rect2); 

        }


    }
}


// decide whether or not to move this rectangle
if(this.suspend>=0){
    // this rect is suspended, reduce the suspension countdown
    this.suspend--;
}else{
    // this rect is active, move it
    this.x+=this.velocityX;
} 

this.x+=this.velocityX;
if(this.x>maxRight){
    this.end=true;

}
return(this);

}; };

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

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