How to have a character moved around the screen?

[亡魂溺海] 提交于 2019-12-13 05:18:37

问题


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!


回答1:


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]);

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.

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.



来源:https://stackoverflow.com/questions/5359283/how-to-have-a-character-moved-around-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!