![](/img/trans.png)
[英]Swift SpriteKit run SKAction in SKAction.runBlock()
[英]Spritekit how to run an SKAction depending on where a touch and hold is at
因此,我嘗試在游戲中實現一種方法,如果玩家的手指在y軸或更低的方向上觸摸屏幕(例如說50),則SKAction將運行以將角色精靈向下移動,並且如果他們的手指在觸摸高於50,另一個動作將使精靈向上移動。 我不知道您如何將它識別為觸摸並按住。 請幫助。
您需要捕獲用戶的觸摸事件才能完成此操作。 我建議您查看RayWenderlich提供的出色的教程,該教程位於: 動畫紋理和通過觸摸事件移動它們
如果您沒有時間這樣做,那么您的代碼可能會像這樣:
首先,您需要了解的是兩個最通用的觸摸事件:-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event和:-( void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
基本上,當用戶將手指放在屏幕上時,將開始觸摸開始方法,而當用戶將手指從屏幕上移開時,將結束觸摸結束方法。
在此示例中,我將使用touchesEnded,但可以隨意更改它,就像將“ Ended”與“ Began”切換一樣容易。
我將向您展示的所有代碼都將在場景實現文件中進行:
如果只想讓精靈在特定點之上向上移動而在特定點之下向下移動,請使用以下命令:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//used to hold the location to travel
CGPoint moveLocation;
//check if the Y location is less than 50
if(location.y < 50)
{
//the the location to the bottom of the screen
moveLocation = CGPointMake(sprite.position.x, 0);
}
//and then if its more than 50
else
{
//set the locaiton to the top of the screen
moveLocation = CGPointMake(sprite.position.x, self.frame.size.height);
}
//move the sprite
//Check if their already moving
if(sprite actionForKey:@"moving")
{
//If they are stop them so they can move in the new direction
[sprite removeActionForKey:@"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:5.0f];
//remember to give the action a key so that we can check for it during the above if statement
[sprite runAction:moveAction withKey:@"moving"];
}
那里有它。 唯一的缺陷是,無論您與該位置的距離如何,到達該位置始終需要5秒鍾,請繼續閱讀以下內容,以獲取有關如何更正該位置的提示,或者如果您想了解如何使該精靈移動到任何位置觸摸屏幕上的位置。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//move the sprite
//Check if their already moving
if(sprite actionForKey:@"moving")
{
//If they are stop them so they can move in the new direction
[sprite removeActionForKey:@"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:5.0f];
//remember to give the action a key so that we can check for it during the above if statement
[sprite runAction:moveAction withKey:@"moving"];
}
該代碼將使其保持原狀,以便在5秒鍾內將精靈移動到屏幕上觸摸的位置。 只要記住用您的SKSpriteNode變量的名稱替換“ sprite”的實例即可。 對於更復雜的運動,請嘗試以下操作:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//set the velocity of the subject
float velocity = self.frame.size.width/3.0;
//The above will ensure that it will take the sprite 3 seconds to travel the distance of the screen
//Determine the difference between the point touched and the sprites position
CGPoint moveDifference = CGPointMake(location.x - sprite.position.x, location.y - sprite.position.y);
//Use pythagorean theorem to figure out the actual length to move
float distanceToMove = sqrtf(moveDifference.x * moveDifference.x + moveDifference.y*moveDifference.y);
//Use the distance to travel and the velocity to determine how long the sprite should move for
float moveDuration = distanceToMove / velocity;
//and finally move the sprite
if(sprite actionForKey:@"moving")
{
[sprite removeActionForKey:@"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:moveDuration];
[sprite runAction:moveAction withKey:@"moving"];
}
這將為子畫面設置速度,確定行進的長度,以及到達新位置所需的時間。
如果要涉及紋理,我強烈建議您閱讀我提供的鏈接。
根據要求,我將很高興提供使用力來控制運動速度的示例。
希望能對您有所幫助,讓我知道我是否有可以運行代碼的位置,但是我確定這是可以的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.