在游戏中动画的设计很中要。

在QML中,它提供了丰富的animation。可是有时我们须要对图像进行变化,就像放电影一样。在今天的这篇文章中,我们将设计一个能够变化图像的动画。

我们能够通过Qt所提供的Sprite功能来实现。

为了设计的方便,我们先设计一个我们自己的bear动画,这个动画的图像大小为: 2048x256。它刚好是8副图256x256

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="800" height="150" alt="">

在我们的Sprite设计中,我们想依照上述图像显示的顺序依次显示每一个图像,这样就能够形成一个能够连续变化的动画效果。

直接把我们的动画设计文件BearSprite贴出来:

BearSprite.qml

import QtQuick 2.0

Item  {
width: 256
height: 256 SpriteSequence {
id: fishSprite
anchors.fill: parent
interpolate: false
goalSprite: "" Sprite {
name: "first"
source: "./gfx/Bear2.png"
frameWidth: 256
frameHeight: 256
frameCount: 1
frameDuration: 800
frameDurationVariation: 400
to: { "second" : 1 }
} Sprite {
name: "second"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "third" : 1 }
} Sprite {
name: "third"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*2
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "fourth" : 1 }
} Sprite {
name: "fourth"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*3
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "fifth" : 1 }
} Sprite {
name: "fifth"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*4
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "sixth" : 1 }
} Sprite {
name: "sixth"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*5
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "seventh" : 1 }
} Sprite {
name: "seventh"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*6
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "eighth" : 1 }
} Sprite {
name: "eighth"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*7
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "first" : 1 }
} Sprite { //WORKAROUND: This prevents the triggering of a rendering error which is currently under investigation.
name: "dummy"
source: "./gfx/Bear2.png"
frameCount: 8
frameWidth: 256
frameHeight: 256
frameX: 0
frameDuration: 200
}
} }


在上面的设计中,我们使用了一个SpriteSequence,里面放了一些我们所须要的Sprite。

        Sprite {
name: "sixth"
source: "./gfx/Bear2.png"
frameCount: 1
frameX: 256*5
frameWidth: 256
frameHeight: 256
frameDuration: 800
frameDurationVariation: 400
to: { "seventh" : 1 }
}

这里的每一个Sprite的设计都差点儿都差点儿相同。每一个Sprite都有一个自己的名字。这里注意frameX。它事实上是在我们上面显示的图里的x坐标位置。比方256x5。表示的是滴5副图。另外,我们的frameHeight和frameWidth也是和原图的大小是一样的,尽管在实际的显示中这个大小能够在Main.qml中能够设置。

使用相同的方法,我们能够做一个FishSprite。

FishSprite.qml




import QtQuick 2.0
import QtMultimedia 5.0 Item {
width: 64
height: 64
property real hp: 3 SoundEffect {
id: spawnSound
source: "./audio/catch.wav"
loops:SoundEffect.Infinite
} SoundEffect {
id: killedSound
source: "./audio/catch-action.wav"
} SpriteSequence {
id: fishSprite
anchors.fill: parent
interpolate: false
goalSprite: "" Sprite {
name: "left"
source: "./gfx/mob-idle.png"
frameWidth: 64
frameHeight: 64
frameCount: 1
frameDuration: 800
frameDurationVariation: 400
to: { "front" : 1 }
} Sprite {
name: "front"
source: "./gfx/mob-idle.png"
frameCount: 1
frameX: 64
frameWidth: 64
frameHeight: 64
frameDuration: 800
frameDurationVariation: 400
to: { "left" : 1, "right" : 1 }
} Sprite {
name: "right"
source: "./gfx/mob-idle.png"
frameCount: 1
frameX: 128
frameWidth: 64
frameHeight: 64
frameDuration: 800
frameDurationVariation: 400
to: { "front" : 1 }
} Sprite { //WORKAROUND: This prevents the triggering of a rendering error which is currently under investigation.
name: "dummy"
source: "./gfx/melee-idle.png"
frameCount: 8
frameWidth: 64
frameHeight: 64
frameX: 0
frameDuration: 200
} NumberAnimation on x {
id: fishSwim
running: false
property bool goingLeft: fishSprite.currentSprite == "right"
to: goingLeft ? -360 : 360
duration: 300
} Component.onCompleted: {
spawnSound.play()
}
} SpriteSequence {
id: bubble
width: 64
height: 64
scale: 0.4 + (0.2 * hp)
interpolate: false
goalSprite: "" Behavior on scale {
NumberAnimation { duration: 150; easing.type: Easing.OutBack }
} Sprite {
name: "big"
source: "./gfx/catch.png"
frameCount: 1
to: { "burst" : 0 }
} Sprite {
name: "burst"
source: "./gfx/catch-action.png"
frameCount: 3
frameX: 64
frameDuration: 200
} Sprite { //WORKAROUND: This prevents the triggering of a rendering error which is currently under investigation.
name: "dummy"
source: "./gfx/melee-idle.png"
frameCount: 8
frameWidth: 64
frameHeight: 64
frameX: 0
frameDuration: 200
}
SequentialAnimation on width {
loops: Animation.Infinite
NumberAnimation { from: width * 1; to: width * 1.1; duration: 800; easing.type: Easing.InOutQuad }
NumberAnimation { from: width * 1.1; to: width * 1; duration: 1000; easing.type: Easing.InOutQuad }
}
SequentialAnimation on height {
loops: Animation.Infinite
NumberAnimation { from: height * 1; to: height * 1.15; duration: 1200; easing.type: Easing.InOutQuad }
NumberAnimation { from: height * 1.15; to: height * 1; duration: 1000; easing.type: Easing.InOutQuad }
}
}
}


Main.qml


import QtQuick 2.0
import Ubuntu.Components 1.1 /*!
\brief MainView with a Label and Button elements.
*/ MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest
applicationName: "sprite.liu-xiao-guo" /*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true // Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false width: units.gu(60)
height: units.gu(85) Page {
id: page
title: i18n.tr("sprite") Column {
anchors.fill: parent FishSprite {
height: units.gu(30)
width: units.gu(30)
} BearSprite {
id: bear
height: units.gu(30)
width: units.gu(30) NumberAnimation on x {
to: page.width
duration: 8*800 onRunningChanged: {
if ( running == false) {
bear.x = 0
start()
}
}
}
}
}
}
}

执行我们的QML应用:

  

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="300" alt="">   



怎样在QML中利用Sprite来做我们须要的动画的更多相关文章

  1. Nginx 中利用 Lua 脚本做访问控制

    使用场景 需要在后端服务之前做访问控制,或没有后端服务的场景,如静态文件. 实验环境 Ubuntu 14.04 Nginx 1.4.6 安装 Lua 运行环境 sudo apt-get install ...

  2. 怎样在QML中使用multitouch

    在Qt QML中.它能够利用multitouch来做一些我们想做的事情.在今天的文章中.我们将介绍怎样使用multitouch来做一些我们想做的事. 事实上,在QML中利用多点触控是很easy的一件事 ...

  3. EBS中利用Socket与外系统通信

    某银行要求做一个签到签退功能,日终EBS系统发送报文与核心系统对帐,规定利用Socket来做传送,记录下步骤: 1.编辑: $INST_TOP/ora/10.1.3/j2ee/oacore/appli ...

  4. PHP中利用GD实现的柱状图

    PHP中利用GD实现的柱状图,自己写的一个画柱状图的类,上代码. <?php Class Chart{ private $image; // 定义图像 private $title; // 定义 ...

  5. nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录

    geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...

  6. 【Unity3D基础教程】给初学者看的Unity教程(三):通过制作Flappy Bird了解Native 2D中的Sprite,Animation

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 上一次我们讲了MonoBehaviou ...

  7. ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)

    原文:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100) 对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是 ...

  8. 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (一)

    前言有使用 ASP.NET MVC 的朋友们一定多多少少有听过 TryUpdateModel,之前就看了很多有关它的文章,但在专案实务上都未曾实际使用过,而 TryUpdateModel 不仅能利用 ...

  9. SQL注入中利用XP_cmdshell提权的用法(转)

    先来介绍一下子服务器的基本情况,windows 2000 adv server 中文版,据称 打过了sp3,asp+iis+mssql .首先扫描了一下子端口,呵呵,开始的一般步骤. 端口21开放: ...

随机推荐

  1. xmpp 环境配置

    XMPP框架地址:http://xmpp.org/xmpp-software/libraries/ 配置流程

  2. HackerRank# Candies

    原题地址 LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢 代码: #include <cmath> #includ ...

  3. 【容斥】HDU 4135 Co-prime

    acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...

  4. ubuntu问题解答集锦

    一.su root提示认证失败 su root提示认证失败 ubuntu root是默认禁用了,不答应用root登陆,所以先要设置root密码.   执行:sudo passwd root 接着输入密 ...

  5. 纯干货,Mysql innodb的ACID特性是怎么实现的?以及高并发情况下会出现的问题

    首先说说什么是ACID: 它们分别是Atomicity(原子性),Consistency(一致性),Isolation(隔离性),Transaction(持久性) 原子性: 意为单个事务里的多个操作要 ...

  6. 16.1113 模拟考试T1

    笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~N的整数.定义序列的代价为累加(1->m-1 abs(ai+1-ai))你现在可以选择两个数x和y,并将序列?中 ...

  7. 【Codeforces Round #503 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...

  8. Java面试题集(四)

    二. Java Web基础部分 在js中如何创建一个对象? var p1={name:”tom”,”age”:12}; function Person(name,age){ this.name=nam ...

  9. Laravel 5.1 报错:[App\Http\Requests\Request] is not instantiable

    Laravel 5.1 报错:[App\Http\Requests\Request] is not instantiable 错误提示: Whoops, looks like something we ...

  10. Struts2的值栈和OGNL牛逼啊

    Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...