我爱自学网 · 源码
发现乱码?点我试试!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ޱĵ</title>
</head>

<body>
<div id="wrap"></div>
</body>
<script src="http://files.cnblogs.com/Cson/cnGame_v1.0.js"></script>
<script>
var canvas=document.createElement("canvas");
canvas.innerHTML="ʹ֧canvas鿴";
canvas.id="gameCanvas";
var wrap=document.getElementById("wrap");
wrap.appendChild(canvas);
var Src="http://images.cnblogs.com/cnblogs_com/Cson/290336/o_player.png";
var background="background.png";

/* ʼ */
cnGame.init('gameCanvas',{width:500,height:400});
var floorY=cnGame.height-40;
var gameObj=(function(){
	/* Ҷ */
	var player=function(options){
		this.init(options);	
		this.speedX=0;
		this.moveDir;
		this.isJump=false;
	}
	cnGame.core.inherit(player,cnGame.Sprite);
	player.prototype.initialize=function(){
		this.addAnimation(new cnGame.SpriteSheet("playerRight",Src,{frameSize:[50,60],loop:true,width:150,height:60}));
		this.addAnimation(new cnGame.SpriteSheet("playerLeft",Src,{frameSize:[50,60],loop:true,width:150,height:120,beginY:60}));
	}
	player.prototype.moveRight=function(){
		if(cnGame.core.isUndefined(this.moveDir)||this.moveDir!="right"){
			this.moveDir="right";
			this.speedX<0&&(this.speedX=0);
			this.setMovement({aX:10,maxSpeedX:15,maxX:450});
			this.setCurrentAnimation("playerRight");
		}
	}
	player.prototype.moveLeft=function(){
		if(cnGame.core.isUndefined(this.moveDir)||this.moveDir!="left"){
			this.moveDir="left";
			this.speedX>0&&(this.speedX=0);
			this.setMovement({aX:-10,maxSpeedX:15,minX:0});
			this.setCurrentAnimation("playerLeft");
		}
	}
	player.prototype.stopMove=function(){
		
		if(this.speedX<0){
			this.setCurrentImage(Src,0,60);
		}
		else if(this.speedX>0){
			this.setCurrentImage(Src);
		}	
	
		this.moveDir=undefined;
		this.resetMovement();
		
		
	}
	player.prototype.update=function(){
		player.prototype.parent.prototype.update.call(this);//øupdate
		if(cnGame.input.isPressed("right")){
			this.moveRight();	
		}
		else if(cnGame.input.isPressed("left")){
			this.moveLeft();
		}
		else{
			this.stopMove();
		}
		
		
	}

	return {
		initialize:function(){
			cnGame.input.preventDefault(["left","right","up","down"]);
			this.player=new player({src:Src,width:50,height:60,x:0,y:floorY-60});
			this.player.initialize();
			this.background=new cnGame.View({src:background,player:this.player,imgWidth:2301});
			this.background.centerPlayer(true);
			
		},
		update:function(){
			this.player.update();
			this.background.update([this.player]);
		},
		draw:function(){
			this.background.draw();
			this.player.draw();
			
		}

	};
})();
cnGame.loader.start([Src,background],gameObj);
</script>
</html>