【发布时间】:2014-04-03 13:30:54
【问题描述】:
我是目标 c 的新手,所以仍然非常了解其中的技巧。我有一个游戏,它使用 TheSneakyNarwhal 提供的代码在屏幕上制作虚拟操纵杆。p>
https://github.com/TheSneakyNarwhal/SpriteKit-Joystick
我已经导入了 git 提供的文件,下面是操纵杆实现相关的代码:
-(Joystick *)newJoystickNode {
SKSpriteNode *jsThumb = [SKSpriteNode spriteNodeWithImageNamed:@"joystick"];
SKSpriteNode *jsBackdrop = [SKSpriteNode spriteNodeWithImageNamed:@"dpad"];
Joystick *joystick = [Joystick joystickWithThumb:jsThumb andBackdrop:jsBackdrop];
joystick.position = CGPointMake(jsThumb.size.width, jsThumb.size.width);
joystick.name = @"playerJoystick";
//[joystick setScale:0.8];
return joystick;
}
-(void)joystickMovement
{
Joystick *joystick = (Joystick*)[self childNodeWithName:@"playerJoystick"];
SKSpriteNode *player = (SKSpriteNode*)[self childNodeWithName:@"hero"];
if ((joystick.velocity.x != 0 || joystick.velocity.y != 0) && (self.speed == 1))
{
player.position = CGPointMake(player.position.x + .1 *joystick.velocity.x, player.position.y + .1 * joystick.velocity.y);
}
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
CADisplayLink *velocityTick = [CADisplayLink displayLinkWithTarget:self selector:@selector(joystickMovement)];
[velocityTick addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[self addChild:[self newJoystickNode]];
[self addChild:[self newFireButtonNode]];
}
return self;
}
这个操纵杆在屏幕上移动角色并且工作正常。我已按照上述 git 提供的说明进行操作。操纵杆位于屏幕的左下角。问题是在某些情况下,如果我用操纵杆移动角色并触摸屏幕上的其他任何位置,这可能会干扰操纵杆并将其拉到右侧。
有没有人在 spriteKit 游戏杆上使用过这个实现并且遇到过这个问题?或者有人知道为什么会这样吗?
【问题讨论】:
标签: ios objective-c sprite-kit joystick