大佬教程收集整理的这篇文章主要介绍了cocos2d-html5游戏学习之绘画蘑菇,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
引擎知识点:
触摸事件:onTouchBegan(触摸前)、onTouchMoved(触摸并且移动)
用法:
1.var layer = cc.Layer.extend({
2.ctor:function(){
4. cc.Director.geTinstance().getTouchDispatcher().addTargetedDelegate(this,truE);//把当前对象加入到触摸监听行列
5. },
6. onTouchBegan:function (touch,event) {
7. //监听触摸瞬间
8. return true;
9. },
10. onTouchMoved:function (touch,event) {
11. //监听触摸移动,只有onTouchBegan返回true时才执行到这一步
12. }
13.});
复制代码
--------------------------------------------------------------------------------------------------------------
一、描绘蘑菇
1、由于蘑菇有自己的行为,我们可以定义个自己的@H_790_1@mushroomSprite继承自cc.Sprite
在src目录中新建@H_790_1@mushroomSprite.js,并把路径加入到cocos2d.js文件中的appFiles数组中
定义@H_790_1@mushroomSprite
1.var MushroomSprite = cc.Sprite.extend({
2. /**
3. *构造函数
4. **/
5.
6. ctor:function(){
8. }
9.});
复制代码
2、Sprite默认没有图片,我们需要赋予一个图片
1.var MushroomSprite = cc.Sprite.extend({
2. ctor:function(){
4. this.initWithFile(s_mushroom);//赋予图片元素
5. }
6.});
复制代码
3、在GameScene.js中把@H_790_1@mushroomSprite添加到游戏场景中
1.//添加蘑菇
2. this.mushroom = newMushroomSprite();
3. this.mushroom.setAnchorPoint(cc.p(0.5,0));
4. this.mushroom.setPosition(cc.p(240,0));
5. this.gameLayer.addChild(this.mushroom,g_GameZOder.ui);
复制代码
这里为了方便管理层级,定义了个全局对象g_GameZOder: s.width=650;" width="600" height="493" src="http://img.voidcn.com/vcimg/static/loading.png" style="BACkground:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" data-src="http://snaile.blog.51cto.come/u261/themes/default/images/spacer.gif">
var g_GameZOder = {Bg: 0,ui: 1}
为了代码更清晰,定义个initData函数来初始化数据
代码如下图:
s.width=650;" width="480" height="320" src="http://img.voidcn.com/vcimg/static/loading.png" style="BACkground:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" data-src="http://snaile.blog.51cto.come/u261/themes/default/images/spacer.gif">
s.width=650;" width="600" height="344" src="http://img.voidcn.com/vcimg/static/loading.png" style="BACkground:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" data-src="http://snaile.blog.51cto.come/u261/themes/default/images/spacer.gif">
1.cc.Director.geTinstance().getTouchDispatcher().addTargetedDelegate(this,truE);
复制代码
s.width=650;" width="600" height="92" src="http://img.voidcn.com/vcimg/static/loading.png" style="BACkground:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" data-src="http://snaile.blog.51cto.come/u261/themes/default/images/spacer.gif">
1.//刚触摸瞬间
2. onTouchBegan:function (touch,event) {
3. return true;
4. },
5. //触摸移动
6. onTouchMoved:function (touch,event) {
7. cc.log("onTouchMoved");
8. }
复制代码
使用谷歌浏览器查看log记录,假如输出"onTouchMoved",说明监听到了 s.width=650;" width="585" height="183" src="http://img.voidcn.com/vcimg/static/loading.png" style="BACkground:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" data-src="http://snaile.blog.51cto.come/u261/themes/default/images/spacer.gif">
1.//触摸移动
2. onTouchMoved:function (touch,event) {
3. cc.log("onTouchMoved");
4. var touchPoint =touch.getLOCATIOn();
5. this.setPositionX(touchPoint.X);//设置X轴位置等于触摸的x位置
6. }
复制代码
4、这个时候点击整个场景移动,蘑菇都会跟随移动,需要限制触摸点在蘑菇上面时才移动,如下:
1.//判断触摸点是否在图片的区域上
2. containsTouchLOCATIOn:function (touch) {
3. //获取触摸点位置
4. var getPoint = touch.getLOCATIOn();
5. //获取图片区域尺寸
6. varcontentSize=this.getContentSize();
7. //定义拖拽的区域
8. var myRect = cc.rect(0,contentSize.width,contentSize.height);
9. myRect.origin.x +=this.getPosition().x-this.getContentSize().width/2;
10. myRect.origin.y +=this.getPosition().y-this.getContentSize().height/2;
11. //判断点击是否在区域上
12. returncc.Rect.CCRectContainsPoint(myRect,getPoint);
13. },
14.//刚触摸瞬间
15. onTouchBegan:function (touch,event) {
16. if(!this.containsTouchLOCATIOn(touch)) return false;//判断触摸点是否在蘑菇上
17. return true;
18. },
19.
复制代码
到这里,蘑菇的移动已经正常了,@H_790_1@mushroomSprite.js最终代码如下
1.var MushroomSprite = cc.Sprite.extend({
2. ctor:function(){
4. this.initWithFile(s_mushroom);//赋予图片元素
5. cc.Director.geTinstance().getTouchDispatcher().addTargetedDelegate(this,truE);
6. },
7. //判断触摸点是否在图片的区域上
8. containsTouchLOCATIOn:function (touch) {
9. //获取触摸点位置
10. var getPoint = touch.getLOCATIOn();
11. //获取图片区域尺寸
12. varcontentSize=this.getContentSize();
13. //定义拖拽的区域
14. var myRect = cc.rect(0,contentSize.height);
15. myRect.origin.x +=this.getPosition().x-this.getContentSize().width/2;
16. myRect.origin.y +=this.getPosition().y-this.getContentSize().height/2;
17. //判断点击是否在区域上
18. returncc.Rect.CCRectContainsPoint(myRect,getPoint);
19. },
20. //刚触摸瞬间
21. onTouchBegan:function (touch,event) {
22. if(!this.containsTouchLOCATIOn(touch)) return false;
23. return true;
24. },
25. //触摸移动
26. onTouchMoved:function (touch,event) {
27. cc.log("onTouchMoved");
28. var touchPoint =touch.getLOCATIOn();
29. this.setPositionX(touchPoint.X);//设置X轴位置等于触摸的x位置
30. }
31.});
复制代码
以上是大佬教程为你收集整理的cocos2d-html5游戏学习之绘画蘑菇全部内容,希望文章能够帮你解决cocos2d-html5游戏学习之绘画蘑菇所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。