QML笔记-QML中SpriteSequence及Sprite的基本使用
发布日期:2021-06-30 11:01:27 浏览次数:2 分类:技术文章

本文共 5010 字,大约阅读时间需要 16 分钟。

目录

 


 

背景

最近在研究一个稍微复杂的QML官方例子,里面有个SpriteSequence及Sprite知识点,我从来没有用过,这次特意花时间提取了出来,方便以后进行查阅。这个东西用来写游戏,或者xx软件的背景,或者xx动态效果,贼吉尔6!!!

 

基本概念

SpriteSequence及Sprite,用法和前端差不多,这里我只说明其中的用法,具体参数含义可以查阅文档;

关键点一:分析素材,如下:

从这个图里面,可以看到人物有4种状态,分别是向下、向左、向右、向上;

每一个有4张图。

SpriteSequence及Sprite有只要选择好要指定的位置(X轴),以及高度(一般是整张图片/4的高度)

就可以实现动态播放了。

关键代码如下:

SpriteSequence {            id: sequence;            width: 50;            height: 100;            interpolate: false;            running: false;            sprites: [                Sprite {                    name: "down";                    source: image1.source;                    frameCount: 4;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "left";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "right";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4*2;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "up";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4*3;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                }            ]        }

其中frameCount和frmeY,frameWidth,frameHeight就是关键点!

interpolate: false;这个参数感觉在背景动态等方面使用特别的好,在此就设置为false;

 

 

博主例子

程序运行截图如下:

程序结构如下:

main.cpp

#include 
#include
int main(int argc, char *argv[]){ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec();}

main.qml

import QtQuick 2.9import QtQuick.Window 2.2Window {    visible: true    width: 640    height: 480    title: qsTr("Sprite Demo")    Image {        id: bg        source: "qrc:/img/bg.jpeg"        fillMode: Image.Pad    }    Soldier{        id : soldier    }}

Soldier.qml

import QtQuick 2.0Item {        Image {            x:200;            id: image1;            source: "qrc:/img/soldier.png";            visible: false        }        SpriteSequence {            id: sequence;            width: 50;            height: 100;            interpolate: false;            running: false;            sprites: [                Sprite {                    name: "down";                    source: image1.source;                    frameCount: 4;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "left";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "right";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4*2;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                },                Sprite {                    name: "up";                    source: image1.source;                    frameCount: 4;                    frameY: image1.height/4*3;                    frameWidth: image1.width/4;                    frameHeight: image1.height/4;                    frameRate: 10;                }            ]        }        focus: true;        Keys.onPressed: {            switch(event.key)            {            case Qt.Key_Up:                sequence.y -= 5;                sequence.jumpTo("up");                sequence.running = true;                break;            case Qt.Key_Down:                sequence.y += 5;                sequence.jumpTo("down");                sequence.running = true;                break;            case Qt.Key_Left:                sequence.x -= 5;                sequence.jumpTo("left");                sequence.running = true;                break;            case Qt.Key_Right:                sequence.x += 5;                sequence.jumpTo("right");                sequence.running = true;                break;            default:                ;            }        }        Keys.onReleased: {            sequence.running = false;        }}

 

 

源码打包下载

转载地址:https://it1995.blog.csdn.net/article/details/90292596 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:QML笔记-在背景图中添加NumberAnimation使得界面效果更佳(小技巧)
下一篇:QML工作笔记-Key Element的使用

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月07日 23时51分04秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【深度学习笔记】循环神经网络和递归神经网络区别 2019-04-30
【学习笔记】英文科技论文常见英语句式积累 2019-04-30
【深度学习笔记】PixelShuffle 2019-04-30
【python3学习笔记】斜杠和双斜杠运算符的区别 2019-04-30
【深度学习笔记】torch.nn.Sequential(* args) 与 torch.nn.Module 2019-04-30
【深度学习笔记】用torch.nn.Sequential()搭建神经网络模型 2019-04-30
【深度学习笔记】用torch.nn.ModuleList搭建神经网络 2019-04-30
【解决错误】AttributeError: module ‘scipy.misc‘ has no attribute ‘imread‘ 2019-04-30
【解决错误】复现RCAN的时候遇到了ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘skimage‘ 2019-04-30
【深度学习笔记】pytorch的点乘(dot product) 2019-04-30
【深度学习笔记】残差 2019-04-30
【错误解决】cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\sr 2019-04-30
【python学习笔记】读取指定文件夹中的图片,结合边缘保留滤波EPF 2019-04-30
【工具和环境】Linux下安装pycharm 2019-04-30
【Accumulation】The last two sentences of the abstract 2019-04-30
【Accumulation】The definition of SISR 2019-04-30
【工具与环境】Windows下安装Sublime Text 3 2019-04-30
【解决错误】ValueError: some of the strides of a given numpy array are negative. 2019-04-30
【工具与环境】Excel中批量插入行 2019-04-30