精灵图像的随机移动位置

B型

目前,当我的精灵在画布上移动时,只有在碰到画布的侧面后才会反弹。有没有办法让我的精灵在画布上的随机位置改变到另一个方向?

这是我更改方向及其移动方式的代码:

Fish.prototype.changeDirection = function () {
    speedXSign = this.speedX > 0 ? 1 : -1;
    speedYSign = this.speedY > 0 ? 1 : -1;
    this.speedX = speedXSign * (1 + Math.random() * 2);
    this.speedY = speedYSign * (1 + Math.random() * 2);
};

Fish.prototype.move = function () {
    this.animIndex++;
    if ( this.animIndex == animFrames.length) this.animIndex = 0;

    this.xPos += this.speedX;
    if ((this.xPos + this.frameWidth * this.frameScale / 2) >= canvas.width && this.speedX > 0 || 
        (this.xPos - this.frameWidth * this.frameScale / 2) <= 0 && this.speedX <= 0) {
        this.speedX = -this.speedX;
    }

    this.yPos += this.speedY;
    if ((this.yPos + this.frameHeight * this.frameScale / 2) >= canvas.height && this.speedY > 0 || 
        (this.yPos - this.frameHeight * this.frameScale / 2) <= 0 && this.speedY <= 0) {
        this.speedY = -this.speedY;
    }
};
贾里德·尼尔(Jared Neil)

一个相当简单的选择是随机选择时间,然后在该时间之后改变鱼的方向。我首先想到的是使用setTimeout我注意到您的changeDirection函数中的比较是向后的,因此我将其修复,并将其设置为在一段时间后随机调用。

Fish.prototype.changeDirection = function () {
    var me = this;
    var speedXSign = this.speedX < 0 ? 1 : -1;
    var speedYSign = this.speedY < 0 ? 1 : -1;
    this.speedX = speedXSign * (1 + Math.random() * 2);
    this.speedY = speedYSign * (1 + Math.random() * 2);
    var time = 1000 + 2000*Math.random();
    setTimeout(function() {me.changeDirection()}, time);
};

您可以通过调整时间变量来更改周转频率。然后,当您添加一条新鱼时,您需要初始化changeDirection循环,因此init可能如下所示:

function init() {
    frameWidth = imgFish.width / frameCount ; 
    frameHeight = imgFish.height ; 

    document.getElementById("button").onclick = function() {
        // create another fish using the Fish class
        var anotherFish = new Fish(xPos, yPos, speedX, speedY, imgFish, frameWidth, frameHeight);
        // put this new fish into the fishes[] array
        fishes.push(anotherFish) ;
        // make it start changing directions
        anotherFish.changeDirection();
        // draw this new fish
        anotherFish.drawFish();
    }
    animate();
}

另外,您也不想改变每一帧的方向,因此请将fish.changeDirection();线条从animate功能中删除

作为附带说明,您可以考虑使它们独立或随机而不是每次都改变x和y方向。这使它看起来更加自然。

    var speedXSign = Math.random() < 0.5 ? 1 : -1;
    var speedYSign = Math.random() < 0.5 ? 1 : -1;

编辑:JSFiddle

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用屏幕上的随机位置永远移动精灵

来自分类Dev

精灵的随机移动

来自分类Dev

精灵的随机移动

来自分类Dev

将精灵图像移动到选定位置

来自分类Dev

在精灵图像中移动SKPhysicsBody

来自分类Dev

触摸图像时移动精灵

来自分类Dev

在精灵图像中移动SKPhysicsBody

来自分类Dev

使用 Swift 3 随机移动精灵

来自分类Dev

JS-使图像随机移动

来自分类Dev

试图使图像随机移动但不移动

来自分类Dev

在pygame中单击时如何使图像移动到随机位置?

来自分类Dev

移动精灵

来自分类Dev

移动精灵

来自分类Dev

jQuery图像贴图-如何更改背景精灵图像的位置?

来自分类Dev

如何使精灵从pygame中的随机x位置掉落

来自分类Dev

如何使用pygame在随机位置生成精灵?

来自分类Dev

Libgdx触摸并拖动屏幕上的任意位置以移动精灵

来自分类Dev

当我更改精灵的位置时,它会继续自行移动

来自分类Dev

从移动获取精灵时,Rect不会更新位置

来自分类Dev

当我更改精灵的位置时,它会继续自行移动

来自分类Dev

HTML div的开始位置和随机移动

来自分类Dev

随机平滑地移动背景图像

来自分类Dev

随机平滑地移动背景图像

来自分类Dev

精灵套件-如何移动精灵?

来自分类Dev

如何使用将camera.position设置为精灵的中心来固定朝向鼠标移动的精灵的随机振动?

来自分类Dev

在固定位置显示随机图像

来自分类Dev

在画布中的随机位置生成图像

来自分类Dev

iOS SpriteKit可以随机移动精灵以及类似空间碰撞之类的东西

来自分类Dev

链接中的图像正在移动链接位置