Rotate the sprite to a sprite position not exact in SpriteKit, with Swift

I'm trying to turn the player to the touch. Using the touch move function, I set the location to a variable. Then I call the function. It works, but the corner is off. Location is a global variable, and the player is the sprite I want to rotate. This is about 15 degrees.

func rotatePlayer(){ let angle = atan2(location!.y - player!.position.y , location!.x - player!.position.x) player?.zRotation = angle } 
+5
source share
3 answers

According to SpriteKit Best Practices doc

Use in-game logical and artistic assets that comply with SpriteKits coordinating and rotational conventions. This means the pattern is oriented to the right. If you orient the work in any other direction, you need to convert the angles between the conventions used in the technique and the conventions used by SpriteKit.

Since the spaceship points up by default (when zRotation is 0), you will need to compensate for the angle by 90 degrees (pi / 2 radians) so that the ship is on the right when zRotation is zero:

 player?.zRotation = angle - CGFloat(M_PI_2) 

Alternatively, you can turn the spaceship to the right. To rotate the image, click "Assets.xcassets" and then click "Spaceship." Right-click on the image "Spaceship" and select "Open in an external editor." This will open the image in the Preview application. In the preview window, select Tools> Rotate Right and Abort. Turning the cover, your code should work without any changes.

+1
source

Try something like this

 //set sprite to image Person = SKSpriteNode(imageNamed: "Person") //set size Person.size = CGSize(width: 40, height: 7) //set position Person.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 120) //rotate node 3.14 / 2 = to 90 degree angle Person.zRotation = 3.14 / 2 Person.zPosition = 2.0 
0
source

Use orientation restriction to rotate the object to the touch position:

 // Create two global Sprite properties: var heroSprite = SKSpriteNode(imageNamed:"Spaceship") var invisibleControllerSprite = SKSpriteNode() override func didMoveToView(view: SKView) { // Create the hero sprite and place it in the middle of the screen heroSprite.xScale = 0.15 heroSprite.yScale = 0.15 heroSprite.position = CGPointMake(self.frame.width/2, self.frame.height/2) self.addChild(heroSprite) // Define invisible sprite for rotating and steering behavior without trigonometry invisibleControllerSprite.size = CGSizeMake(0, 0) self.addChild(invisibleControllerSprite) // Define a constraint for the orientation behavior let rangeForOrientation = SKRange(constantValue: CGFloat(M_2_PI*7)) heroSprite.constraints = [SKConstraint.orientToNode(invisibleControllerSprite, offset: rangeForOrientation)] } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */ for touch: AnyObject in touches { // Determine the new position for the invisible sprite: // The calculation is needed to ensure the positions of both sprites // are nearly the same, but different. Otherwise the hero sprite rotates // back to it original orientation after reaching the location of // the invisible sprite var xOffset:CGFloat = 1.0 var yOffset:CGFloat = 1.0 var location = touch.locationInNode(self) if location.x>heroSprite.position.x { xOffset = -1.0 } if location.y>heroSprite.position.y { yOffset = -1.0 } // Create an action to move the invisibleControllerSprite. // This will cause automatic orientation changes for the hero sprite let actionMoveInvisibleNode = SKAction.moveTo(CGPointMake(location.x - xOffset, location.y - yOffset), duration: 0.2) invisibleControllerSprite.runAction(actionMoveInvisibleNode) // Optional: Create an action to move the hero sprite to the touch location let actionMove = SKAction.moveTo(location, duration: 1) heroSprite.runAction(actionMove) } } 

Tutorial: http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html

Video: https://youtu.be/8d8MH_gXt84

0
source

Source: https://habr.com/ru/post/1237482/


All Articles