使用块的 Sprite-kit 动作
Sprite-kit action using blocks
我有:
你点击一个球移动到那个位置。
屏幕上下水平分割。
假设球在下方,您无法点击下方使其移动。您必须单击屏幕的顶部。这与上下翻转有关。
我要做什么:
x 坐标完全没有变化。
当球打到顶部时,它会改变方向并在没有点击的情况下返回
删除 UITouch
位置变量
上下撑系统撑
万事如意,非常感谢。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
//SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
//[sprite runAction:[SKAction repeatActionForever:action]];
for (UITouch *touch in touches) {
location = [touch locationInNode:self];
}
float ballVelocity = self.frame.size.height/3.0;
CGPoint moveDifference = CGPointMake(location.x - ball.position.x,location.y - ball.position.y);
float distanceToMove = sqrtf(moveDifference.x * moveDifference.x +moveDifference.y * moveDifference.y);
float moveDuration = distanceToMove / ballVelocity;
Act_Move = [SKAction moveTo:location duration:moveDuration];
Act_MoveDone = [SKAction runBlock:^(){
NSLog(@"stoped");}];
ActballMoveSeq = [SKAction sequence:@[Act_Move,Act_MoveDone]];
if(((location.y>screenSize.height/2)&&(ball.position.y<screenSize.height/2))||((location.y<screenSize.height/2)&&(ball.position.y>screenSize.height/2))){
if(canTap == true){
[ball runAction:ActballMoveSeq withKey:@"moveBall_seq"];
}
}
}
要使球向上移动一定距离并自行返回,请对您的节点使用 applyImpulse。
// modify the dy value (100) to whatever value suits your needs
[myNode.physicsBody applyImpulse:CGVectorMake(0, 100)];
只要你的节点受到重力的影响,它最终会回来。
我有:
你点击一个球移动到那个位置。
屏幕上下水平分割。
假设球在下方,您无法点击下方使其移动。您必须单击屏幕的顶部。这与上下翻转有关。
我要做什么:
x 坐标完全没有变化。
当球打到顶部时,它会改变方向并在没有点击的情况下返回
删除
UITouch
位置变量上下撑系统撑
万事如意,非常感谢。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called when a touch begins */ //SKAction *action = [SKAction rotateByAngle:M_PI duration:1]; //[sprite runAction:[SKAction repeatActionForever:action]]; for (UITouch *touch in touches) { location = [touch locationInNode:self]; } float ballVelocity = self.frame.size.height/3.0; CGPoint moveDifference = CGPointMake(location.x - ball.position.x,location.y - ball.position.y); float distanceToMove = sqrtf(moveDifference.x * moveDifference.x +moveDifference.y * moveDifference.y); float moveDuration = distanceToMove / ballVelocity; Act_Move = [SKAction moveTo:location duration:moveDuration]; Act_MoveDone = [SKAction runBlock:^(){ NSLog(@"stoped");}]; ActballMoveSeq = [SKAction sequence:@[Act_Move,Act_MoveDone]]; if(((location.y>screenSize.height/2)&&(ball.position.y<screenSize.height/2))||((location.y<screenSize.height/2)&&(ball.position.y>screenSize.height/2))){ if(canTap == true){ [ball runAction:ActballMoveSeq withKey:@"moveBall_seq"]; } } }
要使球向上移动一定距离并自行返回,请对您的节点使用 applyImpulse。
// modify the dy value (100) to whatever value suits your needs
[myNode.physicsBody applyImpulse:CGVectorMake(0, 100)];
只要你的节点受到重力的影响,它最终会回来。