html5游戏 美术,cocos2d-html5游戏学习之绘画小熊

引擎知识点:Action(动作)、cc.RotateBy(旋转)、cc.RepeatForever(动作循环)用法:var sprite = cc.Sprite.create("a.png");

var rotate = cc.RotateBy.create(1,90);参数1:动作时间  参数2:旋转的角度

sprite.runAction(rotate);//sprite在1秒内旋转90度

sprite.runAction(cc.RepeatForever(rotate));//sprite不断的旋转

sprite.stopAllActions();//停止所有动作

复制代码

更多用法参考官方测试例--------------------------------------------------------------------------------------------------------------一、描绘熊1、在src目录中新建BearSprite.js,并把路径加入到cocos2d.js文件中的appFiles数组中2、定义BearSpritevar BearSprite = cc.Sprite.extend({

/**

*构造函数

**/

ctor:function(){

this._super();

}

});

复制代码

3、Sprite默认没有图片,我们需要赋予一个图片var BearSprite = cc.Sprite.extend({

ctor:function(){

this._super();

this.initWithFile(s_bear_eyesopen);//赋予图片元素

}

});

复制代码

4、把BearSprite添加到游戏场景中//添加蘑菇

this.bear = new BearSprite();

this.bear.setPosition(cc.p(240,60))

this.gameLayer.addChild(this.bear.,g_GameZOder.ui);

复制代码

代码如下图:

forum.php?mod=p_w_upload&aid=MjUwfDE4ZjhmOGZmfDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 刷新浏览器效果如下:

forum.php?mod=p_w_upload&aid=MjUxfGQyYzRhZGQwfDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 二、让熊旋转起来~1、BearSprite中定义一个beginRotate方法,用来开始旋转beginRotate:function(){

var rotate = cc.RotateBy.create(2,360); //旋转角度,第1个参数:时间,第2个参数:在这个时间内旋转的角度

var rep1 = cc.RepeatForever.create(rotate); //循环旋转

this.runAction(rep1);//执行

},

复制代码

2、BearSprite中定义一个方法stopRotate,用来停止旋转的stopRotate:function(){

this.stopAllActions();

}

复制代码

3、在GameScene中调用beginRotate()即可旋转起来this.bear.beginRotate(); //开始旋转刷新浏览器查看效果,熊旋转起来了。三、让熊移动起来~1、在GameSence和Bear中添加update方法作为每帧的循环定义GameSence中的update作为主控制update: function (dt) {

//dt为每帧所消耗的时间,单位为秒

}

复制代码

2、在GameSence中的onEnter加入schedule来启动主update,如下this.schedule(this.update, 0);//参数1:执行函数,参数2:调用间隔时间,0为每帧都调用

forum.php?mod=p_w_upload&aid=MjUyfDM4NjFhYTVlfDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 3、Bear中的update更新自己定义速度velocity等于cc.p(150,150);

forum.php?mod=p_w_upload&aid=MjUzfGQ1NjY5N2ExfDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 定义update更新Bear位置等状态update: function (dt) {

//移动位置

this.setPosition(cc.pAdd(this.getPosition(), cc.pMult(this.velocity, dt)));

}

复制代码

this.velocity为移动的速度在GameSence中的update加入bear的updateupdate: function (dt) {

//dt为每帧所消耗的时间,单位为秒

this.bear.update(dt);

}

复制代码

一般来说有不断位移的话,速度最好乘以dt,这样看起来会流畅以上通过update的循环可以使熊移动起来刷新浏览器查看效果,熊旋移动起来了。四、边界碰撞检测1、BearSprite中定义半径radius来检测碰撞,赋值为25

forum.php?mod=p_w_upload&aid=MjU0fDU2YTlmMzk3fDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 2、BearSprite中定义方法checkHitEdge来做边界碰撞检测代码如下://检查边界碰撞

checkHitEdge: function () {

var pos = this.getPosition();

var winSize = cc.Director.getInstance().getWinSize();

//熊碰到右边边界

if (pos.x > winSize.width - this.radius) {

//假如向右移动

this.velocity.x *= -1;//改变水平速度方向

}

//熊碰到左边边界

if (pos.x < this.radius) {

this.velocity.x *= -1;//改变水平速度方向

}

//熊碰到下面边界

if (pos.y <= this.radius) {

//减少1生命

this.curSence.reduceLives();

}

//熊碰到上边边界

if (pos.y >= winSize.height - this.radius) {

this.velocity.y *= -1;

}

},

复制代码

3、把检测函数checkHitEdge加入到update中,每帧都做判断

forum.php?mod=p_w_upload&aid=MjU1fDgzNTc3MTllfDE0MTkwNjg4MzN8MTU0NDN8NDUxNA%3D%3D&noupdate=yes 刷新浏览器查看效果,熊旋碰到边界能够反弹回来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值