我正在尝试回应场景中的滑动手势并移动精灵作为回应。精灵显示在屏幕的左侧。 handleSwipeRight中的日志记录语句将转到日志。但是精灵根本就没动。当然,我只是遗漏了SKAction的基本内容?
这是我的场景代码:
MFFMyScene.h:
#import <SpriteKit/SpriteKit.h>
@interface MFFMyScene : SKScene <UIGestureRecognizerDelegate>
@property (nonatomic) SKSpriteNode *player;
@property (nonatomic) UISwipeGestureRecognizer * swipeRightGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeLeftGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeUpGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeDownGesture;
@end
来自MFFMyScene.m的相关位:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
NSLog(@"Size: %@", NSStringFromCGSize(size));
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"dungeon"];
SKTexture *texture = [atlas textureNamed:@"hero_trans.png"];
_player = [SKSpriteNode spriteNodeWithTexture:texture];
_player.position = CGPointMake(10, 150);
[self addChild:_player];
}
return self;
}
-(void) didMoveToView:(SKView *)view {
_swipeRightGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSwipeRight:)];
_swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[view addGestureRecognizer:_swipeRightGesture];
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)sender {
if(sender.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"scene: SWIPE RIGHT");
SKAction *movePlayerRight = [SKAction moveByX:10.0
y:0.0
duration:100.0];
[_player runAction:movePlayerRight];
}
}
答案 0 :(得分:0)
使您的属性变得强大,使用setter让系统为您做正确的内存管理,例如
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe;
self.rightSwipe = /* init code here */
检查滑动手势状态:
- (void)swipeHandled:(UISwipeGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateRecognized) {
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
/* moving code here */
}
}
}
答案 1 :(得分:0)
我使用这个SKAction错了。我正在寻找能够“每隔z秒移动x / y”或“每帧移动x / y z秒”的SKAction。我最初写它的方式,SKAction将在100秒内将精灵总数提高10点。
所以我认为精灵毕竟是在移动,但我的X / Y /持续时间值是如此之小,以至于它看起来好像没有移动。当x = 10且持续时间= 100秒时,它在100秒内移动了10个点。我甚至不了解点与像素,但这是缓慢的运动。
我把它改为x = 100,y = 50,持续时间= 1.0,它移动得很好。