这一回我们让http://www.html5china.com/html5games/mogu/index2.html
一、先定义全局变量
- var bearEyesClosedImg = new Image();//闭着眼睛的熊熊
- var horizontalSpeed = 2;//水平速度
- var verticalSpeed = -2; //垂直速度,开始肯定是要向上飘,所以要负数
- var bearAngle = 2;//熊旋转的速度
二、定义熊
首先定义一只公用熊
- //定义动物熊 Animal 继承 游戏对象GameObject
- function Animal() {};
- Animal.prototype = new GameObject();//游戏对象GameObject
- Animal.prototype.angle = 0;//旋转的角度,(用来改变熊的旋转速度)
定义我们使用的熊
- //定义熊实例
- var animal = new Animal();
初始化熊
- bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的
- animal.image = bearEyesClosedImg;//熊图片
- animal.x = parseInt(screenWidth/2);//x坐标
- animal.y = parseInt(screenHeight/2); //y坐标
三、描绘熊在画布上
因为熊是相对移动的,所以我们要加一个基准
- //以当前熊的中心位置为基准
- ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));
- //描绘熊
- ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));