簡體   English   中英

如何使SKSpriteNode沿觸摸方向旋轉?

[英]How to make SKSpriteNode rotate in the direction of touching?

您能幫我解決我遇到的問題嗎?

定義CC_RADIANS_TO_DEGREES( 角度 )(( 角度 )* 57.29577951f)// PI

-(void)didMoveToView:(SKView )view {/在此處設置場景* /

 _skyColor = [SKColor colorWithRed:113.0/255.0 green:197.0/255.0 blue:207.0/255.0 alpha:1.0];
[self setBackgroundColor:_skyColor];


//Setup the array to hold the walking frames
NSMutableArray *padlingFrames = [NSMutableArray array];

//Load the TextureAtlas for the bear
SKTextureAtlas *kajakAnimatedAtlas = [SKTextureAtlas atlasNamed:@"KajakImages"];

//Load the animation frames from the TextureAtlas
long numImages = kajakAnimatedAtlas.textureNames.count;
for (int i=1; i <= numImages; i++) {
    NSString *textureName = [NSString stringWithFormat:@"kajak_0%d", i];
    SKTexture *temp = [kajakAnimatedAtlas textureNamed:textureName];
    [padlingFrames addObject:temp];
}
_kajakPadlingFrames = padlingFrames;

//Create kajak sprite, setup position in middle of the screen, and add to Scene
SKTexture *temp = _kajakPadlingFrames[0];
_kajak = [SKSpriteNode spriteNodeWithTexture:temp];

_kajak.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

[_kajak setScale:0.2];
[self addChild:_kajak];

}

-(void)touchesBegan:(NSSet * )用event :(UIEvent )event {/觸摸開始時調用* /

CGPoint touchLocation = [[touches anyObject] locationInNode:self];

[self updateRotate:touchLocation];

}

  • (void)updateRotate:(CGPoint)touchLocation {

    float deltaX = touchLocation.x-_kajak.position.x; float deltaY = touchLocation.y-_kajak.position.y;

    浮角= atan2f(deltaY,deltaX); SKAction * action = [SKAction rotationByAngle:CC_RADIANS_TO_DEGREES(angle)持續時間:5.0];

    [_kajak runAction:action]; }

皮划艇沒有朝我觸摸的方向旋轉。 這是我想念的東西嗎? 請幫我。 提前給你加油

SKSpriteNodes的零角度朝上(與將零角度視為向右的標准觸發函數相反)。 我必須創建一個特殊的角度計算功能,以在游戲中將其考慮在內(請參見下面的Swift)。

您可能還需要確保用於該位置的坐標系對於精靈和觸摸位置相同。 根據您的代碼(我對Obj-C不太滿意),我相信它們都基於場景的邊界,但我不確定。

// angle between two points
// ------------------------
// SpriteKit's zero angle is pointing up 
// atan2 returns an angle pointing right
// we're usung sprite kit's conventions so removing 90° from the result
//
func spriteAngleFrom(start:CGPoint, to finish:CGPoint) -> CGFloat
{
   let deltaX = finish.x - start.x
   let deltaY = finish.y - start.y 
   return atan2(deltaY,deltaX) - CGFloat.pi/2
}

您還需要使用rotateToAngle,而不是rotateByAngle。 我建議您也指定shortestUnitArc:true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM