一、实现火苗(主角)“窜动”
1.首先,我们需要准备一组图片,这组图片必须能组成数帧动画,如下:

2.在场景中新建一个名为role的节点,将这三个精灵拖入role节点中
3.为role节点添加一个脚本role.js,在此脚本的properties中添加三个属性,名称分别对应图片的名字;在加入一个timer属性,记录时间的流逝。代码如下:
properties: {
timer:0,
role_01:{
default:null,
type:cc.Sprite
},
role_02:{
default: null,
type: cc.Sprite
},
role_03:{
default:null,
type:cc.Sprite
},
role_04:{
default:null,
type:cc.Sprite
}
},
4.在属性检查器中拖入这三个精灵
5.编写脚本,在update方法中添加如下代码:
update (dt) {
this.timer+=dt; //累加时间流逝
if(this.timer>0.15){ //切换主角显示状态
if(this.role_01.node.active){
this.role_01.node.active=false;
this.role_02.node.active=true;
}else if(this.role_02.node.active){
this.role_02.node.active=false;
this.role_03.node.active=true;
}else if(this.role_03.node.active){
this.role_03.node.active=false;
this.role_04.node.active=true;
}else if(this.role_04.node.active){
this.role_04.node.active=false;
this.role_01.node.active=true;
}
this.timer=0;
}
},
二、实现背景图
这其实是两张左右可以互相衔接的两张图,通过无缝排列,然后进行滚动,实现无限视图效果
三、主角的弹跳与下坠
1.首先为role设置一个speed属性表示速度
2.speed属性会在Update() 内逐渐减少,实现惯性下坠
3.在每次点击后,speed属性会回升一段数值,实现跳跃
四、碰撞组件(collider)的使用
略。。
说明:
属性active:
当前节点的自身激活状态。
值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
如果你想检查节点在场景中实际的激活状态可以使用 Node/activeInHierarchy:property。
本文详细介绍了如何在游戏中实现主角动画的切换,包括火苗(主角)的窜动效果,以及背景图的无限滚动视图。同时,还讲解了主角的弹跳与下坠效果的实现方法,通过设置速度属性并在update()内逐渐减少来实现惯性下坠,点击后速度回升实现跳跃。此外,还提到了碰撞组件(collider)的基本使用。
1013

被折叠的 条评论
为什么被折叠?



