简体   繁体   English

如何在移动时给我的javascript球滚动动画?

[英]How to give my javascript ball a rolling animation when it moves?

I have the following HTML code: 我有以下HTML代码:

<img src="game_files/ball.png" id="ball" /> It's a div containing an image of a ball (top view of a ball) <img src="game_files/ball.png" id="ball" />这是一个包含球形图像的div(球的顶视图)

This ball I can move with my arrow keys and some javacript to go up, left, right, down, etc etc. I do this with this code: 这个球我可以用我的箭头键和一些javacript移动上,左,右,下等等。我用这段代码做这个:

var ball = function  () {
    var inited = false,
        el,
        xcheck = 50,
        x = 50,
        ycheck = 50,
        y = 50,
        xspeed = 0,
        yspeed = 0,
        power = 0.4,
        friction = 0.99,
        xwind = 0,
        ywind = 0,
        dead = true,
        timer = function(keys) {
            if (dead === true) {
                return;
            }
            if (keys[38] === true) {
                yspeed += power;
            }
            if (keys[40] === true) {
                yspeed -= power;
            }
            if (keys[37] === true) {
                xspeed += power;
            }
            if (keys[39] === true) {
                xspeed -= power;
            }
            xspeed = xspeed * friction - xwind;
            yspeed = yspeed * friction - ywind;
            if (Math.abs(xspeed) < 0.004) {
                xspeed = 0;
            }
            if (Math.abs(xspeed) < 0.004) {
                xspeed = 0;
            }
            x -= xspeed;
            y -= yspeed;
            plane.move(x,y);
        };
    return {
        init: function() {
            if (inited != true) {
                inited = true;
                el = document.getElementById('ball');
                roller.register(timer, 'time');
            }
        }
    };
}();

This all works, but the ball has no rolling animation! 这一切都有效,但球没有滚动动画! The image just slides to the left or right How can I add this? 图像只是向左或向右滑动如何添加? I found this tutorial: http://www.emanueleferonato.com/2007/06/10/creation-of-realistic-spheres-in-flash-with-textures-and-masking/ which I thought could help me out. 我找到了这个教程: http//www.emanueleferonato.com/2007/06/10/creation-of-realistic-spheres-in-flash-with-textures-and-masking/我认为可以帮助我。

Unfortunately, this is for a flash ball (I was hoping this would apply in some sort to JS too). 不幸的是,这是一个闪光球(我希望这也适用于JS)。 This tutorial shows exactly what I want: a ball with rolling animation (see the tutorial page near the end of the tutorial the ball with the leopard skin, just below: Lines 26-37: If the position of the texture relatively to the ball...). 本教程准确显示了我想要的内容:一个带有滚动动画的球(参见教程页面附近的教程页面,带有豹皮的球,正好在下面:第26-37行:如果纹理的位置相对于球。 ..)。

How can I apply this to my JS ball? 我怎样才能将它应用到我的JS球? Any thoughts? 有什么想法吗?

Kind regards and a happy new year 亲切的问候和新年快乐

ps I use/load jquery as well ps我也使用/加载jquery

--EDIT-------- - 编辑 - - - -

Created a fiddle: http://jsfiddle.net/mauricederegt/dhUw5/ 创造了一个小提琴: http//jsfiddle.net/mauricederegt/dhUw5/

1- open the fiddle 1-打开小提琴

2- click the 1 2-单击1

3- use the arrow keys to move the ball around, stay on the green field! 3-使用箭头键移动球,留在绿色的田野!

4- see, no rolling effect 4-看,没有滚动效果

To have the same effect as in the Flash example you linked you just have to add a background to the element and move that accordingly. 要获得与您链接的Flash示例相同的效果,您只需要为元素添加背景并相应地移动它。

I replaced your img element with a span and gave it the background-image via CSS and a border-radius to make it round. 我用一个span替换你的img元素,并通过CSS和border-radius给它背景图像,使其成为圆形。 In your plane.move function I added the following line: 在你的plane.move函数中,我添加了以下行:

document.getElementById('ball').style.backgroundPosition = x + 'px ' + y +'px';

Voilá, same effect as in the Flash example: http://jsfiddle.net/dhUw5/1/ Voilá,与Flash示例中的效果相同: http//jsfiddle.net/dhUw5/1/

You would probably use HTML5 canvas. 您可能会使用HTML5画布。 You would clip part of the image used as the ball's surface using drawImage and then mask it to get a circle shape . 您将使用drawImage剪切用作球表面的部分图像,然后将其遮盖以获得圆形 Then you can animate it by redrawing the canvas, with the clipping position (sx, sy) altered in a similar way to the flash example you linked to. 然后,您可以通过重绘画布来对其进行动画处理,剪裁位置(sx,sy)的更改方式与您链接到的flash示例类似。 (Following is not tested; make a jsfiddle with your original code if you want to try it.) (以下未经过测试;如果您想尝试,请使用原始代码制作一个jsfiddle。)

// ... when the user moves
this.sy += yspeed;
this.sx += xspeed;
if (this.sx > textureSize) {
    this.sx -= textureSize;
}
else if (this.sx < 0) {
    this.sx += textureSize;
}
if (this.sy > textureSize) {
    this.sy -= textureSize;
}
else if (this.sy < 0) {
    this.sy += textureSize;
}

// ... redraw the ball once every frame
context.clearRect(0, 0, canvasWidth, canvasHeight); // clear the canvas
context.save();
context.beginPath();
context.arc(this.x, this.y, ballRadius, Math.PI * 2, false);
context.clip();    
context.drawImage(ballImage, this.sx, this.sy, 
                  ballDiameter, ballDiameter, 
                  this.x, this.y,
                  ballDiameter, ballDiameter);      // redraw the ball      
context.restore();

Alternatively you could use a div with the texture as a background image, and mask it to make a circle using CSS3 or by overlaying another image (see Best way to mask an image in HTML5 ). 或者,您可以使用纹理作为背景图像的div,并使用CSS3或通过覆盖其他图像来掩盖它以创建圆形(请参阅在HTML5中屏蔽图像的最佳方式 )。 Then you would change the coordinates of the background image (texture) using 然后,您将使用更改背景图像(纹理)的坐标

ballElement.style.backgroundPosition = this.sx + 'px ' + this.sy + 'px';

This is quite easy if you use CSS transforms. 如果使用CSS转换,这很容易。 In this example the speed of rotation is determined according to the scalar value of the speed the ball is currently traveling at: 在这个例子中,旋转速度是根据球当前行进速度的标量值确定的:

    var increment = Math.round(Math.sqrt(Math.pow(xspeed, 2) + Math.pow(yspeed, 2)));
    if (!isNaN(increment)) {
        currangle = currangle + ((xspeed < 0 ? 1 : -1) * increment);
    }

    $('#ball').css({
        '-webkit-transform': 'rotate(' + currangle + 'deg)'
    });

    $('#ball').css({
        '-moz-transform': 'rotate(' + currangle + 'deg)'
    });

This code goes in the move method in plane . 此代码在plane中的move方法中。 Here is a demonstration: http://jsfiddle.net/BahnU/show/ 这是一个演示: http//jsfiddle.net/BahnU/show/

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

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