import { _decorator, Component, Node, resources, path, SpriteFrame ,SpriteAtlas,Animation,AnimationClip, Enum} from 'cc';
import { Joystick,CheckTouch } from './Joystick';
const { ccclass, property } = _decorator;
export let _animation:Animation; //动画组建
export let speed=1; //速度
export let skill=1; //默认技能
export let RoleStatus:boolean=false;
@ccclass('role')
export class role extends Component {
nAnim:Animation=null;
start() {
}
onLoad(){
_animation=this.node.getChildByName("role").addComponent(Animation);
this.nAnim=_animation;
}
update(deltaTime: number) {
}
//设置武器偏移量
setWuqiOffSet(x:number,y:number,k:number){
this.node.setPosition(x,y/2)
}
//设置角色偏移量
setRoleOffSet(x:number,y:number,k:number){
this.node.setPosition(x,y)
}
//加载角色动画资源
onloadRoleRes(){
resources.load("animation/role/1", SpriteAtlas, (err: any, spFrame: SpriteAtlas) => {
if (err && !spFrame) {
console.log("加载失败")
return;
}
// _animation =this.node.getChildByName("role").addComponent(Animation);//添加动画组建
let frames:SpriteFrame[] =spFrame.getSpriteFrames();//this.sprAtlas.getSpriteFrames();//data.getSpriteFrames();
var clip = AnimationClip.createWithSpriteFrames(frames, frames.length);//创建动画帧图
frames.forEach((v,k) => {
var x=v['_offset']['x'];
var y=v['_offset']['y'];
console.log( speed/frames.length*k)
// 添加帧事件
clip.events.push({
frame: 1/frames.length*k, // 秒
func: "setRoleOffSet",// 回调函数名称
params: [x.toString(), y.toString(),k.toString()] // 回调参数
});
});
clip.name = "role";
clip.speed = speed;//速度
clip.sample = 1;// 每秒播放帧数
clip.wrapMode = 0;//模式
_animation.addClip(clip);
_animation.defaultClip=clip
RoleStatus=true;
_animation.play();
//监听播放结束
_animation.on(Animation.EventType.FINISHED, (msg) => {
RoleStatus=false;
}, this);
});
}
}
cocoscreator3.X 动态创建帧动画
于 2022-09-07 14:28:35 首次发布