cocos动态引入图片

import { _decorator, Component, Node, SpriteFrame,resources,Sprite,director} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
    start() {
        const url = 'images/a1/spriteFrame';
        resources.load(url, SpriteFrame, this.onLoadStart.bind(this));
        //点击事件
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
    }
    onLoadStart(err, spriteFrame) {
        if (err) {
            console.log('createRes:' + err);
            return;
        }
        const sprite = this.getComponent(Sprite);
        sprite.spriteFrame = spriteFrame;
        //设置图片的宽高
        sprite.node.width = 100;
        sprite.node.height = 100;
    }
    onTouchStart(event) {
        // 触摸开始
        console.log('Touch Start');
        console.log('Touch Location:', event.getLocation());
        //切换场景
        director.loadScene('scene-001');
    }
    update(deltaTime: number) {
        
    }
}



posted @ 2023-09-07 06:51  窦戈  阅读(97)  评论(0编辑  收藏  举报