简体   繁体   中英

How to make SKSpriteNode rotate in the direction of touching?

Could you please help me with the problem I have?

define CC_RADIANS_TO_DEGREES( ANGLE ) (( ANGLE ) * 57.29577951f) // PI

-(void)didMoveToView:(SKView )view { / Setup your scene here */

 _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 *)touches withEvent:(UIEvent )event { / Called when a touch begins */

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;

    float angle = atan2f(deltaY, deltaX); SKAction *action = [SKAction rotateByAngle: CC_RADIANS_TO_DEGREES(angle) duration: 5.0];

    [_kajak runAction:action]; }

The kayak is not rotate in the direction the I touch. It is some thing I missing? Please help me. Tank you in advance

The zero angle for SKSpriteNodes is pointing up (as opposed to standard trig functions that treat zero angle as pointing right). I had to create a special angle calculation function to take that into account in my game (see below, in Swift).

You may also need to make sure that the coordinate system you are using for the positions is the same for the sprite and the touch location. From your code (i'm not very comfortable with Obj-C) I believe they are both base on the scene's bounds but I'm not certain.

// 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
}

You also need to use rotateToAngle, not rotateByAngle. And I would suggest you also specify shortestUnitArc : true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM