【发布时间】:2014-10-04 16:51:44
【问题描述】:
我希望我的精灵像这样从屏幕的一侧移动到另一侧:
点击前:
然后在屏幕被点击后:
会不会是这样开始的:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
谢谢!
【问题讨论】:
标签: ios objective-c ios7 sprite-kit
我希望我的精灵像这样从屏幕的一侧移动到另一侧:
点击前:
然后在屏幕被点击后:
会不会是这样开始的:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
谢谢!
【问题讨论】:
标签: ios objective-c ios7 sprite-kit
我不太确定您是想在触摸屏幕时获取手指的位置并将其应用于精灵的位置,还是只是在触摸屏幕时更改精灵的位置。
如果您想在 touchesBegan 中获取手指的位置并将该位置应用于精灵:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView:self];
spriteNode.position = location;
}
}
如果您只想在每次触摸时将精灵的位置更改为树的另一侧:
首先,您需要跟踪您的精灵当前位于哪一侧。让我们创建一个 BOOL 来保存该信息:
BOOL isRightSide;
然后,在开始时,如果您的精灵从树的右侧开始,只需将 TRUE 分配给布尔值。
最后,在触摸事件中:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isRightSide) {
//Change to sprite's position to the LEFT side
} else {
//Change to sprite's position to the RIGHT side
}
isRightSide = !isRightSide;
}
我希望这会有所帮助 =)
【讨论】: