创建了对象后,怎么动起来呢?
那就让角色动起来吧
首先来梳理一下动起来需要哪些步骤:
- 引用
Dragonbones
命名空间(这个以后就省略了,刚需!!!) - 加载资源
- 创建对象
- 监测按键
- 移动
按照这个顺序来一个:
1.创建一个脚本,并引用命名空间
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DragonBones;
public class PlayerMove : BaseDemo{
}
2.加载资源
public class PlayMove : BaseDemo{
protected override void OnStart(){
UnityFactory.factory.LoadDragonBonesData ("person_1/person_1_ske");
UnityFactory.factory.LoadTextureAtlasData ("person_1/person_1_tex");
}
}
3.创建对象
这里我们不用之前的方法创建对象了,而是把对象封装在一个类里,然后把关于对象的操作也放在这个类里,程序看起来就简单明了了。这个类后面我们再来具体实现它。
public class PlayMove : BaseDemo{
private Player _player;
protected override void OnStart(){
UnityFactory.factory.LoadDragonBonesData ("person_1/person_1_ske");
UnityFactory.factory.LoadTextureAtlasData ("person_1/person_1_tex");
this._player = new Player ();
}
}
4.监测按键并调用移动的方法
先来定义一下上下左右的按键
public const KeyCode left = KeyCode.A;
public const KeyCode right = KeyCode.D;
public const KeyCode up = KeyCode.W;
public const KeyCode down = KeyCode.S;
调用Move()
方法移动角色,这里的OnUpdate()
方法在BaseDemo.cs
125行,是用virtual修饰符声明了一个虚方法,并且在Update()
中调用了它,所以在这个PlayerMove.cs
中会自动执行这个方法。
protected override void OnUpdate(){
var isLeft = Input.GetKey (left);
var isRight = Input.GetKey (right);
var isUp = Input.GetKey (up);
var isDown = Input.GetKey (down);
if (isUp) {
this._player.Move (0, 1);
} else if (isDown) {
this._player.Move (0, -1);
}
if (isLeft) {
this._player.Move (-1, 0);
} else if (isRight) {
this._player.Move (1, 0);
}
if (isLeft == isRight && isUp == isDown) {
this._player.Move (0, 0);
}
}
5.完成Player类
public class Player
{
private const float NORMALIZE_MOVE_SPEED = 0.1f;//标准移动速度
private const float MAX_MOVE_SPEED_FRONT = NORMALIZE_MOVE_SPEED * 1.4f;//最大向前移动速度
private const float MAX_MOVE_SPEED_BACK = NORMALIZE_MOVE_SPEED * 1.0f;//最大向后移动速度
private int _faceDir = 1;//方向
private int _moveHorizontalDir = 0;//水平移动距离
private int _moveVerticleDir = 0;//垂直移动距离
private float _speedX = 0.0f;//水平移动速度
private float _speedY = 0.0f;//垂直移动速度
private DragonBones.Armature _armature;
private UnityArmatureComponent _armatureComponent;
private DragonBones.AnimationState _walkState = null;//动画状态,播放动画时产生,可以对每个播放的动画进行更细致的控制和调节。
//构造方法
public Player()
{
this._armatureComponent = UnityFactory.factory.BuildArmatureComponent("Armature人设1改(动画)", "person_1");
this._armature = this._armatureComponent.armature;
this._armatureComponent.name = "person_2";
this._armatureComponent.transform.localPosition = new Vector3(0.0f, CoreElement.GROUND, 0.0f);
this._UpdateAnimation();
}
//移动方法
public void Move(int horizontalDir,int verticalDir)
{
if (this._moveHorizontalDir == horizontalDir && this._moveVerticleDir == verticalDir)
{
return;
}
this._moveHorizontalDir = horizontalDir;
this._moveVerticleDir = verticalDir;
this._UpdateAnimation();
}
public void Update()
{
this._UpdatePosition();
}
//更新位置方法
private void _UpdatePosition()
{
if (this._speedX == 0.0f && this._speedY == 0.0f)
{
return;
}
var position = this._armatureComponent.transform.localPosition;
if (this._speedX != 0)
{
position.x += this._speedX * this._armatureComponent.animation.timeScale;
if (position.x < -4.0f)
{
position.x = -4.0f;
}
else if (position.x > 4.0f)
{
position.x = 4.0f;
}
}
if (this._speedY != 0.0f) {
position.y += this._speedY * this._armatureComponent.animation.timeScale;
if (position.y < -4.0f) {
position.y = -4.0f;
} else if (position.y > 4.0f) {
position.y = 4.0f;
}
}
this._armatureComponent.transform.localPosition = position;
}
//更新动画方法
private void _UpdateAnimation()
{
//没有移动时的动画
if (this._moveHorizontalDir == 0 && _moveVerticleDir == 0)
{
this._speedX = 0;
this._speedY = 0;
this._armature.animation.FadeIn("stand", -1.0f, -1, 0).resetToPose = false;
this._walkState = null;
}
else
{
//移动时的动画
if (this._walkState == null)
{
this._walkState = this._armature.animation.FadeIn("moving", -1.0f, -1, 0);
this._walkState.resetToPose = false;
}
//控制角色迈腿方向
if (this._moveHorizontalDir * this._faceDir > 0 || this._moveVerticleDir * this._faceDir > 0) {
this._walkState.timeScale = MAX_MOVE_SPEED_FRONT / NORMALIZE_MOVE_SPEED;
}
else if (this._moveVerticleDir * this._faceDir < 0)
{
this._walkState.timeScale = MAX_MOVE_SPEED_FRONT / NORMALIZE_MOVE_SPEED;
}
else
{
this._walkState.timeScale = -MAX_MOVE_SPEED_BACK / NORMALIZE_MOVE_SPEED;
}
//控制角色移动方向
if (this._moveHorizontalDir * this._faceDir >= 1)
{
this._speedX = MAX_MOVE_SPEED_FRONT * this._faceDir;
Debug.Log (this._moveHorizontalDir * this._faceDir);
}
else if(this._moveHorizontalDir * this._faceDir <= -1)
{
this._speedX = -MAX_MOVE_SPEED_BACK * this._faceDir;
Debug.Log (this._moveHorizontalDir * this._faceDir);
}
if (this._moveVerticleDir * this._faceDir >= 1) {
this._speedY = MAX_MOVE_SPEED_FRONT * this._faceDir;
} else if(this._moveVerticleDir * this._faceDir <= -1){
this._speedY = -MAX_MOVE_SPEED_FRONT * this._faceDir;
}
}
}
}
最后贴出完整代码,详细解析都在注释里:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DragonBones;
public class PlayerMove : BaseDemo {
public const float GROUND = 0.0f;//地面。
//键盘控制
public const KeyCode left = KeyCode.A;
public const KeyCode right = KeyCode.D;
public const KeyCode up = KeyCode.W;
public const KeyCode down = KeyCode.S;
//Player类的对象
private Player _player;
protected override void OnStart()
{
// Load data加载数据
UnityFactory.factory.LoadDragonBonesData("person_1/person_1_ske");
UnityFactory.factory.LoadTextureAtlasData("person_1/person_1_tex");
//new一个实例
this._player = new Player();
}
protected override void OnUpdate()
{
// Input
var isLeft = Input.GetKey(left);
var isRight = Input.GetKey(right);
var isUp = Input.GetKey(up);
var isDown = Input.GetKey(down);
/*移动 i
* sLeft == isRight 这句话有点坑,就是无论一起按还是一起不按都生效。
* if(){……}
* else if(){……}
* else{}
* 与
* if(){……}else{}
* if(){……}else{}
* 会产生不一样的效果
* 下面判断才能达到我上下左右移动不出差的效果,其实就有很多种情况,列出一种没错但是效果不一致的情况
* if (isUp) {this._player.Move (0, 1);}
* else if (isDown) {this._player.Move (0, -1);}
* else if (isLeft) {this._player.Move (-1, 0);}
* else if (isRight) {this._player.Move (1, 0);}
* else if (isLeft == isRight && isUp == isDown) {this._player.Move (0, 0);}
* 当然,控制角色上下移动的方法还很多。
* */
if (isUp) {
this._player.Move (0, 1);
} else if (isDown) {
this._player.Move (0, -1);
}
if (isLeft) {
this._player.Move (-1, 0);
} else if (isRight) {
this._player.Move (1, 0);
}
if (isLeft == isRight && isUp == isDown) {
this._player.Move (0, 0);
}
this._player.Update();
}
}
//玩家类start
public class Player
{
private const float NORMALIZE_MOVE_SPEED = 0.1f;//标准移动速度
private const float MAX_MOVE_SPEED_FRONT = NORMALIZE_MOVE_SPEED * 1.4f;//最大向前移动速度
private const float MAX_MOVE_SPEED_BACK = NORMALIZE_MOVE_SPEED * 1.0f;//最大向后移动速度
private int _faceDir = 1;//方向
private int _moveHorizontalDir = 0;//水平移动距离
private int _moveVerticleDir = 0;//垂直移动距离
private float _speedX = 0.0f;//水平移动速度
private float _speedY = 0.0f;//垂直移动速度
private DragonBones.Armature _armature;
private UnityArmatureComponent _armatureComponent;
private DragonBones.AnimationState _walkState = null;//动画状态,播放动画时产生,可以对每个播放的动画进行更细致的控制和调节。
//构造方法
public Player()
{
this._armatureComponent = UnityFactory.factory.BuildArmatureComponent("Armature人设1改(动画)", "person_1");
this._armature = this._armatureComponent.armature;
this._armatureComponent.name = "person_2";
this._armatureComponent.transform.localPosition = new Vector3(0.0f, CoreElement.GROUND, 0.0f);
this._UpdateAnimation();
}
//移动方法
public void Move(int horizontalDir,int verticalDir)
{
if (this._moveHorizontalDir == horizontalDir && this._moveVerticleDir == verticalDir)
{
return;
}
this._moveHorizontalDir = horizontalDir;
this._moveVerticleDir = verticalDir;
this._UpdateAnimation();
}
public void Update()
{
this._UpdatePosition();
}
//更新位置方法
private void _UpdatePosition()
{
if (this._speedX == 0.0f && this._speedY == 0.0f)
{
return;
}
var position = this._armatureComponent.transform.localPosition;
if (this._speedX != 0)
{
position.x += this._speedX * this._armatureComponent.animation.timeScale;
if (position.x < -4.0f)
{
position.x = -4.0f;
}
else if (position.x > 4.0f)
{
position.x = 4.0f;
}
}
if (this._speedY != 0.0f) {
position.y += this._speedY * this._armatureComponent.animation.timeScale;
if (position.y < -4.0f) {
position.y = -4.0f;
} else if (position.y > 4.0f) {
position.y = 4.0f;
}
}
this._armatureComponent.transform.localPosition = position;
}
//更新动画方法
private void _UpdateAnimation()
{
//没有移动时的动画
if (this._moveHorizontalDir == 0 && _moveVerticleDir == 0)
{
this._speedX = 0;
this._speedY = 0;
this._armature.animation.FadeIn("stand", -1.0f, -1, 0).resetToPose = false;
this._walkState = null;
}
else
{
//移动时的动画
if (this._walkState == null)
{
this._walkState = this._armature.animation.FadeIn("moving", -1.0f, -1, 0);
this._walkState.resetToPose = false;
}
//控制角色迈腿方向
if (this._moveHorizontalDir * this._faceDir > 0 || this._moveVerticleDir * this._faceDir > 0) {
this._walkState.timeScale = MAX_MOVE_SPEED_FRONT / NORMALIZE_MOVE_SPEED;
}
else if (this._moveVerticleDir * this._faceDir < 0)
{
this._walkState.timeScale = MAX_MOVE_SPEED_FRONT / NORMALIZE_MOVE_SPEED;
}
else
{
this._walkState.timeScale = -MAX_MOVE_SPEED_BACK / NORMALIZE_MOVE_SPEED;
}
//控制角色移动方向
if (this._moveHorizontalDir * this._faceDir >= 1)
{
this._speedX = MAX_MOVE_SPEED_FRONT * this._faceDir;
Debug.Log (this._moveHorizontalDir * this._faceDir);
}
else if(this._moveHorizontalDir * this._faceDir <= -1)
{
this._speedX = -MAX_MOVE_SPEED_BACK * this._faceDir;
Debug.Log (this._moveHorizontalDir * this._faceDir);
}
if (this._moveVerticleDir * this._faceDir >= 1) {
this._speedY = MAX_MOVE_SPEED_FRONT * this._faceDir;
} else if(this._moveVerticleDir * this._faceDir <= -1){
this._speedY = -MAX_MOVE_SPEED_FRONT * this._faceDir;
}
}
}
}
//玩家类end
网友评论