【发布时间】:2013-11-27 21:54:00
【问题描述】:
如何在精灵工具包中以网格方式拖动和移动精灵?我已经可以在触摸时移动它们,但我需要模拟一个平铺地图......
【问题讨论】:
标签: scroll grid sprite sprite-kit
如何在精灵工具包中以网格方式拖动和移动精灵?我已经可以在触摸时移动它们,但我需要模拟一个平铺地图......
【问题讨论】:
标签: scroll grid sprite sprite-kit
覆盖 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 为移动设置动画,但如果您正在跟踪触摸,您可能需要实时进行。
【讨论】: