如何在sprite kit中以触摸方式拖动和移动精灵,但是以网格方式?我已经可以触摸它们了,但我需要模拟一个平铺图...
答案 0 :(得分:0)
覆盖SKScene中的方法:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
然后当您收到此消息时,只需检查位置:
UITouch *touch = [touches anyObject];
if(touch){//
CGPoint location = [touch locationInNode:self];
.. moving code here
}
获得职位后,只需根据触摸当前所在的网格项移动它:
//Grid of 20x20 pixels, with entire grid starting at 0,0
static const CGFloat GridSize = 20;
int gridColumn = (int)(location.x/GridSize); //we can lose the precision here
int gridRow = (int)(location.y/GridSize); //we can lose the precision here
node.position = CGPointMake(gridColumn*GridSize, gridRow*GridSize);
当然你可以使用SKAction为动作制作动画,但是如果你正在跟踪触摸,你可能想要它实时。