日期:2014-05-16 浏览次数:20372 次
上一讲我们谈到了如何让静态人物变为动态,今天我们来谈谈如何使人物移动,因为这一讲涉及上一讲内容所以我把上一讲的链接写在下方:
http://blog.csdn.net/yorhomwang/article/details/7984576
今天我准备换几张图片,这样更新鲜些。
pic1.png | pic2.png | pic3.png |
var moveLengthLeft = 0; var moveLengthTop = 0; var actionST = 0; var timeInterval = 150; var pic = 0; function action() { var pic1 = "./pic2.png"; var pic2 = "./pic3.png"; var pic3 = "./pic1.png"; var actionArray = [pic1, pic2, pic3]; var doc = document.getElementById("ID_IMG_CAOCAO"); if (pic == actionArray.length - 2){ pic = 0; }else{ pic += 1; } if(pic > 2){ pic = 2; doc.src = "./pic1.png" } doc.src = actionArray[pic]; } function walk() { setInterval(action, timeInterval); for(var i = 0; i < 100; i++){ $("#ID_IMG_CAOCAO").animate({marginLeft: moveLengthLeft}, 10, function(){ //用jquery中的animate来控制人物行走 actionST ++; if(actionST == 100){ standCaocao(); } }); //在动画做完时调用callback。callback里可以放函数。 $("#ID_IMG_CAOCAO").animate({marginTop: moveLengthTop}, 10); moveLengthLeft += 2; moveLengthTop += 1; } } function standCaocao() { pic = 2; }局部分析如下:
function action() { var pic1 = "./pic2.png"; var pic2 = "./pic3.png"; var pic3 = "./pic1.png"; var actionArray = [pic1, pic2, pic3]; var doc = document.getElementById("ID_IMG_CAOCAO"); if (pic == actionArray.length - 2){ pic = 0; }else{ pic += 1; } if(pic > 2){ pic = 2; doc.src = "./pic1.png" } doc.src = actionArray[pic]; }
function walk() { setInterval(action, timeInterval); for(var i = 0; i < 100; i++){ $("#ID_IMG_CAOCAO").animate({marginLeft: moveLengthLeft}, 10, function(){ //用jquery中的animate来控制人物行走 actionST ++; if(actionST == 100){ standCaocao(); } }); //在动画做完时调用callback。callback里可以放函数。 $("#ID_IMG_CAOCAO").animate({marginTop: moveLengthTop}, 10); m