【问题标题】:Sprite node won't move when touch is recognized in Sprite Kit在 Sprite Kit 中识别触摸时,Sprite 节点不会移动
【发布时间】:2014-08-13 00:16:56
【问题描述】:

当用户触摸屏幕上的某个点时,我试图让精灵节点移动。正在识别我的触摸,但没有发生任何动作!

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    self.anchorPoint = CGPointMake(0.5, 0.5);

    SKSpriteNode *_box = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
    _box.position = CGPointMake(0.5,0.5);

    [_box addChild:_box];
}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];

    _currentTouch = touch;
}
}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
CGPoint currentLocation = [_currentTouch locationInView:_currentTouch.view];
if (currentLocation.y > 300) {
    _box.position = CGPointMake(_box.position.x, _box.position.y + 20);
    NSLog(@"touching");
     }
}

@end

我不确定我是不是走错了路,但我可以拼命寻求帮助!谢谢!

【问题讨论】:

    标签: ios xcode position sprite-kit touchesbegan


    【解决方案1】:

    您的代码中有许多错误,因此向您展示一个满足您要求的示例项目会更容易:

    #import "MyScene.h"
    
    @implementation MyScene
    {
        SKSpriteNode *myNode;
    }
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            myNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(30, 30)];
            myNode.position = CGPointMake(100, 100);
            [self addChild:myNode];
        }
        return self;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self.scene];
    
        if(touchLocation.y > 200)
        {
            myNode.position = CGPointMake(myNode.position.x, myNode.position.y+10);
        }
    }
    
    -(void)update:(CFTimeInterval)currentTime
    {
        //
    }
    
    @end
    

    【讨论】:

    • 非常感谢!完美运行!
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多