use*_*273 5 objective-c ios sprite-kit
我的场景中有一个物体.当我触摸屏幕时,我希望对象的y位置成为我触摸的y位置.为此,我有以下代码:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
SKNode *player = [self childNodeWithName:@"player"];
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
player.position = CGPointMake(player.position.x, positionInScene.y);
}
Run Code Online (Sandbox Code Playgroud)
它适用于此代码,但如何让对象以稳定的速度移动到触摸位置?因此,不是跳到触摸y位置,而是以预定义的速度移动到它.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
SKNode *player = [self childNodeWithName:@"player"];
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
// Determine speed
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(player.position.x, positionInScene.y); duration:actualDuration];
[player runAction:actionMove];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6611 次 |
| 最近记录: |