如何让一个人物在屏幕上移动? [英] How to have a character moved around the screen?

查看:500
本文介绍了如何让一个人物在屏幕上移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个操纵杆(最后),我想知道如何根据操纵杆的位置,如何获得一个英雄在屏幕上移动?我使用Cocos2d和任何帮助将非常感谢!

I already have a joystick programmed (finally) and I was wondering how I would get a 'hero' to move about the screen based on where the joystick is? I am using Cocos2d and any help would be greatly appreciated!

推荐答案

最简单的方法是子类 CCNode ,然后像处理任何其他 CCNode 一样处理它。首先,您可以像下面这样更改播放器的位置结构:

The simplest way to do this is to subclass CCNode for your player object and then manipulate it as you would any other CCNode. To start with, you can change the player's position struct like so:

player.position = ccp( player.position.x + [joystick xValue],
                       player.position.y + [joystick yValue]);

这是做一些假设你想要你的摇杆和播放器的行为,的操纵杆类,但希望你能得到我的漂移。如果不清楚, [joystick xValue] [joystick yValue] 将是一些有界值1和1,表示)表示触摸在操纵杆上的位置。根据值的范围,您可能需要通过一些常数缩放,以更快或更慢地移动播放器。

This is making some assumptions about how you want your joystick and player to behave, as well as some properties of the joystick class, but hopefully you get my drift. If it isn't clear, [joystick xValue] and [joystick yValue] would be some bounded value (between -1 and 1, say) representing the touch's position on the joystick. Depending on the range of values, you may want to then scale this by some constant to move the player faster or more slowly.

cocos2d网站有一个很好的初学者教程,我强烈推荐。你也许还想读一读 CCAction 课程,以便进行更复杂的演习。

The cocos2d web site has a great tutorial for beginners that I'd highly recommend. You might want to read up on the CCAction class as well for more complicated maneuvers.

这篇关于如何让一个人物在屏幕上移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆