[英]How to detect hidden SKSpriteNode by touching them in Spritekit?
[英]How to make SKSpriteNode rotate in the direction of touching?
您能帮我解决我遇到的问题吗?
-(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.