日期:2014-05-17 浏览次数:20722 次
var Stage = {}; Stage.paint(){ //根据舞台上每个小丑当前时刻的状态,重绘舞台 } function Sprite(){ this.go = function(){ setInterval(goAhead,100); function goAhead(){ //一些状态改变代码....... Stage.paint();//要求舞台重绘 } } } function Sprite2(){ This.go = function(){ setInterval(goAhead,100); function goAhead(){ //一些状态改变代码....... Stage.paint();//要求舞台重绘 } } }另外一种设计模式是,只有一个时间线,每隔固定时间显示一祯,就是每隔一定时间舞台Stage步进一祯,步进的时候舞台上所有元件同时步进一次,任何元件要产生动画的时候,加入到这个时间主线中,跟着Stage步进而步进,这样的帧频是固定的,即使在不产生动画的情况下,Stage也是在不停地重绘着
var Stage = {}; Stage.players = { //...将小丑添加至舞台 }; Stage.step(){ for(i in Stage.players){ Stage.players.step();//舞台上每个小丑步进 } setTimeout(this.step,100);//舞台每隔一定时间步进 } function Sprite(){ this.step = function(){ //......步进 } }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title></title> </head> <body> <canvas id='canvas' width="800" height="800"></canvas> <script type='text/javascript' src='jquery.js'></script> <script type='text/javascript'> var kit = {}; var FPS = 30; kit.imgArr = ["img/player.gif","img/bg.png"]; kit.load = function(){ this.img = []; for(i in this.imgArr){ this.img[i] = new Image(); this.img[i].src = this.imgArr[i]; } } kit.load(); var ctx = document.getElementById("canvas").getContext("2d"); var controller = {};//控制对象 controller.stage = {};//舞台对象 controller.stage.players = {//舞台上的小丑们 scen:new Scen(), player1:new Sprite()//一个小精灵 } controller.stage.paint = function(){//绘制桢 ctx.fillStyle = "#fff";//清除前桢 ctx.fillRect(0,0,800,800); for(every in this.players){ this.players[every].paint();//小丑们添加