How to have a character moved around the screen?
I already have a jo开发者_JAVA技巧ystick 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!
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.
精彩评论