Spritekit Add a height marker to the game bounce character

Source: Internet
Author: User
Tags spritekit

This is a similar to jumping Doodle small game, the protagonist constantly eat energy ball to get jumping energy to jump to a higher place, if the picture hit the black hole is hung ...

In the game debugging process if you can know in real time the height of the protagonist is good, which will help the program ape dynamic judgment game victory and defeat logic.

You can use Print to display the height in the console, but this is not very convenient to watch, simply let us put the height of the logo and the protagonist of the game to close together!;]

Because any Sknode object and its child objects in Spritekit can contain children, they can be moved with the parent node, rotated, scaled ... That's the effect we want.

First, add the following code after the code that created the game lead:

Add a label with a display height above the player let label = Sklabelnode (fontnamed:"Copperplate") label. Horizontalalignmentmode=. CenterLabel. Verticalalignmentmode=. TopLabel. Position= Cgpoint (x:0,y: Self. Size. Height/2.0+ -) label. FontSize=20.0Label. FontColor= Skcolor. Whitecolor() label. Text="\ (SELF.POSITION.Y)"Label. Name="Heightlabel"Self. AddChild(label)

One would ask why the label's position is (0,size.height/2.0 + 20.0), because its parent node anchor is in the center of the texture, i.e. (0.5,0.5), so label.position.x = 0 means the x axis is in the center position, The y-axis is located at the top of the game leading up to 20 points.

Finally we will refresh the array in real time and add the following line to the Gamescene Update method:

iflet label = playerNode!.childNodeWithName("heightLabel"as? SKLabelNode{                "\(Int(playerNode!.position.y))"            }

To compile and run the app, let's look at the effect:

Spritekit Add a height marker to the game bounce character

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.